[Hive]JsonSerde使用指南

简介: 注意:重要的是每行必须是一个完整的JSON,一个JSON不能跨越多行,也就是说,serde不会对多行的Json有效。 因为这是由Hadoop处理文件的工作方式决定,文件必须是可拆分的,例如,Hadoop将在行尾分割文本文件。

注意:

重要的是每行必须是一个完整的JSON,一个JSON不能跨越多行,也就是说,serde不会对多行的Json有效。 因为这是由Hadoop处理文件的工作方式决定,文件必须是可拆分的,例如,Hadoop将在行尾分割文本文件。

// this will work
{ "key" : 10 }

// this will not work
{
  "key" : 10 
}

2. 下载Jar

使用之前先下载jar:

http://www.congiu.net/hive-json-serde/

如果要想在Hive中使用JsonSerde,需要把jar添加到Hive类路径中:

add jar json-serde-1.3.7-jar-with-dependencies.jar;

3. 与数组使用

源数据:

{"country":"Switzerland","languages":["German","French","Italian"]}
{"country":"China","languages":["chinese"]}

Hive表:

CREATE TABLE tmp_json_array (
    country string,
    languages array<string> 
)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
STORED AS TEXTFILE;

LOAD DATA LOCAL INPATH '/home/xiaosi/a.txt' OVERWRITE INTO TABLE  tmp_json_array;

使用:

hive> select languages[0] from tmp_json_array;
OK
German
chinese
Time taken: 0.096 seconds, Fetched: 2 row(s)

4. 嵌套结构

源数据:

{"country":"Switzerland","languages":["German","French","Italian"],"religions":{"catholic":[6,7]}}
{"country":"China","languages":["chinese"],"religions":{"catholic":[10,20],"protestant":[40,50]}}

Hive表:

CREATE TABLE tmp_json_nested (
    country string,
    languages array<string>,
    religions map<string,array<int>>)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
STORED AS TEXTFILE;

LOAD DATA LOCAL INPATH '/home/xiaosi/a.txt' OVERWRITE INTO TABLE  tmp_json_nested ;

使用:

hive> select * from tmp_json_nested;
OK
Switzerland	["German","French","Italian"]	{"catholic":[6,7]}
China	["chinese"]	{"catholic":[10,20],"protestant":[40,50]}
Time taken: 0.113 seconds, Fetched: 2 row(s)
hive> select languages[0] from tmp_json_nested;
OK
German
chinese
Time taken: 0.122 seconds, Fetched: 2 row(s)
hive> select religions['catholic'][0] from tmp_json_nested;
OK
6
10
Time taken: 0.111 seconds, Fetched: 2 row(s)

5. 坏数据

格式错误的数据的默认行为是抛出异常。 例如,对于格式不正确的json(languages后缺少':'):

{"country":"Italy","languages"["Italian"],"religions":{"protestant":[40,50]}}

使用:

hive> LOAD DATA LOCAL INPATH '/home/xiaosi/a.txt' OVERWRITE INTO TABLE  tmp_json_nested ;
Loading data to table default.tmp_json_nested
OK
Time taken: 0.23 seconds
hive> select * from tmp_json_nested;
OK
Failed with exception java.io.IOException:org.apache.hadoop.hive.serde2.SerDeException: 
Row is not a valid JSON Object - JSONException: Expected a ':' after a key at 31 [character 32 line 1]
Time taken: 0.096 seconds

这种方式不是一种好的策略,我们数据中难免会遇到坏数据。如下操作可以忽略坏数据:

ALTER TABLE json_table SET SERDEPROPERTIES ( "ignore.malformed.json" = "true");

更改设置后:

hive> ALTER TABLE tmp_json_nested SET SERDEPROPERTIES ( "ignore.malformed.json" = "true");
OK
Time taken: 0.122 seconds
hive> select * from tmp_json_nested;
OK
Switzerland	["German","French","Italian"]	{"catholic":[6,7]}
China	["chinese"]	{"catholic":[10,20],"protestant":[40,50]}
NULL	NULL	NULL
Time taken: 0.103 seconds, Fetched: 3 row(s)

现在不会导致查询失败,但是坏数据记录将变为NULL NULL NULL。

注意:

如果JSON格式正确,但是不符合Hive范式,则不会跳过,依然会报错:

{"country":"Italy","languages":"Italian","religions":{"catholic":"90"}}

使用:

hive> ALTER TABLE tmp_json_nested SET SERDEPROPERTIES ( "ignore.malformed.json" = "true");
OK
Time taken: 0.081 seconds
hive> select * from tmp_json_nested;
OK
Failed with exception java.io.IOException:org.apache.hadoop.hive.ql.metadata.HiveException: java.lang.ClassCastException:
java.lang.String cannot be cast to org.openx.data.jsonserde.json.JSONArray
Time taken: 0.097 seconds

6. 将标量转为数组

这是一个常见的问题,某一个字段有时是一个标量,有时是一个数组,例如:

{ field: "hello", .. }
{ field: [ "hello", "world" ], ...

在这种情况下,如果将表声明为array<string>,如果SerDe找到一个标量,它将返回一个单元素的数组,从而有效地将标量提升为数组。 但是标量必须是正确的类型。

7. 映射Hive关键词

有时可能发生的是,JSON数据具有名为hive中的保留字的属性。 例如,您可能有一个名为“timestamp”的JSON属性,它是hive中的保留字,当发出CREATE TABLE时,hive将失败。 此SerDe可以使用SerDe属性将hive列映射到名称不同的属性。

{"country":"Switzerland","exec_date":"2017-03-14 23:12:21"}
{"country":"China","exec_date":"2017-03-16 03:22:18"}
CREATE TABLE tmp_json_mapping (
    country string,
    dt string
)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
WITH SERDEPROPERTIES ("mapping.dt"="exec_date")
STORED AS TEXTFILE;
hive> select * from tmp_json_mapping;
OK
Switzerland	2017-03-14 23:12:21
China	2017-03-16 03:22:18
Time taken: 0.081 seconds, Fetched: 2 row(s)

“mapping.dt”,表示dt列读取JSON属性为exec_date的值。


原文:https://github.com/rcongiu/Hive-JSON-Serde

目录
相关文章
|
存储 SQL 大数据
hive使用指南
创建库:create database base_name; 创建内/外部表: CREATE EXTERNAL TABLE t_lxw1234 ( id INT, ip STRING COMMENT ‘访问者IP’, avg_view_depth DECIMAL(5,1), ...
1834 0
|
存储 SQL 机器学习/深度学习
[Hive]Hive使用指南七 空值与NULL
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SunnyYoona/article/details/78276551 1.
3323 0
|
SQL HIVE
[Hive]Lateral View使用指南
1. 语法 lateralView: LATERAL VIEW udtf(expression) tableAlias AS columnAlias (',' columnAlias)* fromClause: FROM baseTable (lateralView)* 2. 描述 Lateral View一般与用户自定义表生成函数(如explode())结合使用。
4226 0
|
SQL HIVE iOS开发
[Hive]Union使用指南
1. union语法 select_statement UNION [ALL | DISTINCT] select_statement UNION [ALL | DISTINCT] select_statement ... UNION将多个SELECT语句的结果集合并为一个独立的结果集。
2155 0
|
SQL Unix HIVE
[Hive]Hive使用指南六 日期相关函数
下面介绍一下常用的Hive日期处理相关函数。 1. to_date 日期时间转日期函数 语法: to_date(string timestamp) 返回值: string 说明: 返回日期时间字段中的日期部分。
1412 0
|
SQL HIVE
[Hive]Hive使用指南四 客户端导入数据
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SunnyYoona/article/details/52935649 根据导...
936 0
|
SQL HIVE Perl
[Hive]Hive使用指南五 客户端导出数据
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SunnyYoona/article/details/52924452 根据导...
781 0
|
SQL 数据库 HIVE
[Hive]Hive使用指南二 Hive命令的3种调用方式
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SunnyYoona/article/details/51549329 1. 多语句执行 执行HQL脚本 hive –f /root/shell/hive-script.sql hive-script.sql类似于script一样,直接写查询命令就行。
1292 0
|
4天前
|
SQL 数据采集 数据挖掘
大数据行业应用之Hive数据分析航班线路相关的各项指标
大数据行业应用之Hive数据分析航班线路相关的各项指标
112 1
|
4天前
|
SQL 存储 大数据
【大数据技术Hadoop+Spark】Hive基础SQL语法DDL、DML、DQL讲解及演示(附SQL语句)
【大数据技术Hadoop+Spark】Hive基础SQL语法DDL、DML、DQL讲解及演示(附SQL语句)
90 0

热门文章

最新文章