Hive:如何用地图列分解表

Abt*_*Pst 4 java dictionary hive user-defined-functions hiveql

我有一张这样的桌子

+-----+------------------------------+
| id    | mapCol                     |
+-----+------------------------------+
| id1   |     {key1:val1, key2:val2} |
| id2   |     {key1:val3, key2:val4} |
+-----+------------------------------+
Run Code Online (Sandbox Code Playgroud)

所以我可以轻松执行类似的查询

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

我得到

+--------+-----+
| key    | val |
+--------+-----+
| key1   | val1|
| key2   | val2|
+--------+-----+
Run Code Online (Sandbox Code Playgroud)

我想生成一个这样的表

+-----+------+-----+
|id   | key  | val |
+-----+------+-----+
| id1 | key1 | val1|
| id1 | key2 | val2|
| id2 | key1 | val3|
| id2 | key2 | val4|
+-----+------------+
Run Code Online (Sandbox Code Playgroud)

请注意,我想显示id以及分解的行。另外,对于多个 id,key可能会重复,因此我希望行能够反映这一点。基本上,id+key应该是唯一的。

我该如何为此编写查询?我试过

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

但我得到了

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

lef*_*oin 5

使用侧视图

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;
Run Code Online (Sandbox Code Playgroud)

结果:

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

使用您的表而不是MyTable子查询。

另请阅读有关横向视图的答案:/sf/answers/3629246631/