如何将存储为 bigint 的 Java 时间戳转换为 Presto 中的时间戳?
How to convert Java timestamp stored as bigint to timestamp in Presto?
几天来我一直没能找到这个。
如果我在配置单元 table 中的数据 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
他们还更改了 presto 的时间戳转换,以始终假定源以秒为单位。
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 的其他人知道如何正确表达转换。 (顺便说一句,我无法重新启动 Presto 或 Hive 服务器以将时区强制转换为 UTC)。
我没有找到从 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)
(诚然很麻烦,但有效)
select from_unixtime(cast(event_time 作为 bigint) / 1000000) + parse_duration(cast((cast(event_time 作为 bigint) % 1000) 作为 varchar) || 'ms') 来自 TableName 限制 10;
几天来我一直没能找到这个。
如果我在配置单元 table 中的数据 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
他们还更改了 presto 的时间戳转换,以始终假定源以秒为单位。 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 的其他人知道如何正确表达转换。 (顺便说一句,我无法重新启动 Presto 或 Hive 服务器以将时区强制转换为 UTC)。
我没有找到从 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)
(诚然很麻烦,但有效)
select from_unixtime(cast(event_time 作为 bigint) / 1000000) + parse_duration(cast((cast(event_time 作为 bigint) % 1000) 作为 varchar) || 'ms') 来自 TableName 限制 10;