Hive:如何用地图列分解表格 [英] Hive: How to explode table with map column

查看:41
本文介绍了Hive:如何用地图列分解表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张这样的桌子

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

所以我可以轻松地执行像

so i can easily perform a query like

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

我明白了

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

我想生成这样的表格

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

请注意,我想将 id 与展开的行一起显示.此外,对于多个 id,key 可能会重复,因此我希望行反映这一点.基本上,id + key 应该是唯一的.

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

selectexplode(mapCol) as (key, val), id from myTab

但是我得到了

FAILED: SemanticException 1:66 UDTF 仅支持 SELECT 子句中的单个表达式

推荐答案

使用 侧视图:

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;

结果:

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

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

Use your table instead of MyTable subquery.

另请阅读有关横向视图的答案:https://stackoverflow.com/a/51846380/2700344.

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

这篇关于Hive:如何用地图列分解表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆