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



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

如果您能在蜂巢中找到准确的解决方案,我们将不胜感激。感谢

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

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

最新更新