本文最后更新于 2021-08-16 10:57:30
函数 查看函数
函数有库的概念,系统提供的除外,系统提供的函数可以在任意库使用!
查看当前库所有的函数:show functions;
查看函数的使用: desc function 函数名;
查看函数的详细使用: desc function extended 函数名;
函数分类
函数来源
函数特征
UDF:用户定义函数 一进一出
UDTF:用户定义表生成函数,一进多出 传入一个参数,返回一个结果集,UDTF必须紧跟select,并且不能有其他查询参数
UDAF:用户定义聚集函数 多进一出 类似于 count avg
常用函数 unix_timestamp()->时间转time_stamp
from_unixtime()->时间戳转日期
当前日期
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 SELECT CURRENT_TIMESTAMP () SELECT unix_timestamp("2021-08-16 15:24:17") SELECT unix_timestamp("2021-08-16 15:24:17","yyyy-MM-dd HH:mm:ss") SELECT from_unixtime(1629098657 ,"yyyy-MM-dd HH:mm:ss") SELECT from_utc_timestamp(1629098657000 ,"PRC"), from_utc_timestamp("2021-08-16 15:24:17","PRC"), from_utc_timestamp("2021-08-16 15:24:17","GMT+8"), from_utc_timestamp("2021-08-16 15:24:17","UTC"), from_utc_timestamp("2021-08-16 15:24:17","GMT") SELECT unix_timestamp("1970-01-01 08:00:00") SELECT from_unixtime(0 ,"yyyy-MM-dd HH:mm:ss")
常用日期函数
hive默认解析的日期必须是: 2019-11-24 08:09:10
unix_timestamp:返回当前或指定时间的时间戳
from_unixtime:将时间戳转为日期格式
current_date:当前日期
current_timestamp:当前的日期加时间
to_date:抽取日期部分
year:获取年
month:获取月
day:获取日
hour:获取时
minute:获取分
second:获取秒
weekofyear:当前时间是一年中的第几周
dayofmonth:当前时间是一个月中的第几天
months_between: 两个日期间的月份,前-后
add_months:日期加减月
datediff:两个日期相差的天数,前-后
date_add:日期加天数
date_sub:日期减天数
last_day:日期的当月的最后一天
date_format格式化日期 date_format(‘2019-11-24 08:09:10’,’yyyy-MM’) mn
常用取整函数
round: 四舍五入
ceil: 向上取整
floor: 向下取整
常用字符串操作函数
upper: 转大写
lower: 转小写
length: 长度
trim: 前后去空格
lpad: 向左补齐,到指定长度
rpad: 向右补齐,到指定长度
regexp_replace: SELECT regexp_replace(‘100-200’, ‘(\d+)’, ‘num’) 使用正则表达式匹配目标字符串,匹配成功后替换!
集合操作
size: 集合(map和list)中元素的个数
map_keys: 返回map中的key
map_values: 返回map中的value
array_contains: 判断array中是否包含某个元素
sort_array: 将array中的元素排序
常用函数2 NVL(string1,replace_with)
判断string1是否为null,如果为null,就替换为replace_with,否则不做操作
使用场景
将null替换为默认值
使用avg()等函数的时候 为null的值不参与总数计算 将其替换为0后就参与计算
SUBSTRING(str,pos[,len]) 截取字符串
CONCAT(str1,str2,….strN)
1 select concat('aaaa','bbbb') ; --aaabbb
CONCAT_WS(separator,[string,array(string)]+)
带有分隔符的拼接
1 select concat_ws('.','www',array('google','com')); -- www.google.com
COLLECT_SET(x)
列转行 一列N行转为一列一行
返回一个集合
集合中元素不重复
结合concat 类似于mysql
1 2 SELECT GROUP_CONCAT( DISTINCT a.REGION_ID ORDER BY a.REGION_ID DESC SEPARATOR ' ') FROM t_region a;
COLLECT_LIST(x)
列转行 一列N行转为一列一行
返回一个list
集合中元素可以重复
结合concat 类似于mysql
1 2 SELECT GROUP_CONCAT(a.REGION_ID ORDER BY a.REGION_ID DESC SEPARATOR ' ') FROM t_region a;
1 select concat_ws ('|' ,collect_list (REGION_ID)') from t_regionexplode(a)
EXPLODE(a) ->UDTF
1 select movie,col1 from movie_info lateral view explode(category) tmp1 as col1
用法:LATERAL VIEW udtf(expression) tableAlias AS columnAlias 解释:lateral view用于和split, explode等UDTF一起使用,它能够将一行数据拆成多行数据 ,在此基础上可以对拆分后的数据进行聚合。 lateral view首先为原始表的每行调用UDTF,UTDF会把一行拆分成一或者多行,lateral view再把结果组合,产生一个支持别名表的虚拟表。
COUNT高级用法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 SELECT type , count (* ) , count (DISTINCT u) , count (CASE WHEN plat= 1 THEN u ELSE NULL END ) , count (DISTINCT CASE WHEN plat= 1 THEN u ELSE NULL END ) , count (CASE WHEN (type= 2 OR type= 6 ) THEN u ELSE NULL END ) , count (DISTINCT CASE WHEN (type= 2 OR type= 6 ) THEN u ELSE NULL END ) FROM t WHERE dt in ("2012-1-12-02", "2012-1-12-03") GROUP BY type ORDER BY type ;
排序 order by order by会对输入做全局排序,因此只有一个Reducer(多个Reducer无法保证全局有序),然而只有一个Reducer,会导致当输入规模较大时,消耗较长的计算时间。
sort by sort by不是全局排序,其在数据进入reducer前完成排序,因此,如果用sort by进行排序,并且设置mapred.reduce.tasks>1,则sort by只会保证每个reducer的输出有序,并不保证全局有序。sort by不同于order by,它不受hive.mapred.mode属性的影响,sort by的数据只能保证在同一个reduce中的数据可以按指定字段排序。使用sort by你可以指定执行的reduce个数(通过set mapred.reduce.tasks=n来指定),对输出的数据再执行归并排序,即可得到全部结果
distribute by distribute by是控制在map端如何拆分数据给reduce端的。hive会根据distribute by后面列,对应reduce的个数进行分发,默认是采用hash算法。sort by为每个reduce产生一个排序文件。在有些情况下,你需要控制某个特定行应该到哪个reducer,这通常是为了进行后续的聚集操作。distribute by刚好可以做这件事。因此,distribute by经常和sort by配合使用。
distribute by要放在sort by前面
cluster by cluster by除了具有distribute by的功能外还兼具sort by的功能。但是排序只能是倒叙排序,不能指定排序规则为ASC或者DESC。
cast 1 2 select cast (date as datetime) as date from table1;select cast (date as date ) as date from table1;
substr & substring 1 2 3 4 5 6 7 substr(string A,int start ) substring(string A,int start ) substr(string A,int start ,int len) substring(string A,int start ,int len)
coalesce 1 2 3 coalesce (expression_1, expression_2, …,expression_n)coalesce (a,b,c);
like 1 2 3 4 5 6 A LIKE B LIKE (A, B) select 'football' like '%ba%' ; select like ('football' , '__otba%' )
rlike & regexp 1 2 3 4 5 6 7 8 9 10 11 A RLIKE B RLIKE(A, B)select 'football' rlike '^footba' select rlike('football' , 'ba' ) select 'football' rlike 'ba' select 'football' regexp '^footba' select regexp('football' , 'ba' ) select 'football' regexp 'ba'
regexp_replace 1 2 3 4 regexp_replace( A, B, C) select regexp_replace('h234ney' , '\\d+' , 'o' )
if 1 2 3 4 5 if(BOOLEAN testCondition, T valueTrue, T valueFalseOrNull)select if(1 is null ,0 ,1 ) select if(null is null ,0 ,1 )
isnotnull 1 2 3 4 5 isnotnull(a)select isnotnull(1 ) select isnotnull(null )
isnull 1 2 3 4 isnull(a)select isnull(null ) select isnull(1 )
nullif 1 2 3 4 5 nullif (a, b)select nullif (1 ,2 ) select nullif (1 ,1 )
nvl 1 2 3 4 nvl(T value , T default_value)select nvl(1 ,0 ) select nvl(null ,0 )
add_months 1 2 3 4 5 6 7 add_months(DATE | STRING| TIMESTAMP start_date, INT num_months)select add_months("2020-05-20",2 ); select add_months("2020-05-20",8 ); select add_months("2020-05-31",1 );
current_date 1 2 3 current_date ()select current_date ()
current_timestamp 1 2 3 current_timestamp ()select current_timestamp ()
datediff 1 2 3 4 datediff(STRING enddate, STRING startdate)select datediff("2020-05-20","2020-05-21"); select datediff("2020-05-21","2020-05-20");
date_add & date_sub 1 2 3 4 5 6 7 8 date_add(DATE startdate, INT days) date_sub(DATE startdate, INT days)select date_add("2020-05-20",1 ); select date_add("2020-05-20",-1 ); select date_sub("2020-05-20",1 ); select date_sub("2020-05-20",-1 );
1 2 3 4 5 6 7 8 date_format(DATE | TIMESTAMP | STRING ts, STRING fmt)select date_format('2020-05-20' , 'yyyy' ); select date_format('2020-05-20' , 'MM' ); select date_format('2020-05-20' , 'dd' ); select date_format('2020-05-20' , 'yyyy年MM月dd日 HH时mm分ss秒' ) ;select date_format('2020-05-20' , 'yy/MM/dd' )
1 2 3 4 5 6 7 8 9 10 11 12 extract (field FROM source)select extract (year from '2020-05-20 15:21:34.467' ); select extract (quarter from '2020-05-20 15:21:34.467' ); select extract (month from '2020-05-20 15:21:34.467' ); select extract (week from '2020-05-20 15:21:34.467' ); select extract (dayofweek from '2020-05-20 15:21:34.467' ); select extract (day from '2020-05-20 15:21:34.467' ); select extract (hour from '2020-05-20 15:21:34.467' ); select extract (minute from '2020-05-20 15:21:34.467' ); select extract (second from '2020-05-20 15:21:34.467' );
year&quarter&month&day&hour&minute&second 1 select second ('2020-05-20 15:21:34' );
trunc 1 2 3 4 5 select trunc('2020-05-20' , 'YY' ); select trunc('2020-05-20' , 'MM' ); select trunc('2020-05-20 15:21:34' , 'MM' );
next_day 1 2 3 4 5 6 7 8 9 10 11 12 13 14 next_day(STRING start_date, STRING day_of_week)select next_day('2020-05-20' ,'Mon' );select next_day('2020-05-20' ,'Tu' );select next_day('2020-05-20' ,'Wed' );select next_day('2020-05-20' ,'Th' );select next_day('2020-05-20' ,'Fri' );select next_day('2020-05-20' ,'Sat' ); select next_day('2020-05-20' ,'Sun' ); select date_add(next_day('2020-05-20' ,'MO' ),-7 ); select date_add(next_day('2020-05-20' ,'MO' ),-1 );