Hive: How to explode table with map column

AbtPst :

I have a table like this

+-----+------------------------------+
| id    | mapCol                     |
+-----+------------------------------+
| id1   |     {key1:val1, key2:val2} |
| id2   |     {key1:val3, key2:val4} |
+-----+------------------------------+

so i can easily perform a query like

select explode(mapCol) as (key, val) from myTab where id='id1'

and i get

+--------+-----+
| key    | val |
+--------+-----+
| key1   | val1|
| key2   | val2|
+--------+-----+

I want to generate a table like this

+-----+------+-----+
|id   | key  | val |
+-----+------+-----+
| id1 | key1 | val1|
| id1 | key2 | val2|
| id2 | key1 | val3|
| id2 | key2 | val4|
+-----+------------+

note that I want to display the id alongwith the exploded rows. Also, for multiple id's, the key may be repeated, hence I want the rows to reflect that. Basically, id + key should be unique.

How would i write a query for this? I tried

select explode(mapCol) as (key, val), id from myTab

but i got

FAILED: SemanticException 1:66 Only a single expression in the SELECT clause is supported with UDTF's

leftjoin :

Use lateral view:

with MyTable as -------use your table instead of this subquery
(select id, str_to_map(mapStr) mapCol
from
(
select stack(2,
'id1','key1:val1,key2:val2',
'id2','key1:val3,key2:val4'
) as (id, mapStr))s
) -------use your table instead of this subquery

select t.id, s.key, s.val
  from MyTable t
       lateral view outer explode(mapCol) s  as key, val;

Result:

OK
id1     key1    val1
id1     key2    val2
id2     key1    val3
id2     key2    val4
Time taken: 0.072 seconds, Fetched: 4 row(s)

Use your table instead of MyTable subquery.

Read also this answer about lateral view: https://stackoverflow.com/a/51846380/2700344.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=159613&siteId=1