hive載入json資料解決方案

來源:互聯網
上載者:User

標籤:分區表   log   預設   from   抽取   指令碼   oca   ted   pac   

hive官方並不支援json格式的資料載入,預設支援csv格式檔案載入,如何在不依賴外部jar包的情況下實現json資料格式解析,本編部落格著重介紹此問題解決方案

首先建立中繼資料表:

create EXTERNAL table access_log (content string) row format delimited fields terminated by ‘\t‘ STORED AS INPUTFORMAT ‘com.hadoop.mapred.DeprecatedLzoTextInputFormat‘ OUTPUTFORMAT ‘org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat‘ location ‘hdfs://sps1:9090/data/accesslog‘

建立視圖表:

create view access_log_view as select eventTime, ip, appName, fp, username, target from access_log lateral view json_tuple(content, "eventTime", "ip", "appName", "fp", "username", "target") t1 as eventTime, ip, appName, fp, username, target;

視圖表利用json tuple將json object的資料進行抽取,這樣就實現了欄位分離。

但是有些記錄檔是/user/aaa/dt=2013-12-01/ds=01/access.log帶有分區目錄的,對於這種格式需要分區表的支援

建立分區表:

create EXTERNAL table access_log (content string) partitioned by (dt int, ds int) row format delimited fields terminated by ‘\t‘ STORED AS INPUTFORMAT ‘com.hadoop.mapred.DeprecatedLzoTextInputFormat‘ OUTPUTFORMAT ‘org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat‘ location ‘hdfs://sps1:9090/data/accesslog4‘;

但是問題來了,發現沒有辦法載入資料,該怎麼辦那。

下一步我們需要手動的載入分區:

alter table access_log add partition(dt=?,ds=?)

這樣就可以查到資料了。切記必須要進行分區add,否則無法查到資料。

建立視圖表:

與上邊建立視圖一樣

但是分區是隨著時間的推移進行增加的,這個不能人肉,我們需要自動化指令碼來協助我們完成

#!/bin/bashsource ~/.bashrcdate=`date +%Y-%m-%d`hour=`date +%H`cmd="ALTER TABLE databaseName.tableName ADD PARTITION(dt=‘$date‘, ht=‘$hour‘);"hive -e "$cmd"

至此為止,有關hive載入json資料和分區表的問題就解釋清楚了,不明白下方留言,我們繼續討論。

hive載入json資料解決方案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.