在 Hive 中将字符串转换为时间戳 [英] Convert string to timestamp in Hive
本文介绍了在 Hive 中将字符串转换为时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 Hive 表中有时间戳的以下字符串表示形式:
I have the following string representation of a timestamp in my Hive table:
20130502081559999
我需要将其转换为这样的字符串:
I need to convert it to a string like so:
2013-05-02 08:15:59
我尝试了以下({code} >>> {result}):
I have tried following ({code} >>> {result}):
from_unixtime(unix_timestamp('20130502081559999', 'yyyyMMddHHmmss')) >>> 2013-05-03 00:54:59
from_unixtime(unix_timestamp('20130502081559999', 'yyyyMMddHHmmssMS')) >>> 2013-09-02 08:15:59
from_unixtime(unix_timestamp('20130502081559999', 'yyyyMMddHHmmssMS')) >>> 2013-05-02 08:10:39
转换为时间戳,然后转换为 unixtime 似乎很奇怪,这样做的正确方法是什么?
Converting to a timestamp and then unixtime seems weird, what is the proper way to do this?
编辑我想通了.
from_unixtime(unix_timestamp(substr('20130502081559999',1,14), 'yyyyMMddHHmmss')) >>> 2013-05-02 08:15:59
或
from_unixtime(unix_timestamp('20130502081559999', 'yyyyMMddHHmmssSSS')) >>> 2013-05-02 08:15:59
还是...有更好的方法吗?
Still... Is there a better way?
推荐答案
不知道你说的更好的方式"是什么意思,但你总是可以编写自己的函数来处理日期转换.
Not sure what you mean by "better way" but you can always write your own function to handle the date conversion.
这篇关于在 Hive 中将字符串转换为时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文