apache-spark - 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 |
+-----------------------------+-------------+--+
解决方案
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)
推荐阅读
- c# - 运行函数
在使用 .Net 的计时器上 - javascript - 有没有办法使用 JavaScript (Vue.js) 将 Java 应用程序(游戏)实现到网站中
- sql - 为每位客户提供多项交易
- html - 在Angular 6项目中绑定到HTML时页面不会加载
- r - 如何使用 Highcharter 创建两个独立的向下钻取图?
- python - 使用 setup.py 时命名空间损坏并导致 AttributeError: module has no attribute
- kubernetes-helm - 在 k3s 中初始化 helm 时出错 - “服务器找不到请求的资源”
- mysql - MySQL - 如何根据行的值多次获取一行?
- arrays - 如何在 Watson Assistant 中更改上下文值
- python - 需要 Python 字典循环帮助