2011-08-26 37 views
8

我有一個包含時間戳列的日誌文件。時間戳採用unix紀元時間格式。如何在配置單元中將unix時元日期字符串轉換爲

我想根據分區年,月,日的時間戳創建分區。

到目前爲止,我已經做了這個,但它是拋出一個錯誤。

PARSE ERROR cannot recognize input '(' in column type 

這是我的代碼。

from (
     from raw_data 
      MAP ${PREFIX}raw_data.line 
      USING 's3://scripts/clean.py' 
      AS (timestamp STRING, name STRING) 
    ) map_out 
INSERT OVERWRITE TABLE date_base_data_temp PARTITION(year(timestamp), month(timestamp)), day(timestamp))) 
    select map_out.name; 

回答

24

這樣看起來很醜。嘗試在蜂巢使用此功能:

SELECT from_unixtime(unix_timestamp) as new_timestamp from raw_data ... 

或者,如果時間戳爲ms,而不是秒:

SELECT from_unixtime(unix_timestamp DIV 1000) as new_timestamp from raw_data ... 

一個UNIX時間戳轉換爲YYYY-MM-DD HH:MM:SS格式,那麼你可以使用下面的函數來獲取年,月,日:

SELECT year(new_timestamp) as year, month(new_timestamp) as month, day(new_timestamp) as day ... 
+0

謝謝!節省了我很多時間。這正是我所期待的! –

+2

確保'timestamp_value'(這裏'unix_timestamp')在幾秒鐘內,否則使用'from_unixtime(timestamp_value DIV 1000)' – narush

+0

我只有時間到秒,但我也想ms。我怎麼做 ? – Avinash

4

對於較新的蜂巢和SparkSQL,日期和類型轉換選項的數據類型的版本可供選擇。繼應蜂巢工作以及星火SQL

SELECT cast(from_unixtime(epoch_datetime) as date) from myHiveTable 
2

如果您需要在自定義格式的日期轉換,使用此:

select date_format(from_unixtime(epoch_datetime),'yyyMM') as formatted_date from myHiveTable; 


這將返回日期爲yearMonth例如201708

0

添加此查詢到的時間戳需要轉換爲日期字符串YYYY-MM-DD的字符串分區列表:

hive> select date_format(from_unixtime(epoch_datetime), 'yyyy-MM-dd') as day from table_name limit 20; 

-- If required, remove the millis precision for timestamps 
hive> select date_format(from_unixtime(cast(epoch_datetime/1000 as bigint)), 'yyyy-MM-dd') as day from table_name limit 20; 
相關問題