2013-07-22 44 views
5
時間戳

我有我的蜂巢表時間戳以下字符串表示:字符串轉換爲在蜂巢

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 

還是......有沒有更好的辦法?

回答

3

不確定您的意思是「更好的方式」,但您始終可以使用write your own function來處理日期轉換。

6

看起來你的格式有三毫秒數字。我猜,根據SimpleDateFormat,你就需要使用以下內容:

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

希望有所幫助。

+0

你的代碼似乎下降了納秒,是否有辦法保存它 –

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

恕我直言,最好的答案在這裏 – mishkin