Create hive table from JSON data

半世苍凉 提交于 2019-12-11 07:39:01

问题


I have a file with Json data which takes the below form:

Ex:

 {
    "Name": "xxxx",
    "Address": [{
        "Street": "aa",
        "City": "bbb"
    }, {
        "Street": "ccc",
        "City": "ddd",
        "Country": "eee"
    }]
}

The above Json is a valid Json. I want to create a hive table on top of data of above form using JsonSerde.


回答1:


Create table with all possible fields defined. If field is not present in json, select will return NULL:

CREATE EXTERNAL TABLE your_table (
Name string,
Address array<struct<Street:string,City:string,Country:string>>
)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'

if you have names in JSON file which conflict with Hive reserved words then add mapping and rename names in table definition:

WITH SERDEPROPERTIES ('mapping.renamed_column'='original_column') and rename your table columns.

Put your file in the table location.

See also docs with some examples here: https://github.com/rcongiu/Hive-JSON-Serde



来源:https://stackoverflow.com/questions/44505954/create-hive-table-from-json-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!