2017-06-14 114 views
2

我已經有幾天沒有找到這個運氣了。如何將存儲爲bigint的Java時間戳轉換爲Presto中的時間戳?

如果我在一個蜂巢表數據的Avro模式是:

{ 
    "type" : "record", 
    "name" : "messages", 
    "namespace" : "com.company.messages", 
    "fields" : [ { 
    "name" : "timeStamp", 
    "type" : "long", 
    "logicalType" : "timestamp-millis" 
    }, { 
    … 

,我使用Presto查詢這個,我沒有得到格式化的時間戳。

select "timestamp", typeof("timestamp") as type, 
current_timestamp as "current_timestamp", typeof(current_timestamp) as current_type 
from db.messages limit 1 
timestamp  type current_timestamp     current_type 
1497210701839 bigint 2017-06-14 09:32:43.098 Asia/Seoul timestamp with time zone 

我認爲這將是一個不是問題的問題,然後把它們轉換成精確到毫秒的時間戳,但我發現我沒有明確的方式來做到這一點。

select cast("timestamp" as timestamp) from db.messages limit 1 
line 1:16: Cannot cast bigint to timestamp 

此外,他們已經改變了急的時間戳鑄造總是假設源以秒爲單位。 https://issues.apache.org/jira/browse/HIVE-3454

所以,如果我用from_unixtime()我不得不砍掉毫秒,否則它給了我一個很遙遠的日期:

select from_unixtime("timestamp") as "timestamp" from db.messages limit 1 
timestamp 
+49414-08-06 07:15:35.000 

當然別人誰與普雷斯托工作更通常知道如何正確表達轉換。 (我無法重新啓動Presto或Hive服務器,以強制時區爲UTC)。

回答

2

我沒有找到從Java時間戳時間戳(自1970年以來的毫秒數)直接轉換,但可以用to_unixtime進行,並加入毫秒爲單位的時間間隔:

presto> with t as (select cast('1497435766032' as bigint) a) 
    -> select from_unixtime(a/1000) + parse_duration(cast((a % 1000) as varchar) || 'ms') from t; 
      _col0   
------------------------- 
2017-06-14 12:22:46.032 
(1 row) 

(固然繁瑣,但作品)

相關問題