对 Hive 中的数组值列执行数值运算

问题描述

对 hive 表执行数值运算

id 数组 价值
[10:20] 2
b [30:40:50] 5

我想把上面的表格转换成下面的

id 数组 价值 converted_array
[10:20] 2 [20:40]
b [30:40:50] 5 [150:200:250]

我想将“array”列与“value”列相乘,并使用 hql 创建一个新列“converted_array”。我知道如何在 python 中做到这一点,但我想知道是否有办法在 hive 中做到这一点。

解决方法

尝试在分解视图上使用 collect_set 如下所示,这会将值列中的常量整数值与数组中的每个元素相乘(使用爆炸),然后返回结果(使用 collect_set):

select id,collect_set(pve) as pv,value1,collect_set(pve*value1) as converted_array
from stacko1
lateral view explode(price_lov) t as pve
group by id,value1;

这应该会为您提供所需的输出。 enter image description here