hive的时间处理函数

2019-11-25  本文已影响0人  不分享的知识毫无意义

由于时间在hive里一般都是按照str存储的,有多重格式,比如时间戳格式或者标准的时间格式,有很多函数可以用于时间格式的换算。

1.unix时间格式转换

1.1 一些函数

select unix_timestamp()

unix_timestamp()可以把日期转换为unix格式,标准用法为 unix_timestamp(string date,string pattern)

select unix_timestamp('20111207 13:01:03','yyyyMMddHH:mm:ss')
输出:1323234063
select from_unixtime(1323234063)
输出:2011-12-07 13:01:03

1.2 unix时间戳和标准时间的换算方法

unix字符串相减得到的是秒,要进行转化的话:

2.日期操作函数

hive> select date_sub('2019-11-25',1);
OK
2019-11-24
select date_add('2019-11-25',1);
OK
2019-11-26
select datediff('2019-11-25','2019-11-24');
返回值:1

3.日期截取函数

select to_date('2019-11-25 17:47:08.971')
- 输出:2019-11-25
select year('2019-11-25 17:47:08.971')
- 输出:2019
select month('2019-11-25 17:47:08.971')
-输出:11
select day('2019-11-25 17:47:08.971')
-输出:25
select weekofyear('2019-11-25 17:47:08.971')
-输出:48
上一篇 下一篇

猜你喜欢

热点阅读