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

时间:2013-07-22 19:44:14

标签: hadoop hive hiveql

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

20130502081559999

我需要将它转换为如此字符串:

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

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

4 个答案:

答案 0 :(得分:8)

看起来您的格式有三毫秒的数字。我想,根据SimpleDateFormat,您需要使用以下内容:

from_unixtime(unix_timestamp('20130502081559999', 'yyyyMMddHHmmssSSS'))

希望有所帮助。

答案 1 :(得分:3)

不确定“更好的方式”是什么意思,但您始终可以write your own function来处理日期转换。

答案 2 :(得分:2)

假设您有这样的输入文件

文件:///data/csv/temptable/temp.csv

1   2015-01-01  
2   2015-10-10 12:00:00.232
3   2016-02-02
4   2015-09-12 23:08:07.124

然后你也可以尝试这种方法:

create external table temptable(id string, datetime string) row format delimited fields terminated by '\t' stored as textfile LOCATION 'file:///data/csv/temptable';

create table mytime as select id, from_utc_timestamp(date_format(datetime,'yyyy-MM-dd HH:mm:ss.SSS'),'UTC') as datetime from temptable;

答案 3 :(得分:0)

如果可用,您只需使用以下语法

1)检查您的蜂巢安装中是否提供了哪些UDF?

show functions;

2)如果从from_unixtime()函数看到,则:

from_unixtime(your_timestamp_field)

这将解决问题!

如果您喜欢我的回答,请添加评论!