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

全部评论

相关推荐

2025-12-12 19:01
南京航空航天大学 C++
秋招没咋投,准备&nbsp;wxg&nbsp;转正之后摆烂了。结果不堪字节&nbsp;HR&nbsp;的骚扰还是面了一下字节。之前想去字节的时候怎么面都挂。现在想着随便面一下结果三面技术面都意外顺利还有加面。十月中旬字节发了意向,wxg&nbsp;转正结果无响应。十月底字节拉了保温群,wxg&nbsp;口头通过,系统显示考核中。十一月初和字节&nbsp;ld&nbsp;交流之后得知&nbsp;base&nbsp;居然能选海外,甚至能小&nbsp;wlb&nbsp;一下,wxg&nbsp;无响应无人联系。十一月中旬把字节&nbsp;base&nbsp;转到了海外,wxg&nbsp;流程灰了,一问超时忘处理了,过两天又变考核中了。十一月下旬字节换了海外&nbsp;HR&nbsp;对接,问了期望薪资,wxg&nbsp;考核终于显示通过,无&nbsp;HR&nbsp;保温,无其他保温。十一月底给字节报了个天价,想吓吓他们,同时告诉微信字节要开了,微信无响应。同样十一月底字节&nbsp;HR&nbsp;告诉我确实给不到那么高,但是能拿期权补上,问能不能接受。微信无响应。同样十一月底字节&nbsp;HR&nbsp;告知了具体方案,符合预期。&nbsp;微信无响应。十二月上旬催&nbsp;wxg&nbsp;不开我就盲拒了,wxg&nbsp;HR&nbsp;火急火燎的打电话问情况,问期望。我给了一个不算夸张的总包数字,因为今年市场在涨,过了三天还不联系我,我再催,约时间下午打电话,非得在我给出的数字上压下去几万,微信又不差这点,为什么不能满足我,让我没有拒绝的理由呢?一番纠结抗争,求稳还是追求挑战,最终选择接受迎接新的挑战,因为堂吉诃德永远不会停下脚步!回想起来,在&nbsp;wxg&nbsp;谈薪的阶段,我认为并没有给予我一定的重视,即使&nbsp;HR&nbsp;表示我在实习期间的表现和之前的面评都很靠前。也没有感觉到想要争取我,虽然我表示拒了&nbsp;offer&nbsp;之后要给我加面委定&nbsp;t6&nbsp;再涨,但我三个月没面试让我面面委那就是白给,还是算了。有缘再见了我亲爱的&nbsp;wxg,再见了曾经的梦中情厂,再见亲爱的&nbsp;mt,再见亲爱的朋友们。也再见,北京的一切。我想润了。秋招结束,卸载牛客,下一个三年,下一个五年,下一个十年后再来看看。
面试中的大熊猫爱吃薯...:我嫉妒得狗眼通红
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务