hive内置函数
一.日期函数
注:函数对应的代码块的第一句都是执行desc function 函数名;
得到的内容
1.from_unixtime(,):
from_unixtime(unix_time, format) - returns unix_time in the specified format
以指定的格式返回时间戳(转成日期)
select from_unixtime(1568201852,'yyyy-MM-dd HH:mm:ss'); ---> 2019-09-11 19:37:32
select from_unixtime(1568207531,'yyyy');-->2019
2.unix_timestamp():
unix_timestamp(date[, pattern]) - Converts the time to a number
(1)select unix_timestamp('2019-09-11 21:24','yyyy-MM-dd HH:mm); --> 1568208240 将日期转换成时间戳
select unix_timestamp('2019-9-11 21:37'); --> null
select unix_timestamp('2019-9-11 21:37:59'); --> 1568209079
(2)select unix_timestamp(); --> 1568207531 给出当前时间戳
3.to_date():
to_date(expr) - Extracts the date part of the date or datetime expression expr
提取日期或日期时间表达式expr的日期部分
select to_date('2019-9-11 21:40:59'); --> 2019-09-11
select to_date('2019/9/11 21:40:59'); --> null
4.datediff();
datediff(date1, date2) - Returns the number of days between date1 and date2
返回date1和date2之间的天数
select datediff('2018-09-11','2019-09-11'); --> -365
select datediff('2018','2019'); --> null
5.date_sub();
date_sub(start_date, num_days) - Returns the date that is num_days before start_date.
返回start_date往前num_days(int型)的日期。
select date_sub('2019-09-11',100); --> 2019-06-03
6.date_add();
date_add(start_date, num_days) - Returns the date that is num_days after start_date.
返回start_date往后num_days(int型)的日期。
select date_add('2019-09-11',100); --> 2019-12-20
7.last_day();
last_day(date) - Returns the last day of the month which the date belongs to.
返回日期所属月份的最后一天。
select last_day('2019-10-1'); --> 2019-10-31
select last_day('2019-10'); --> null;
8.next_day();
next_day(start_date, day_of_week) - Returns the first date which is later than start_date and named as indicated.
返回start_date下个星期几的日期
select next_day('2019-09-11','MON'); --> 2019-09-16
9.current_date:
奇怪的是desc function current_date;报错
//之前的一堆at...就不写了
FAILED: ParseException line 1:14 cannot recognize input near 'current_date' '''' '<EOF>' in function identifier
用法
select current_date; 2019-09-11 返回当前日期
select current_date(); 2019-09-11 返回当前日期
10.current_timestamp();
desc也查看不了
select current_timestamp(); --> 2019-09-13 16:49:37.264
11.hour();
hour(param) - Returns the hour componemnt of the string/timestamp/interval
返回字符串/时间戳/间隔的小时组件
select hour('2019-09-13 16:49:37.264'); --> 16
12.minute();
minute(param) - Returns the minute component of the string/timestamp/interval
返回字符串/时间戳/间隔的分钟组件
select minute('2019-09-13 16:59:12'); --> 59
二.数学函数
1.rand();
rand([seed]) - Returns a pseudorandom number between 0 and 1
返回0到1之间的伪随机数
hive> select rand(),rand(),rand(100),rand(100);
OK
0.27745004825516917 0.8217498985868743 0.72200965485964340.7220096548596434
可以看到如果指定了参数,得到的伪随机数是一样的
2.round();
round(x[, d]) - round x to d decimal places
舍入x到d小数位
hive> select round(1.233,1),round(1.25,1);
OK
1.2 1.3
可以看到是根据四舍五入,保留了第二个参数指定的小数位
3.ceil():
ceil(x) - Find the smallest integer not smaller than x
找到不小于x的最小整数
hive> select ceil(10.233);
OK
11
4.floor();
floor(x) - Find the largest integer not greater than x
找到不大于x的最大整数
hive> select floor(10.233);
OK
10
三.字符函数
1.split();
split(str, regex) - Splits str around occurances that match regex
Splits str匹配正则表达式的匹配项
hive> select split('1,2,3',',');
OK
["1","2","3"]
可以看到会分割成数组
hive> select
> arr[0]
> from(
> select split('1,2,3',',') as arr
> ) t1;
OK
1
2.substr();
substr(str, pos[, len]) - returns the substring of str that starts at pos and is of length len or substr(bin, pos[, len]) - returns the slice of byte array that starts at pos and is of length len
substr(str,pos [,len]) - 返回str的子字符串,该字符串以pos开头,长度为len或substr(bin,pos [,len]) - 返回以pos开头的字节数组的片段 长度len
hive> select substr('areyouok',1,5);
OK
areyo
hive> select substr('areyouok',2,5);
OK
reyou
3.concat();
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
concat(str1,str2,... strN) - 返回str1,str2,... strN或concat(bin1,bin2,... binN)的串联 - 返回二进制数据bin1,bin2,...中的字节串联。 .. binN
hive> select concat('1','2','3');
OK
123
4.concat_ws();
concat_ws(separator, [string | array(string)]+) - returns the concatenation of the strings separated by the separator.
返回由分隔符分隔的字符串的串联。
hive> select concat_ws(',','1','2','3');
OK
1,2,3