with mydata as (
select 1 id, map('key11','val11','key12','val12','key13','val13') as mymap
union all
select 2 id, map('key21','val21','key22','val22','key13','val13') as mymap --Key13 also exist in first row
)
select id, map_keys(d.mymap) keys
from mydata d
;
结果:
id keys
1 ["key11","key12","key13"]
2 ["key21","key22","key13"]
with mydata as (
select 1 id, map('key11','val11','key12','val12','key13','val13') as mymap
union all
select 2 id, map('key21','val21','key22','val22','key13','val13') as mymap --Key13 also exist in first row
)
select --id,
collect_set(key) as keys
from mydata d
lateral view outer explode(map_keys(d.mymap)) e as key
--group by id --without id in groupby you get the distinct list of keys in all rows
--with id in groupby you get list of map keys for each row
;
1条答案
按热度按时间eyh26e7m1#
函数Map键(map)返回一个无序数组,其中包含输入Map的键。
示例,请参见代码中的注解:
结果:
如果需要所有行中唯一键的列表,请分解数组并使用collect\u set再次收集,它将返回不同键的数组:
结果: