【问题标题】:How can I create an external table in HIVE from HDFS file that contains a JSON array?如何从包含 JSON 数组的 HDFS 文件在 HIVE 中创建外部表?
【发布时间】:2018-03-12 14:40:26
【问题描述】:

我的 json 看起来像这样:

  [
  {
  "blocked": 1,
  "object": {
  "ip": "abc",
  "src_ip": "abc",
  "lan_initiated": true,
  "detection": "abc",
  "src_port": ,
  "src_mac": "abc",
  "dst_mac": "abc",
  "dst_ip": "abc",
  "dst_port": "abc"
},
"object_type": "url",
"threat": "",
"threat_type": "abc",
"device_id": "abc",
"app_id": "abc",
"user_id": "abc",
"timestamp": 1520268249657,
"date": {
  "$date": "Mon Mar 05 2018 16:44:09 GMT+0000 (UTC)"
},
"expire": {
  "$date": "Fri May 04 2018 16:44:09 GMT+0000 (UTC)"
},
"_id": "abc"
  }
]

我试过了:

CREATE EXTERNAL TABLE `table_name`(
  reports array<struct<
  user_id: string ,
  device_id: string ,
  app_id: string ,
  blocked: string ,
  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> ,
  object_type: string ,
  threat: string ,
  threat_type: string ,
  servertime:string,
  date_t: struct<dat:string>,
  expire: struct<dat:string>,
  id: string >>)
ROW FORMAT SERDE
  'org.openx.data.jsonserde.JsonSerDe'
WITH SERDEPROPERTIES (
  'ignore.malformed.json'='false','mapping.dat'='$date', 'mapping.servertime'='timestamp','mapping.date'='date_t','mapping._id'='id')
STORED AS INPUTFORMAT
  'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  'abc'

然后

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

但我得到:由于 OWN_TASK_FAILURE 导致顶点未成功 - 由于:null 而被杀死/失败。

我已经读过,因为 JSON 以 '[' 开头,因此无法解析。有任何想法吗? json的结构一定要改吗?

【问题讨论】:

  • 你可以试试 '{"reports": [...your data...]}` 吗?
  • @Bala,谢谢你的提问,我无法修改json文件的结构。

标签: arrays json hive hdfs


【解决方案1】:

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

LOCATION
  'abc'

LOCATION 应该是您的JSON 文件应该存在的文件夹。您可以为 JSON 文件指定任何名称。

您还需要确保JSON-Serde jar 在类路径中。如果没有,请在尝试创建表之前使用以下命令。

hive> add jar /path/to/<json-serde-jar>;

【讨论】:

    猜你喜欢
    • 2017-09-24
    • 2018-05-28
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    相关资源
    最近更新 更多