将字符串转换为Hive中的时间戳

将字符串转换为Hive中的时间戳

本文介绍了将字符串转换为Hive中的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Hive表中有一个时间戳的字符串表示形式:

  20130502081559999 

$ b

我需要将它转换为如下所示的字符串:

  2013-05-02 08:15:59 

我试过以下{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看起来很奇怪,是这样做的正确方法吗?



编辑
我想明白了。

  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 

仍然...有更好的方法吗?

解决方案

不确定您的意思是更好的方式,但您始终可以来处理日期转换。


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

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

Converting to a timestamp and then unixtime seems weird, what is the proper way to do this?

EDITI figured it out.

from_unixtime(unix_timestamp(substr('20130502081559999',1,14), 'yyyyMMddHHmmss')) >>> 2013-05-02 08:15:59

or

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中的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 08:49