从json数组文件格式创建hive表

时间:2018-06-08 23:57:09

标签: json hive

我在s3中有json文件格式:

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

需要将其加载到hive表中:

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

我知道这是先前被问到的问题,但我没有得到我所期待的答案。

1 个答案:

答案 0 :(得分:0)

使用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
相关问题