HIve中嵌套JSON出错

时间:2017-04-26 05:13:32

标签: json hadoop hive nested hive-serde

我试图在hive中加载这个json数据

tar -xvf source destiantion

使用DDL命令

{
    "id": "0001",
    "type": "donut",
     "name": "Cake",
     "ppu": 0.55,
     "batters":
         {
             "batter":
                 [
                     { "id": "1001", "type": "Regular" },
                     { "id": "1002", "type": "Chocolate" },
                    { "id": "1003", "type": "Blueberry" },
                    { "id": "1004", "type": "Devil's Food" }
                ]
        },
    "topping":
        [
            { "id": "5001", "type": "None" },
            { "id": "5002", "type": "Glazed" },
            { "id": "5005", "type": "Sugar" },
            { "id": "5007", "type": "Powdered Sugar" },
            { "id": "5006", "type": "Chocolate with Sprinkles" },
            { "id": "5003", "type": "Chocolate" },
            { "id": "5004", "type": "Maple" }
        ]
}

让我犯错误

ADD JAR /home/cloudera/Downloads/json-serde-1.3.6-SNAPSHOT-jar-with-dependencies.jar;

CREATE EXTERNAL TABLE format.json_serde (
  `id` string,
  `type` string,
  `name` string,
 `ppu` float,       
  batters` struct < `batter`:array < struct <`bid`:string, `btype`:string >>>,
  `topping`:array < struct<`tid`:int, `ttype`:string>>
)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe';

2 个答案:

答案 0 :(得分:0)

  1. 你有拼写错误 ttype`:string 应为 ttype:string
    battersstruct 应为 batters struct
    顶部:数组应为顶部数组

  2. JSON SerDe映射按名称完成 您的结构字段名称应与实际名称匹配,例如id而非bidtid,否则您将获得这些字段的NULL值。

  3. 已经有一个JSON SerDe whicg是Hive安装的一部分。 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-RowFormats&SerDe

  4. create external table json_serde 
    ( 
        id      string
       ,type    string 
       ,name    string 
       ,ppu     float
       ,batters struct<batter:array<struct<id:string,type:string>>>
       ,topping array<struct<id:string,type:string>>
    ) 
    row format serde 
    'org.apache.hive.hcatalog.data.JsonSerDe' 
    stored as textfile
    ;
    
    select * from json_serde
    ;
    
    +------+-------+------+-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    |  id  | type  | name |        ppu        |                                                                     batters                                                                      |                                                                                                                  topping                                                                                                                  |
    +------+-------+------+-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | 0001 | donut | Cake | 0.550000011920929 | {"batter":[{"id":"1001","type":"Regular"},{"id":"1002","type":"Chocolate"},{"id":"1003","type":"Blueberry"},{"id":"1004","type":"Devil'sFood"}]} | [{"id":"5001","type":"None"},{"id":"5002","type":"Glazed"},{"id":"5005","type":"Sugar"},{"id":"5007","type":"PowderedSugar"},{"id":"5006","type":"ChocolatewithSprinkles"},{"id":"5003","type":"Chocolate"},{"id":"5004","type":"Maple"}] |
    +------+-------+------+-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    

答案 1 :(得分:0)

当我删除顶部附近的分号时,它有效。感谢


CREATE EXTERNAL TABLE format.json_serde(
id字符串,
type字符串,
name字符串,
ppu浮动,

batters struct&lt; batter:array&lt; struct&lt; bid:string,btype:string&gt;&gt;&gt;,


topping数组&lt; struct&lt; tid:string,ttype:string&gt;&gt; )