HIVE json存儲-非法數據處理

存儲

1、將json數據以string 方式存儲在hive 中,然後比如使用LATERAL VIEW json_tuple的方法,獲取所需要的列名,或者 get_json_object(json,’$.id’) 的方式獲取數據。
2、底層用json的方式存儲
下載Jar
使用之前先下載jar:
http://www.congiu.net/hive-json-serde/
如果要想在Hive中使用JsonSerde,需要把jar添加到hive類路徑中:
add jar json-serde-1.3.7-jar-with-dependencies.ja

create EXTERNAL table databases.temp_tb(
v string,
algo  string,
rList array<struct<rtype:string,ctype:string,num:string,cid:string,cause:string>> )
partitioned by(
dt string,
)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
STORED AS TEXTFILE
;

alter table databases.temp_tbadd partition (dt='${dt}'') location '/data_dir/'

json存儲問題1 如果是非法數據就會報錯

可以增加配置用以跳過錯誤數據,運行查詢不會報錯,非法數據記錄將變爲NULL。
ALTER TABLE tabled*** SET SERDEPROPERTIES ( "ignore.malformed.json" = "true");

json存儲問題2 json數據中包含hive關鍵字時,導入的數據報錯

此時 SerDe可以使用SerDe屬性將hive列映射到名稱不同的屬性

create EXTERNAL table databases.temp_tb(
ids_alias string,
algo  string,
rList array<struct<rtype:string,ctype:string,num:string,cid:string,cause:string>> )
partitioned by(
dt string,
)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
WITH SERDEPROPERTIES ("mapping.ids_alias"="ids")
STORED AS TEXTFILE
;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章