如何在蜂巢中获得毫秒精度?

Uma*_*maN 6 hadoop timestamp hive hiveql

文件说,时间戳支持以下转换:

•浮点数字类型:以小数精度解释为UNIX时间戳,以秒为单位

首先,我不确定如何解释这一点.如果我有时间戳2013-01-01 12:00:00.423,我可以将其转换为保留毫秒数的数字类型吗?因为那就是我想要的.

更一般地说,我需要在诸如的时间戳之间进行比较

select maxts - mints as latency from mytable
Run Code Online (Sandbox Code Playgroud)

其中maxts薄荷糖是时间戳列.目前,这让我NullPointerException使用Hive 0.11.0.如果我做的话,我可以执行查询

select unix_timestamp(maxts) - unix_timestamp(mints) as latency from mytable
Run Code Online (Sandbox Code Playgroud)

但这仅适用于秒,而不是毫秒精度.

任何帮助赞赏.如果您需要其他信息,请告诉我.

Lor*_*dig 11

如果您想使用毫秒,请不要使用unix时间戳功能,因为这些功能将日期视为自纪元以来的秒数.

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.
Run Code Online (Sandbox Code Playgroud)

而是将JDBC兼容时间戳转换为double.
例如:

给定制表符分隔的数据:

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';
Run Code Online (Sandbox Code Playgroud)

然后你可以用毫秒来查询startTime(st)和endTime(et)之间的区别,如下所示:

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;
Run Code Online (Sandbox Code Playgroud)