如何在 Hive 中将 ISO 日期转换为 UTC 日期 [英] How to convert ISO Date to UTC date in Hive

查看:38
本文介绍了如何在 Hive 中将 ISO 日期转换为 UTC 日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下 JSON 数据:我需要将该日期或 mongo_date 转换为 utc 时间戳,以每年、每月、每周使用 map reduce 根据时间线示例分析 hive 中的数据

I have JSON data as below: I need to convert that date or mongo_date into utc timestamp, to analyse the data in hive as per timeline example per year, per month, per week using map reduce

{
    "_id" : ObjectId("51ac77050e9edcdad271ce2d"),
    "company" : null,
    "date" : "19760224",
    "mongo_date" : ISODate("1976-02-24T00:00:00Z")

推荐答案

Hive 理解这种格式:'yyyy-MM-dd HH:mm:ss.SSS'.

Hive understands this format: 'yyyy-MM-dd HH:mm:ss.SSS'.

使用 unix_timestamp() 转换为从 1970-01-01 过去的秒数,然后使用 from_unixtime() 转换为正确的格式:

Use unix_timestamp() to convert to seconds passed from 1970-01-01, then use from_unixtime() to convert to proper format:

 select from_unixtime(UNIX_TIMESTAMP("2017-01-01T05:01:10Z", "yyyy-MM-dd'T'HH:mm:ss'Z'"),"yyyy-MM-dd HH:mm:ss"); 

结果:

2017-01-01 05:01:10

更新.这个方法是用regexp_replace去掉Z,用空格替换T,并转换成时间戳如有必要,不使用 unix_timestamp(),这将保留毫秒:

Update. This method is to remove Z and replace T with space using regexp_replace and convert to timestamp if necessary, without using unix_timestamp(), this will preserve milliseconds:

select timestamp(regexp_replace("2019-05-17T17:03:09.775Z", '^(.+?)T(.+?)Z$','$1 $2'));

结果:

2019-05-17 17:03:09.775

这篇关于如何在 Hive 中将 ISO 日期转换为 UTC 日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆