2013-07-22 16 views
5

Ho la seguente rappresentazione di stringa di un timestamp in mia tabella Hive:stringa Converti in timestamp Hive

20130502081559999 

ho bisogno di convertirlo in una stringa in questo modo:

2013-05-02 08:15:59 

ho provato seguente ({codice} >>> {risultato}):

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 

Conversione in un timestamp e poi unixtime sembra strano, quello che è il puntello è il modo di fare questo?

EDIT L'ho capito.

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

o

from_unixtime(unix_timestamp('20130502081559999', 'yyyyMMddHHmmssSSS')) >>> 2013-05-02 08:15:59 

Ancora ... c'è un modo migliore?

risposta

3

Non sai cosa intendi per "modo migliore" ma puoi sempre write your own function per gestire la conversione della data.

6

Sembra che il tuo formato abbia tre millisecondi di cifre. Direi che, in base alla SimpleDateFormat, si avrebbe bisogno di utilizzare il seguente:

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

Speranza che aiuta.

+0

Il tuo codice sembra lasciar cadere i nanosecondi, c'è un modo per preservarlo –

2

Supponiamo di avere file di input come questo 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 

Poi si può anche provare questo approccio:

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; 
+0

IMHO la migliore risposta qui – mishkin