Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to timestamp in 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

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?

EDIT I 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?

like image 740
ferics2 Avatar asked Jul 22 '13 19:07

ferics2


3 Answers

Looks like your format has three millisecond digits. I'd guess that, according to the SimpleDateFormat, you would need to use the following:

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

Hope that helps.

like image 117
Lukas Vermeer Avatar answered Oct 07 '22 12:10

Lukas Vermeer


Not sure what you mean by "better way" but you can always write your own function to handle the date conversion.

like image 40
Paul M Avatar answered Oct 07 '22 12:10

Paul M


Suppose you have input file like this

file:///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

Then you can also try this approach:

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;
like image 20
Mahendra Avatar answered Oct 07 '22 12:10

Mahendra