Hive 从嵌套数组中提取数据
Hive extract data from Nested Array
您好,我需要从 数组 中提取数据,我正在使用 Athena
create external table test
(
customer string
)
Location 'something-something'
这个table的单行是,
select * from customer limit 1
{ "ID": "XXXX", "USerDate": { "items": [{ "Name": "Nir", "CLG": "NPT", "Place": "CBE", "Any Group": {}, "Interest": { "items": [{ "Games": "Cricket", "Music": "AR" }] }, "Others": {} }] } }
我需要像这样提取行
|编号 |名称 |地点 |游戏|音乐 |
|-----|---------|---------|---------|----- -----|
select json_extract_scalar(customer,'$.ID') as ID
,json_extract_scalar(i1.item,'$.Name') as Name
,json_extract_scalar(i1.item,'$.Place') as Place
,json_extract_scalar(i2.item,'$.Games') as Games
,json_extract_scalar(i2.item,'$.Music') as Music
from test
cross join unnest (cast(json_extract(customer,'$.USerDate.items')
as array(json))) as i1 (item)
cross join unnest (cast(json_extract(i1.item,'$.Interest.items')
as array(json))) as i2 (item)
;
ID | Name | Place | Games | Music
------+------+-------+---------+-------
XXXX | Nir | CBE | Cricket | AR
您好,我需要从 数组 中提取数据,我正在使用 Athena
create external table test
(
customer string
)
Location 'something-something'
这个table的单行是,
select * from customer limit 1
{ "ID": "XXXX", "USerDate": { "items": [{ "Name": "Nir", "CLG": "NPT", "Place": "CBE", "Any Group": {}, "Interest": { "items": [{ "Games": "Cricket", "Music": "AR" }] }, "Others": {} }] } }
我需要像这样提取行
|编号 |名称 |地点 |游戏|音乐 |
|-----|---------|---------|---------|----- -----|
select json_extract_scalar(customer,'$.ID') as ID
,json_extract_scalar(i1.item,'$.Name') as Name
,json_extract_scalar(i1.item,'$.Place') as Place
,json_extract_scalar(i2.item,'$.Games') as Games
,json_extract_scalar(i2.item,'$.Music') as Music
from test
cross join unnest (cast(json_extract(customer,'$.USerDate.items')
as array(json))) as i1 (item)
cross join unnest (cast(json_extract(i1.item,'$.Interest.items')
as array(json))) as i2 (item)
;
ID | Name | Place | Games | Music
------+------+-------+---------+-------
XXXX | Nir | CBE | Cricket | AR