hive中字符串和日期的转换常用的函数大全

获取本地时间:

用到from_unixtime和unix_timestamp两种函数:

from_unixtime:时间戳转日期函数

用法:from_unixtime(bigint unixtime[, stringformat])

返回值: string

例子1:获取当前Linux的系统日期

substr(from_unixtime(unix_timestamp()),1,10)

结果为:2017-01-03

例子2:

--20180905转成2018-09-05
select from_unixtime(unix_timestamp('20180905','yyyymmdd'),'yyyy-mm-dd')
from dw.ceshi_data

--结果如下:
2018-09-05
 
--2018-09-05转成20180905
select from_unixtime(unix_timestamp('2018-09-05','yyyy-mm-dd'),'yyyymmdd')
from dw.ceshi_data

--结果如下:
20180905

unix_timestamp:日期转时间戳函数

用法:unix_timestamp(string date) 

注意:里面格式必须是yyyy-MM-dd HH:mm:ss,如果不是会返回null值

返回值:   bigint

例子:select unix_timestamp('2017-09-05 12:01:03') 
  from dw.ceshi_data;

结果如下:
1536120063
 
--获取当前日期的时间戳:
select unix_timestamp() 
  from dw.ceshi_data;

结果如下:
1536126324

字符串的转换:

hive表中,存放着无法直接识别的字符串格式的时间,如'20170728102031',要计算两个时间相差的秒数。

解决方法:

1、先将字符串调整为hive可以识别的格式,即将形如'20170728102031' 转成 '2017-07-28 10:20:31'。 因为hive的 regexp_replace 不支持子语句,没法一次转换,只能用万能的 substr 和拼接函数来写了

select concat(substr('20170728102031',1,4),'-',

substr('20170728102031',5,2),'-',

substr('20170728102031',7,2),' ',

substr('20170728102031',9,2),':',

substr('20170728102031',11,2),':',

substr('20170728102031',13,2))

 

2、用 unix_timestamp 转成unix时间戳,然后计算两个日期相差秒数 

 

 

select unix_timestamp(concat(substr('20170728102031',1,4),'-',substr('20170728102031',5,2),'-',

substr('20170728102031',7,2),' ',

substr('20170728102031',9,2),':',

substr('20170728102031',11,2),':',

substr('20170728102031',13,2))) - unix_timestamp(concat(substr('20170728112031',1,4),'-',

substr('20170728112031',5,2),'-',

substr('20170728112031',7,2),' ',

substr('20170728112031',9,2),':',

substr('20170728112031',11,2),':',

substr('20170728112031',13,2)))

datediff的用法:

hive > select create_time ,datediff(from_unixtime(unix_timestamp(),’yyyy-MM-dd HH:mm:ss’), create_time) from test;

结果就是:当前时间是2017-11-16 与create_time的11-10之间差了6天,输出6;

 hive >select datediff(’2012-12-08′,’2012-05-09′) from dual; 
213

to_date的用法:

日期时间转日期函数: to_date语法: to_date(string timestamp) 返回:string 
hive> select to_date(’2011-12-08 10:03:01′) from dual; 
2011-12-08

year语法:

日期转年函数: year语法: year(string date)

hive> select year(’2011-12-08 10:03:01′) from dual;

2011

以下可以取到 month,hour,minute,second 用法和上面的一样

weekofyear语法:

日期转周函数: weekofyear语法: weekofyear (string date) 返回值: int 说明: 返回日期在当前的周数。

举例: hive> select weekofyear(’2011-12-08 10:03:01′) from dual;

49

date_add用法:

日期增加函数: date_add语法:   date_add(string startdate, int days)
返回值: string
说明: 返回开始日期startdate增加days天后的日期
举例:
hive> select date_add(’2012-12-08′,10) from dual;
2012-12-18

date_sub用法:
日期减少函数: date_sub语法: date_sub (string startdate, int days) 
返回值: string 
说明: 返回开始日期startdate减少days天后的日期。 
举例: 
hive> select date_sub(’2012-12-08′,10) from dual; 
2012-11-28


 所以我们利用其中的hour和datediff来获取create_time与当前时间的小时差:
hive> select create_time,

(hour(from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss'))-

hour(create_time)+(datediff(from_unixtime(unix_timestamp(),

'yyyy-MM-dd HH:mm:ss'), create_time))*24) as hour_dValue

from test;
   2017-11-10 09:43:35   145
 

  • 13
    点赞
  • 121
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值