Reputation: 831
I am seeking some advise on understanding the possibility of converting Map in hive to columns (transpose) Some sample records as and how i like to represent them are given below.
{"AR":["Fasdfadsfasd","Hasdfasdf"]}
{"DR":["Aasdfads","Baasdfasdf","Iasdfadsf","Zasdfasdf"]}
{"PR":["Easdfadsf","Gasdfadsfads"]}
{"SR":["Casdfasdf","Dasdfadsf","Jasdfasdfa"]}
AR DR PR SR
Fasdfadsfasd Aasdfads Easdfadsf Casdfasdf
Hasdfasdf Baasdfasdf Gasdfadsfads Dasdfadsf
NULL Iasdfadsf NULL Jasdfasdfa
NULL Zasdfasdf NULL NULL
Appreciate the help!
Upvotes: 1
Views: 84
Reputation: 38325
See comments in the code. Transformation steps were not combined intentionally to make it easier to understand:
with your_table as ( --use your_table instead of this
select stack(4,
'{"AR":["Fasdfadsfasd","Hasdfasdf"]}',
'{"DR":["Aasdfads","Baasdfasdf","Iasdfadsf","Zasdfasdf"]}',
'{"PR":["Easdfadsf","Gasdfadsfads"]}',
'{"SR":["Casdfasdf","Dasdfadsf","Jasdfasdfa"]}'
) as src
)
select max(AR) AR, max(DR) DR, max(PR) PR, max(SR) SR --group by position
from
(
select case when key='AR' then value end AR, --transpose
case when key='DR' then value end DR,
case when key='PR' then value end PR,
case when key='SR' then value end SR,
pos
from
(
select s.key, regexp_replace(v.value,'(^\\")|(\\"$)','') value, v.pos --remove quotes from value
from
(
select regexp_replace(m[0],'(^\\")|(\\"$)','') key, regexp_replace(m[1],'\\[|\\]','') value --remove [] from value and unquote key
from
(
select split(
regexp_replace(src,'\\{|\\}',''), --remove curly braces
'[:]') as m --split map key and value
from your_table t
)s
)s lateral view outer posexplode(split(value,'\\",\\"')) v as pos, value
)s
)s
group by pos;
Result:
ar dr pr sr
Fasdfadsfasd Aasdfads Easdfadsf Casdfasdf
Hasdfasdf Baasdfasdf Gasdfadsfads Dasdfadsf
NULL Iasdfadsf NULL Jasdfasdfa
NULL Zasdfasdf NULL NULL
Upvotes: 1