gpt4 book ai didi

hadoop - 如何在配置单元中获得毫秒精度?

转载 作者:可可西里 更新时间:2023-11-01 14:15:58 25 4
gpt4 key购买 nike

documentation表示时间戳支持以下转换:

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

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

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

select maxts - mints as latency from mytable

其中 maxtsmints 是时间戳列。目前,这给了我 NullPointerException 使用 Hive 0.11.0。如果我做类似的事情,我就可以执行查询

select unix_timestamp(maxts) - unix_timestamp(mints) as latency from mytable

但这只适用于秒,不适用于毫秒精度。

感谢任何帮助。如果您需要更多信息,请告诉我。

最佳答案

如果您想使用毫秒,请不要使用 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 compliant timestamp加倍。
例如:

给定制表符分隔的数据:

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;

关于hadoop - 如何在配置单元中获得毫秒精度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18737561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com