How do I get millisecond precision in hive?

前端 未结 1 1423
南旧
南旧 2020-12-16 05:23

The documentation says that timestamps support the following conversion:

•Floating point numeric types: Interpreted as UNIX timestamp in seconds with decimal precisi

相关标签:
1条回答
  • 2020-12-16 05:44

    If you want to work with milliseconds, don't use the unix timestamp functions because these consider date as seconds since epoch.

    hive> describe function extended unix_timestamp;
    unix_timestamp([date[, pattern]]) - Returns the UNIX timestamp
    Converts the current or specified time to number of seconds since 1970-01-01.
    

    Instead, convert the JDBC compliant timestamp to double.
    E.g:

    Given a tab delimited data:

    cat /user/hive/ts/data.txt :
    a   2013-01-01 12:00:00.423   2013-01-01 12:00:00.433
    b   2013-01-01 12:00:00.423   2013-01-01 12:00:00.733
    
    CREATE EXTERNAL TABLE ts (txt string, st Timestamp, et Timestamp) 
    ROW FORMAT DELIMITED
    FIELDS TERMINATED BY '\t'
    LOCATION '/user/hive/ts';
    

    Then you may query the difference between startTime(st) and endTime(et) in milliseconds as follows:

    select 
      txt, 
      cast(
        round(
          cast((e-s) as double) * 1000
        ) as int
      ) latency 
    from (select txt, cast(st as double) s, cast(et as double) e from ts) q;
    
    0 讨论(0)
提交回复
热议问题