【问题标题】:How to import LinkedIn Json file into Hive External Table?如何将 LinkedIn Json 文件导入 Hive 外部表?
【发布时间】:2013-05-24 06:34:29
【问题描述】:

以下是使用 API 从 LinkedIn 导出的 JSON 文件。

{
   "numResults": 21,
   "people":  
             {  "total": 21,
                 "values":
                         {    "firstName": "Kshitiz",
                              "headline": "Interbank Derivatives  Bank Treasury",
                               "id": "aK8sji3rN7",
                               "industry": "Financial Services",
                               "lastName": "Jain",
                               "locations": {"country": {"code": "in"},
                               "name": "Mumbai Area, India"
                                            },
                               "numConnections": 500,
                               "pictureUrl": "http://m3.licT5WVdExyDEYDzE6cp0VwZ"
                          }
             }

}

将上面的json文件保存在一个文本文件中,并导入hadoop目录/sample。

使用以下命令创建外部表。还添加了 serde 的 JAR 文件。

create external table linkedi(numResults int,people Struct<total:int,values:Struct<firstName:String,headline:String,id:String,industry:String,lastName:String,locations:Struct<country:Struct<code:String>,name:String>,numConnections:int,pictureUrl:String>>)
ROW FORMAT SERDE 'org.apache.hadoop.hive.contrib.serde2.JsonSerde' location '/sample'; 

运行select statement ( select * from linkedi;) 时会显示以下错误。

好的 失败并出现异常 java.io.IOException:java.lang.ClassCastException: org.json.JSONObject cannot be cast to [Ljava.lang.Object; 耗时:0.213 秒

显示错误的原因是什么?表的结构是否有任何错误?

【问题讨论】:

    标签: hadoop hive hiveql


    【解决方案1】:

    我遇到了同样的麻烦。 Panshul 是对的,Apache 的 SerDe 不支持嵌套 JSON。但我仍然无法使用“hive-json-serde-0.2.jar”,至少不能使用最新版本的 Hive。

    我发现最好的方法是使用 Openx 的 SerDe 库。简而言之,工作 JAR 是 json-serde-1.3-jar-with-dependencies.jar,可以在 here 找到。这个正在使用“STRUCT”,甚至可以忽略一些格式错误的 JSON。在创建表的过程中,包括以下代码:

     ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
     WITH SERDEPROPERTIES ("ignore.malformed.json" = "true")
     LOCATION ...
    

    如果需要,可以从herehere 重新编译它。在添加了必要的库之后,我尝试了第一个存储库,它对我来说编译得很好。该存储库最近也已更新。

    查看更多详情here

    【讨论】:

    • 如果您可以在此处发布答案的相关部分,那就太好了,以防该链接失效。
    【解决方案2】:

    您使用的 SerDe 不支持嵌套 JSON。您可以尝试先展平 JSON 或 尝试使用: hive-json-serde.googlecode.com/files/hive-json-serde-0.2.jar

    【讨论】:

      猜你喜欢
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 2012-05-27
      • 1970-01-01
      • 2018-03-29
      • 2019-10-21
      • 2017-10-26
      • 2017-08-06
      相关资源
      最近更新 更多