我有以下JSON数据:我需要将该日期或mongo_date
转换为UTC时间戳,以便按时间轴在Hive中分析数据,例如按年、按月、按周使用MapReduce。
{
"_id" : ObjectId("51ac77050e9edcdad271ce2d"),
"company" : null,
"date" : "19760224",
"mongo_date" : ISODate("1976-02-24T00:00:00Z")
我有以下JSON数据:我需要将该日期或mongo_date
转换为UTC时间戳,以便按时间轴在Hive中分析数据,例如按年、按月、按周使用MapReduce。
{
"_id" : ObjectId("51ac77050e9edcdad271ce2d"),
"company" : null,
"date" : "19760224",
"mongo_date" : ISODate("1976-02-24T00:00:00Z")
Hive理解这个格式:'yyyy-MM-dd HH:mm:ss.SSS'
。
使用unix_timestamp()
将其转换为从1970-01-01开始经过的秒数,然后使用from_unixtime()
将其转换为正确的格式:
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()
,这将保留毫秒:
select timestamp(regexp_replace("2019-05-17T17:03:09.775Z", '^(.+?)T(.+?)Z$','$1 $2'));
结果:
2019-05-17 17:03:09.775