如何从包含json数组的hdfs文件在配置单元中创建外部表?

798qvoo8  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(263)

我的json如下所示:

  1. [
  2. {
  3. "blocked": 1,
  4. "object": {
  5. "ip": "abc",
  6. "src_ip": "abc",
  7. "lan_initiated": true,
  8. "detection": "abc",
  9. "src_port": ,
  10. "src_mac": "abc",
  11. "dst_mac": "abc",
  12. "dst_ip": "abc",
  13. "dst_port": "abc"
  14. },
  15. "object_type": "url",
  16. "threat": "",
  17. "threat_type": "abc",
  18. "device_id": "abc",
  19. "app_id": "abc",
  20. "user_id": "abc",
  21. "timestamp": 1520268249657,
  22. "date": {
  23. "$date": "Mon Mar 05 2018 16:44:09 GMT+0000 (UTC)"
  24. },
  25. "expire": {
  26. "$date": "Fri May 04 2018 16:44:09 GMT+0000 (UTC)"
  27. },
  28. "_id": "abc"
  29. }
  30. ]

我试过:

  1. CREATE EXTERNAL TABLE `table_name`(
  2. reports array<struct<
  3. user_id: string ,
  4. device_id: string ,
  5. app_id: string ,
  6. blocked: string ,
  7. object: struct<ip:string,src_ip:string,lan_initiated:string,detection:string,src_port:string,src_mac:string,dst_mac:string,dstp_ip:string,dst_port:string> ,
  8. object_type: string ,
  9. threat: string ,
  10. threat_type: string ,
  11. servertime:string,
  12. date_t: struct<dat:string>,
  13. expire: struct<dat:string>,
  14. id: string >>)
  15. ROW FORMAT SERDE
  16. 'org.openx.data.jsonserde.JsonSerDe'
  17. WITH SERDEPROPERTIES (
  18. 'ignore.malformed.json'='false','mapping.dat'='$date', 'mapping.servertime'='timestamp','mapping.date'='date_t','mapping._id'='id')
  19. STORED AS INPUTFORMAT
  20. 'org.apache.hadoop.mapred.TextInputFormat'
  21. OUTPUTFORMAT
  22. 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
  23. LOCATION
  24. 'abc'

之后呢

  1. SELECT * FROM table_name
  2. LATERAL VIEW outer explode(reports) exploded_table as rep;

但是我得到:由于自己的任务失败,vertex没有成功-被杀死/失败原因:null。
我已经读过了,因为json以“[”开头,所以无法解析它。有什么想法吗?json的结构必须改变吗?

kadbb459

kadbb4591#

我相信您在代码中指定位置时犯了错误,您已经提到了

  1. LOCATION
  2. 'abc'
  3. ``` `LOCATION` 应为您的 `JSON` 文件应该存在。你可以给这辆车取任何名字 `JSON` 文件。
  4. 您还需要确保json serde jar在类路径中。如果没有,请在尝试创建表之前使用下面的命令。

hive> add jar /path/to/;

相关问题