Hive(七)【内置函数】

一.系统内置函数

1.查看系统自带内置函数

show functions;

2.查看函数的具体用法

如查看upper函数

desc function extended upper;

二.常用内置函数

1.数学函数

round

round(DOUBLE a):返回对a四舍五入的BIGINT值

round(DOUBLE a, INT d):返回DOUBLE型d的保留n位小数的DOUBLE型的近似值

案例

0: jdbc:hive2://hadoop102:10000> select round(4.5),round(4.12345,2);
+------+-------+
| _c0  |  _c1  |
+------+-------+
| 5    | 4.12  |
+------+-------+

2.字符函数

split

split(string str, string pat) 返回类型:array

解析:按照正则表达式pat来分割字符串str,并将分割后的数组字符串的形式返回。

案例

0: jdbc:hive2://hadoop102:10000> select split('aaa-bbb-ccc','-');
+----------------------+
|         _c0          |
+----------------------+
| ["aaa","bbb","ccc"]  |
+----------------------+

substring

substring(string A, int start, int len) 返回类型:string

解析:对于字符串A,从start位置开始截取长度为length的字符串并返回

案例

0: jdbc:hive2://hadoop102:10000> select substring('2020-06-29',1,7);
+----------+
|   _c0    |
+----------+
| 2020-06  |
+----------+
concat

concat(string A, string B...) 返回类型:string

解析:将每个字符串拼接,也可以是sql查询的字段

案例

0: jdbc:hive2://hadoop102:10000> select concat('aaa','--','bbb','|','ccc');
+---------------+
|      _c0      |
+---------------+
| aaa--bbb|ccc  |
+---------------+
concat_ws

concat_ws(string SEP, string A, string B...)

解析:sep是分割符,其余字符串以这个分割符拼接

案例

0: jdbc:hive2://hadoop102:10000> select concat_ws('-','aaa','bbb','ccc');
+--------------+
|     _c0      |
+--------------+
| aaa-bbb-ccc  |
+--------------+
lower,upper

解析:lower将字符串转全部转为小写,upper将字符串转全部转为大写

案例

0: jdbc:hive2://hadoop102:10000> select lower('Hello World') ,upper('Hello World');
+--------------+--------------+
|     _c0      |     _c1      |
+--------------+--------------+
| hello world  | HELLO WORLD  |
+--------------+--------------+
trim

解析:去前后空格

length

解析:字符串长度,字符数

3.日期函数

to_date

解析:从一个字符串中取出为日期的部分

案例

0: jdbc:hive2://hadoop102:10000> select to_date('2020-06-29 20:34:01');
+-------------+
|     _c0     |
+-------------+
| 2020-06-29  |
+-------------+
yaer、month、day

解析:从一个日期中取出相应的年、月、日

案例

0: jdbc:hive2://hadoop102:10000> select year('2020-06-29 20:34:01'),month('2020-06-29 20:34:01'),day('2020-06-29 20:34:01');
+-------+------+------+
|  _c0  | _c1  | _c2  |
+-------+------+------+
| 2020  | 6    | 29   |
+-------+------+------+

4.条件函数

case...when...

语法格式:case A when B then C [when D then E]* [else F] end

解析:对于A来说,如果判断为B则返回C,如果判断为D则返回E(此处判断条件可为多个),如果以上都不是则返回F。注意,最后还有还有一个end结束符

案例

需求:求不同部门的男女各有多少人

原始数据 : emp_sex.txt

name dept_id sex
悟空	A	男
大海	A	男
宋宋	B	男
凤姐	A	女
婷姐	B	女
婷婷	B	女

建表

create table emp_sex(
name string,
dept_id string,
sex string
)
row format delimited fields terminated by '	';

把原始数据导入表

load data local inpath '/opt/module/testdata/emp_sex.txt' into table emp_sex;

查看一下

select * from emp_sex;
name|dept_id|sex|
----|-------|---|
悟空  |A      |男  |
大海  |A      |男  |
宋宋  |B      |男  |
凤姐  |A      |女  |
婷姐  |B      |女  |
婷婷  |B      |女  |

实现:分组统计某个字段不同值得数量;

select 
dept_id,
sum(case when sex='男' then 1 else 0 end) man_count,
sum(case when sex='女' then 1 else 0 end) woman_count
from emp_sex
group by dept_id;
nvl

语法:NVL( 字段值,value)

解析:给空字段赋值,如果字段值为空则赋值为value,否则字段值不变;若都为null那么返回null

案例

0: jdbc:hive2://hadoop102:10000> select nvl('aaa',1),nvl(null,1);
+------+------+
| _c0  | _c1  |
+------+------+
| aaa  | 1    |
+------+------+

常用函数查询


常用日期函数
unix_timestamp:返回当前或指定时间的时间戳	        select unix_timestamp();  select unix_timestamp('2008-08-08 08:08:08'); 
from_unixtime:将时间戳转为日期格式                 select from_unixtime(1218182888);
current_date:当前日期                  select current_date();
current_timestamp:当前的日期加时间     select current_timestamp();
to_date:抽取日期部分                   select to_date('2008-08-08 08:08:08');   select to_date(current_timestamp());
year:获取年                            select year(current_timestamp());
month:获取月                           select month(current_timestamp());
day:获取日                             select DAY(current_timestamp());
hour:获取时                            select HOUR(current_timestamp());
minute:获取分                          select minute(current_timestamp());
second:获取秒                          select SECOND(current_timestamp());
weekofyear:当前时间是一年中的第几周    select weekofyear(current_timestamp());  select weekofyear('2020-01-08');
dayofmonth:当前时间是一个月中的第几天  select dayofmonth(current_timestamp());  select dayofmonth('2020-01-08');
months_between: 两个日期间的月份       select months_between('2020-07-29','2020-06-28');
add_months:日期加减月                  select add_months('2020-06-28',1);
datediff:两个日期相差的天数            select datediff('2019-03-01','2019-02-01');   select datediff('2020-03-01','2020-02-01');
date_add:日期加天数                    select date_add('2019-02-28',1);   select date_add('2020-02-28',1);
date_sub:日期减天数                    select date_sub('2019-03-01',1);   select date_sub('2020-03-01',1);
last_day:日期的当月的最后一天          select last_day('2020-02-28');   select last_day('2019-02-28');
date_format() :格式化日期   日期格式:'yyyy-MM-dd hh:mm:ss'   select date_format('2008-08-08 08:08:08','yyyy-MM-dd hh:mm:ss');  

常用取整函数
round: 四舍五入     select round(4.5);     
ceil:  向上取整     select ceil(4.5);
floor: 向下取整     select floor(4.5);

常用字符串操作函数
upper: 转大写         select upper('abcDEFg');
lower: 转小写         select lower('abcDEFg');
length: 长度          select length('abcDEFg');
trim:  前后去空格     select length('   abcDEFg    ');  select length(trim('   abcDEFg    '));
lpad: 向左补齐,到指定长度   select lpad('abc',11,'*');
rpad:  向右补齐,到指定长度  select rpad('abc',11,'*');  
substring: 剪切字符串         select substring('abcdefg',1,3);     select rpad(substring('13843838438',1,3),11,'*');
regexp_replace: SELECT regexp_replace('100-200', '(\d+)', 'num');   select regexp_replace('abc d e f',' ','');
	使用正则表达式匹配目标字符串,匹配成功后替换!

集合操作
size: 集合中元素的个数
map_keys: 返回map中的key
map_values: 返回map中的value         select size(friends),map_keys(children),map_values(children) from person;
array_contains: 判断array中是否包含某个元素     select array_contains(friends,'lili') from person;
sort_array: 将array中的元素排序         select sort_array(split('1,3,4,5,2,6,9',','));   
                                         select sort_array(split('a,d,g,b,c,f,e',','));

原文地址:https://www.cnblogs.com/wh984763176/p/13210430.html