根据配置单元中collect\u列表的结果构造Map

but5z9lq  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(299)

一系列的union all产生一个键值对列表,我想用它来构建一个Map。
所需的功能如下:

select id1, id2, map(collect_list(col)) as measurements
from
(
    select id1, id2, "height" as col
    union all
    select id1, id2, count(*) as col from table1
    union all
    select id1, id2, "weight" as col
    union all
    select id1, id2, count(*) as col from table2
)

正确的方法是什么?
我希望得到的结果是:

id1  id2  measurements
1    10   {"height": 10, "weight": 20}
2    20   {"height": 10, "weight": 20}
wecizke3

wecizke31#

你的要求不是很清楚,但看起来你需要这样的东西

select id1, id2, named_struct("height", height, "weight", weight) from 
(
select t1.id1,t1,id2,height,weight from 
(select id1, id2, count(*) as height from table1 group by id1,id2) t1
join 
(select id1, id2, count(*) as height from table2 group by id1,id2) t2
on t1.d1=t2.d1 and t1.d2=t2.d2
) t;

我没有运行它,但它应该工作。

相关问题