I have data in below format in hive. table test(seq string, result string);
|seq | result |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|0001 | [{"offerId":"Default_XYZ","businessName":"Apple","businessGroup":"Default","businessIssue":"Default","interactionId":"-4930126168287369915","campaignID":"P-1","rank":"1"},{"offerId":"Default_NAV","businessName":"Orange","businessGroup":"Default","businessIssue":"Default","interactionId":"-7830126168223452134","campaignID":"P-1","rank":"2"}] |
Output should be like
|seq | offerId | businessName | businsesGroup| businessIssue | interactionId | campaignId | rank |
----------------------------------------------------------------------------------------------------------------
|0001 | Default_XYZ | Apple | Default | Default | -4930126168287369915 | P-1 | 1 |
|0001 | Default_NAV | Orange | Default | Default | -7830126168223452134 | P-1 | 2 |
I tried to convert string to Array of struct, but it didnt work with direct CAST.
Any help please?
[EDIT - Tried below query]
select sequenceNumber, offerId, businessName, rank from (
select sequenceNumber,
collect_list(oid['offerId']) as offerid_list
, collect_list(oid['businessName']) as businessName_list
,collect_list(oid['rank']) as rank_list
from (
select sequenceNumber,
str_to_map(translate(offer_Id,'{}','')) as oid
from test
lateral view explode (split(translate(result, '[]"',''),"\\},")) oid as offer_id
) x
group by sequenceNumber
) y lateral view explode(offerid_list) olist as offerId
lateral view explode(businessName_list) olist as businessName
lateral view explode(rank_list) rlist as rank