6
4

1 回答 1

12

如果您想使用毫秒,请不要使用 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.

相反,将符合JDBC 的时间戳转换为双精度。
例如:

给定一个制表符分隔的数据:

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';

然后您可以查询 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;
于 2013-09-11T22:59:32.243 回答