我有一個看起來像這樣的檔案:
33.49.147.163 20140416123526 https://news.google.com/topstories?hl=en-US&gl=US&ceid=US:en 29 409 Firefox/5.0
我想將它加載到蜂巢表中。我這樣做:
create external table Logs (
ip string,
ts timestamp,
request string,
page_size smallint,
status_code smallint,
info string
)
row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe'
with serdeproperties (
"timestamp.formats" = "yyyyMMddHHmmss",
"input.regex" = '^(\\S*)\\t{3}(\\d{14})\\t(\\S*)\\t(\\S*)\\t(\\S*)\\t(\\S*).*$'
)
stored as textfile
location '/data/user_logs/user_logs_M';
和
select * from Logs limit 10;
結果是
33.49.147.16 NULL https://news.google.com/topstories?hl=en-US&gl=US&ceid=US:en 29 409 Firefox/5.0
如何正確決議時間戳,以避免這種空值?
uj5u.com熱心網友回復:
"timestamp.formats"SerDe 屬性僅適用于 LazySimpleSerDe ( STORED AS TEXTFILE),不適用于RegexSerDe. 如果您使用 RegexSerDe,則在查詢中決議時間戳。
在 CREATE TABLE 和查詢中將ts列定義為STRING資料型別,如下所示:
select timestamp(regexp_replace(ts,'(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})','$1-$2-$3 $4:$5:$6.0')) as ts
當然,您可以使用 SerDe 作為單獨的列提取時間戳的每個部分,并在查詢中將它們與分隔符正確連接以獲得正確的時間戳格式,但這不會給您任何改進,因為無論如何您都需要在查詢中進行額外的轉換。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/380084.html
