代码之家  ›  专栏  ›  技术社区  ›  nak5120

将presto中的列从epoch转换为date[重复]

  •  0
  • nak5120  · 技术社区  · 7 年前

    几天来我一直没找到这个。

    如果配置单元表中数据的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)。

    0 回复  |  直到 8 年前
        1
  •  3
  •   Piotr Findeisen    8 年前

    我没有找到从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)
    

    (无可否认很麻烦,但很管用)

        2
  •  2
  •   Rajiv Singh    7 年前

    从表名限制10的“unixtime(cast(event_time as bigint)/1000000)+parse_duration(cast(event_time as bigint)%1000)as varchar)||‘ms’”中选择;

    推荐文章