我需要创建一个新列或将原始数组从array <string>更改为map <string,int>,其中int值在各个字符串的新表中

时间:2019-03-27 06:35:42

标签: sql hadoop hive hiveql

我有两个表。
1.table1:(string,array(string))

abc ["s","m"]
def ["m","a","l"]
xyz ["s","a"]
2.table2:(string,int)
m 12
s 26
l 57
a 45
现在我想要如下表所示:(string,map(string,int))
abc ["s":26,"m":12]
def ["m":12,"a":45,"l":57]
xyz ["s":26,"a":45]
1.为此,我需要 HIVE 查询。
2。如何为
abc 38

这样的弓形行求和

1 个答案:

答案 0 :(得分:0)

这可以解决问题

select t1.cat, collect_list(concat_ws(":", t1.subs, cast(t2.cnt as string))) from 
(select cat, subs from temp.table1
lateral view explode(sub) s as subs) t1
join 
    temp.table2 t2
on
    t1.subs = t2.sub
group by t1.cat

如果要基于上表进行求和。

select cat, sum(split(sub, ":")[1]) from temp.test3
lateral view explode(`_c1`) c as sub
group by cat