Hive:如何转换毫秒时间戳?

Hive: how to convert millisecond timestamps?

我正在尝试使用 HIVE UDFs (https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions) 从 Sparklyr 到正确读入一些时间戳。

不幸的是,我无法正确解析以下时间戳:

unix_timestamp('2011-03-01T00:00:04.226Z', 'yyyy-MM-ddThh:mm:ss.SSS' ) 

returns NAs..

有什么想法吗?这里的正确模式是什么? 谢谢!

您需要引用 TZ

hive> select unix_timestamp('2011-03-01T00:00:04.226Z', "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'" );
OK
1298959204

或者如果你不怕笨拙的话,试试这个:

select unix_timestamp(cast(regexp_replace('2011-03-01T00:00:04.226Z', '(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).(\d{3})Z', '-- ::.' ) as timestamp))

要从 EST 转换为 UTC,请使用以下命令:

hive> select to_utc_timestamp(unix_timestamp('2011-03-01T00:00:04.226Z', "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'" )*1000, 'EST');
OK
2011-03-01 05:00:04

需要与 1000 相乘,因为从 Hive Language Manual:

Fractional values are considered as seconds. Integer values are considered as milliseconds.. E.g to_utc_timestamp(2592000.0,'PST'), to_utc_timestamp(2592000000,'PST') and to_utc_timestamp(timestamp '1970-01-30 16:00:00','PST') all return the timestamp 1970-01-31 00:00:00