标签:arc cat tab substr amp 单元 hive rip 日期
数据:2020-07-23 15:01:13
Presto:select to_unixtime(cast(‘2020-07-23 15:01:13‘ as timestamp))
Hive:select unix_timestamp(cast(‘2020-07-23 15:01:13‘ as timestamp))
数据:2020-07-23 15:01:13.343
Presto:select to_unixtime(cast(‘2020-07-23 15:01:13.343‘ as timestamp))*1000
Hive:select unix_timestamp(cast(substr(‘2020-07-23 15:01:13.343‘, 1, 19) as timestamp)) * 1000 + cast(substr(‘2020-07-23 15:01:13.343‘, 21) as bigint)
数据:1595487673
Presto:select format_datetime(from_unixtime(1595487673),‘yyyy-MM-dd HH:mm:ss‘)
Hive:select from_unixtime(1595487673,‘yyyy-MM-dd HH:mm:ss‘)
数据:1595487673343
Presto:select concat(format_datetime(from_unixtime(1595487673343/1000),‘yyyy-MM-dd HH:mm:ss.‘), cast(1595487673343%1000 as varchar))
Hive:select concat(from_unixtime(cast(1595487673343/1000 as int),‘yyyy-MM-dd HH:mm:ss.‘), cast(1595487673343%1000 as string))
数据:2020-07-24 11:42:58 - 2020-07-23 15:01:13
Presto:select date_diff(‘day‘, cast(‘2020-07-23 15:01:13‘ as timestamp), cast(‘2020-07-24 11:42:58‘ as timestamp))
Hive:select datediff(‘2020-07-24 11:42:58‘,‘2020-07-23 15:01:13‘);
这个数据,因为相差的时间小于24小时,Presto输出的是0,而Hive是1,这个坑要注意一下。还有要注意的就是Presto是时间大的放后面,而Hive是时间大的放前面。
数据:2020-07-24 11:42:58 + 1
Presto:select date_add(‘day‘, 1, cast(‘2020-07-24 11:42:58‘ as timestamp))
Hive:select date_add(‘2020-07-24 11:42:58‘, 1)
如果要计算相差的其他时间单位,Presto是修改前面的时间单元即可,可选有如下几个:
Unit | Description |
---|---|
millisecond | Milliseconds |
second | Seconds |
minute | Minutes |
hour | Hours |
day | Days |
week | Weeks |
month | Months |
quarter | Quarters of a year |
year | Years |
而Hive是通过对应的时间单元函数获取到时间单元后在进行计算,例如上面的例子2020-07-24 11:42:58 - 2020-07-23 15:01:13
,我要计算他们的小时差,那么我可以这么写:
select hour(‘2020-07-24 11:42:58‘) - hour(‘2020-07-23 15:01:13‘) + datediff(‘2020-07-24 11:42:58‘,‘2020-07-23 15:01:13‘)*24
标签:arc cat tab substr amp 单元 hive rip 日期
原文地址:https://www.cnblogs.com/harrylyx/p/13371792.html