org.apache.hadoop.hive.serde2.SerDeException: Row is not a valid JSON Object
2017-09-15 17:36
1901 查看
重要的是每行必须是一个完整的JSON,一个JSON不能跨越多行,也就是说,serde不会对多行的Json有效。 因为这是由Hadoop处理文件的工作方式决定,文件必须是可拆分的,例如,Hadoop将在行尾分割文本文件。
使用之前先下载jar:
如果要想在Hive中使用JsonSerde,需要把jar添加到Hive类路径中:
源数据:
Hive表:
使用:
源数据:
Hive表:
使用:
格式错误的数据的默认行为是抛出异常。 例如,对于格式不正确的json(languages后缺少':'):
使用:
这种方式不是一种好的策略,我们数据中难免会遇到坏数据。如下操作可以忽略坏数据:
更改设置后:
现在不会导致查询失败,但是坏数据记录将变为NULL NULL NULL。
注意:
如果JSON格式正确,但是不符合Hive范式,则不会跳过,依然会报错:
使用:
这是一个常见的问题,某一个字段有时是一个标量,有时是一个数组,例如:
在这种情况下,如果将表声明为array<string>,如果SerDe找到一个标量,它将返回一个单元素的数组,从而有效地将标量提升为数组。 但是标量必须是正确的类型。
有时可能发生的是,JSON数据具有名为hive中的保留字的属性。 例如,您可能有一个名为“timestamp”的JSON属性,它是hive中的保留字,当发出CREATE TABLE时,hive将失败。 此SerDe可以使用SerDe属性将hive列映射到名称不同的属性。
“mapping.dt”,表示dt列读取JSON属性为exec_date的值。
原文:http://blog.csdn.net/sunnyyoona/article/details/70170173
// 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的值。
相关文章推荐
- object hbase is not a member of package org.apache.hadoop
- 使用JSONObject报错:Java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeEx
- log4j:ERROR A "org.jboss.logging.appender.FileAppender" object is not assignable to a "org.apache.lo
- org.apache.hadoop.ipc.RemoteExceptionOperation category READ is not supported in state standby解决办法
- 启动HDFS之后一直处于安全模式org.apache.hadoop.hdfs.server.namenode.SafeModeException: Log not rolled. Name node is in safe mode.
- 配apache出错:libtool: link: `passwd/apr_getpass.lo' is not a valid libtool object
- hadoop错误org.apache.hadoop.util.DiskChecker$DiskErrorException Could not find any valid local directory for
- java.lang.ClassNotFoundException: org.apache.hadoop.hive.hbase.HBaseSplit
- Hive ClassNotFoundException: Class org.apache.hadoop.hive.contrib.serde2.RegexSerDe not found
- error: object kafka is not a member of package org.apache.spark.streaming
- ERROR: org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not running yet
- log4j:ERROR A "org.jboss.logging.appender.FileAppender" object is not assignable to a "org.apache.lo
- org.apache.hadoop.hbase.NotServingRegionException: Region is not online 错误
- 解决hiveserver2报错:java.io.IOException: Job status not available - Error while processing statement: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.mr.MapRedTask
- 使用Hive时提示org.apache.hadoop.metrics.jvm.EventCounter is deprecated警告的解决方案
- org.apache.hadoop.hbase.NotServingRegionException: Region is not online 错误
- hive执行查询出现:org.apache.hadoop.hive.ql.metadata.HiveException: Hive Runtime Error while processing row
- 解决kylin sync table报错:MetaException(message:java.lang.ClassNotFoundException Class org.apache.hive.hcatalog.data.JsonSerDe not found
- Hive 启动报错 ClassNotFoundException: org.apache.hadoop.hive.service.HiveServer
- org.apache.hadoop.hbase.NotServingRegionException: Region is not online 错误