趁着离职前夕的空档,整理了hive的所有函数,有很多新的发现,后面不断温习吧。
一、函数help
- desc function XX 显示函数描述信息
hive> desc function concat;
OK
concat(str1, str2, … strN) - returns the concatenation of str1, str2, … strN or concat(bin1, bin2, … binN) - returns the concatenation of bytes in binary data bin1, bin2, … binN
- desc function extended XX 显示函数详细描述信息
hive> desc function extended concat;
OK
concat(str1, str2, … strN) - returns the concatenation of str1, str2, … strN or concat(bin1, bin2, … binN) - returns the concatenation of bytes in binary data bin1, bin2, … binN
Returns NULL if any argument is NULL.
Example:
SELECT concat(‘abc’, ‘def’) FROM src LIMIT 1;
‘abcdef’
所有函数都可以根据以上语句查阅语法和例子。
二、有特色的函数
2.1 时间函数
2.1.1 add_months(start_date, num_months) 日期的月份加减
hive> SELECT add_months(‘2009-04-02’, 1);
OK
2009-05-02
hive> SELECT add_months(‘2009-01-31’, 1);
OK
2009-02-28
hive> SELECT add_months(‘2009-01-31’, -1);
OK
2008-12-31
2.1.2 current_date() 当前日期(yyyy-mm-dd)
current_timestamp() 当前日期(yyyy-mm-dd hh:mm:ss.sss)
2.1.3 last_day(date) 日期date所在月份的最后一天
select last_day(‘2019-02-25’);
OK
2019-02-28
2.1.4 next_day(start_date, day_of_week) 生成日期start_date之后的第一个规定星期所对应日期
SELECT next_day(‘2019-03-28’,‘friday’);
OK
2019-03-29
2.1.5 trunc(date, fmt) 按精度fmt对date进行处理,只保留年份或月份(fmt=‘MONTH’/‘MON’/'MM’为月份,fmt=‘YEAR’/‘YYYY’/'YY’为年份)
SELECT trunc(‘2009-02-25’, ‘MM’);
OK
2009-02-01
2.2 数学函数
2.2.1 cbrt(double) 开方
hive> select cbrt(8);
OK
2.0
2.2.2 ceil(x) 或ceiling(x) 向上取整,即取不小于x的最小整数
floor(x) 向下取整
hive> select ceil(4.5);
OK
5
hive> select ceil(-4.5);
OK
-4
2.2.3 conv(num, from_base, to_base) 进制转换函数(to_base如果为负数,则取正处理)
hive> select conv(17,10,2);
OK
10001
hive> select conv(17,10,-2);
OK
10001
2.2.4 a div b a除以b的商(忽略余数)
2.2.5 greatest(v1, v2, …) 一组数的最大值
least(v1, v2, …) 一组数的最小值
hive> select greatest(1,3,4,5);
OK
5
2.2.6 histogram_numer