首页 > 解决方案 > Hive unix_timestamp 在源列中无法工作毫秒

问题描述

我正在使用 Hive 1.1

a_ingestion_dtm 中的最后六位数字表示毫秒。

但是如果我也指定模式,unix_timestamp hive 函数会给我相同的结果

那么我如何获得包含毫秒的 unix_timestamp 的结果

 select a_ingestion_dtm,unix_timestamp(a_ingestion_dtm) from omega limit 10;
 +-----------------------------+-------------+--+
 |       a_ingestion_dtm       |     _c1     |
 +-----------------------------+-------------+--+
 | 2019-07-08 16:11:02.076002  | 1562616662  |
 | 2019-07-08 21:07:26.253007  | 1562634446  |
 | 2019-07-08 21:07:14.284     | 1562634434  |
 +-----------------------------+-------------+--+


 select a_ingestion_dtm,unix_timestamp(a_ingestion_dtm,'yyyy-MM-dd HH:mm:ss.SSSSSS') from omega limit 10;

 +-----------------------------+-------------+--+
 |       a_ingestion_dtm       |     _c1     |
 +-----------------------------+-------------+--+
 | 2019-07-08 16:11:02.076002  | 1562616662  |
 | 2019-07-08 21:07:26.253007  | 1562634446  |
 | 2019-07-08 21:07:14.284     | 1562634434  |
 +-----------------------------+-------------+--+

标签: apache-sparkhadoophivetimestampunix-timestamp

解决方案


unix_timestamp(string date) 返回从 1970 开始的秒数

您可以将毫秒部分连接到 unix_timestamp 返回的 bigint。

就像在这个演示中一样:

with your_data as (
select stack(3,
'2019-07-08 16:11:02.076002',
'2019-07-08 21:07:26.253007',
'2019-07-08 21:07:14.284'
) as ts
)

select concat_ws('.',cast(unix_timestamp(ts) as string),regexp_extract(ts,'\\.(\\d+)$')) 
  from your_data;

结果:

OK
1562627462.076002
1562645246.253007
1562645234.284
Time taken: 0.057 seconds, Fetched: 3 row(s)

推荐阅读