如何获取当前时间戳为2020-04-17T19:17:56.017719Z格式

问题描述 投票:2回答:1

我正在使用Hive,并且手头有一项重要任务,需要使用2020-04-17T19:17:56.017719Z格式的当前时间戳。

在Hive中为此解决方案提供的任何帮助,都将受到高度赞赏。谢谢

hive timestamp format hiveql utc
1个回答
1
投票
with time as (
select reflect('java.util.Date','getTime') as millis
)
select concat( from_unixtime(floor(millis / 1000   ),"yyyy-MM-dd'T'HH:mm:ss"), '.',cast((millis % 1000)as int),'Z')
from time

结果:

2020-04-26T11:12:35.590Z

或另一种方法

select concat(from_unixtime(unix_timestamp(split(current_timestamp,'\\.')[0]),"yyyy-MM-dd'T'HH:mm:ss"),'.',split(current_timestamp,'\\.')[1],'Z') 

结果:

2020-04-26T11:28:13.433Z

使用regexp_replace的另一种方法:

select regexp_replace(current_timestamp,'(.*) (.*)','$1T$2Z')

结果:

2020-04-26T11:50:51.804Z

如果您需要在Hive中获得微秒,那么肮脏的把戏是

with time as (
select reflect('java.util.Date','getTime') as millis, reflect('java.lang.System','nanoTime') as nano 

)
select concat( from_unixtime(floor(millis / 1000   ),"yyyy-MM-dd'T'HH:mm:ss"), '.',cast(millis%1000 as int),cast(nano%1000 as int),'Z')
from time

结果:

2020-04-26T21:53:31.261356Z

但是这不是真正的微秒精度。

© www.soinside.com 2019 - 2024. All rights reserved.