Reputation: 364
I'm trying to explode records in multiple columns in Hive.
For example, if my dataset looks like this -
COL_01 COL_02 COL_03
1 A, B X, Y, Z
2 D, E, F V, W
I want this as the output -
COL_01 COL_02 COL_03
1 A X
1 B Y
1 NULL Z
2 D V
2 E W
2 F NULL
Is there a way to do this in Hive?
I saw some posts on exploding for a single column but not for multiple columns like in this case.
Upvotes: 2
Views: 2879
Reputation: 38325
Explode separately in subqueries and join them using full join.
with your_data as (
select stack(2,
1, 'A, B', 'X, Y, Z',
2, 'D, E, F', 'V, W'
) as (col_01, col_02, col_03)
)
select nvl(s1.col_01,s2.col_01) as col_01, --do the same nvl() for all not exploded columns
s1.col_02, s2.col_03
from
(select d.col_01, c2.pos2, c2.col_02 --explode col_02
from your_data d
lateral view outer posexplode(split(col_02,', ?')) c2 as pos2, col_02
)s1
full join
(select d.col_01, c3.pos3, c3.col_03 --explode col_03
from your_data d
lateral view outer posexplode(split(col_03,', ?')) c3 as pos3, col_03
)s2
on s1.col_01=s2.col_01
and s2.pos3=s1.pos2 --match position
Result:
col_01 s1.col_02 s2.col_03
1 A X
1 B Y
1 NULL Z
2 D V
2 E W
2 F NULL
Upvotes: 3
Reputation: 797
@Manu - You can do a lateral view on 2 columns but that would be a cross product. but I see what you need is a one to 1 mapping between the columns.
Any change you can create a map field with columns col02 and col03?
Upvotes: 0