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
;

猜你喜欢

转载自blog.csdn.net/qq_36470475/article/details/89226417