Hive:转换“yyyy-MM-dd'T'HH:mm:ss.SSS'Z'”中缺少秒数的字符串日期时间

2024-04-17

我使用以下代码将字符串日期时间变量转换为日期时间,但转换后的字符串缺少 SSS 部分。

使用的代码:

cast(FROM_UNIXTIME(UNIX_TIMESTAMP(oldtime, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),"yyyy-MM-dd HH:mm:ss.SSS") as timestamp) as newtime

结果:

2019-03-08T18:28:36.901Z 转换为 08MAR2019:18:28:36.000000

字符串中的其他一些旧时光:

2020-03-09T16:05:06:827Z
2020-03-09T16:03:19:354Z
2020-03-11T16:03:57:280Z
2020-03-10T16:02:57:642Z
2020-03-10T16:04:07:455Z
2020-03-10T16:04:09:737Z
2020-03-10T16:03:57:280Z
2020-03-10T16:02:46:816Z

转换后的时间中缺少 SSS 部分“901”。我需要保留 SSS 部分的帮助,因为我需要按准确时间对记录进行排序。

谢谢你!


from_unixtime https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF总是直到minutes(yyyy-MM-dd HH:mm:ss) to get millisecs我们需要采取一些解决方法。

  • 我们将提取millisecs从old_time开始使用regexp_extract then concat那个到from_unixtime结果并最终投射到timestamp.

Example:

select old_time,
timestamp(concat_ws(".", --concat_ws with . and cast
FROM_UNIXTIME(UNIX_TIMESTAMP(old_time, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),"yyyy-MM-dd HH:mm:ss"), -- from_unixtime and unix_timestamp to convert without millisecs
regexp_extract(string(old_time),".+\\.(.*)(?i)z",1))) as newtime from --regexp_extract to extract last 3 digits before z then concat
(select string("2020-03-11T21:14:41.335Z")old_time)e

+------------------------+-----------------------+
|old_time                |newtime                |
+------------------------+-----------------------+
|2020-03-11T21:14:41.335Z|2020-03-11 21:14:41.335|
+------------------------+-----------------------+

UPDATE:

您的样本数据有:在毫秒之前,尝试使用以下查询:

select old_time,
    timestamp(concat_ws(".", --concat_ws with . and cast
    FROM_UNIXTIME(UNIX_TIMESTAMP(old_time, "yyyy-MM-dd'T'HH:mm:ss:SSS'Z'"),"yyyy-MM-dd HH:mm:ss"), -- from_unixtime and unix_timestamp to convert without millisecs
    regexp_extract(string(old_time),".+\\:(.*)(?i)z",1))) as newtime from --regexp_extract to extract last 3 digits before z then concat
    (select string("2020-03-11T21:14:41:335Z")old_time)e
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Hive:转换“yyyy-MM-dd'T'HH:mm:ss.SSS'Z'”中缺少秒数的字符串日期时间 的相关文章

随机推荐