蜂巢:如何转换毫秒时间戳?



我正在尝试使用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' ) 

返回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', '$1-$2-$3 $4:$5:$6.$7' ) 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 语言手册:

小数值被视为秒。整数值被视为毫秒。例如to_utc_timestamp(2592000.0,'PST')、to_utc_timestamp(2592000000,'PST')和to_utc_timestamp(时间戳'1970-01-30 16:00:00','PST')都返回时间戳 1970-01-31 00:00:00

最新更新