从 JSON 数组文件格式创建 Hive 表



我在 s3 中有 json 文件格式:

{
"Id" : "123-6789",
"items" : [ {
item1: "chair",
item2: "table"
}, {
item1: "shoes",
item2: "socks"
}, {
item1: "phone",
item2: "charger"
} ]
}

需要将其加载到蜂巢表中:

create EXTERNAL table Items(
Id string,
Items array<struct<item1:string,item2:string>>)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
LOCATION 's3://path/';

当我从项目中选择*时,我得到:

Id        items 
123-6789 [{"item1":"chair","item2":"table"},{"item1":"shoes","item2":"socks"},{"item1":"phone","item2":"charger"}]

我需要以下输出:

Id          Item1   Item2
123-6789    chair   table
123-6789    shoes   socks
123-6789    phone   charger

我知道这是以前问过的问题,但我没有得到我所期望的答案。

使用LATERAL VIEWexplode

select Id,a.item1,a.item2
from
Items LATERAL VIEW explode (result) r as a

或者

select 
Id, 
get_json_object(Items,'$.item1') as item1,
get_json_object(Items,'$.item2') as item2
from items

最新更新