Hive对同一表中其他数组列的排序数组列

Hive对同一表中其他数组列的排序数组列,第1张

Hive对同一表中其他数组列的排序数组列

分解两个数组,进行排序,然后再次聚合数组。

sort
在子查询中使用之前
collect_list
对数组进行排序:

with your_data as(select array(1,2,3,4,5) as col1,array(0.43,0.01,0.45,0.22,0.001)as col2)select original_col1,original_col2, collect_list(c1_x) as new_col1, collect_list(c2_x) as new_col2from(select d.col1 as original_col1,d.col2 as original_col2, c1.x as c1_x, c2.x as c2_x, c1.i as c1_i   from your_data d      lateral view posexplode(col1) c1 as i,x      lateral view posexplode(col2) c2 as i,xwhere c1.i=c2.i distribute by original_col1,original_col2sort by c2_x)sgroup by original_col1,original_col2;

结果:

OKoriginal_col1   original_col2        new_col1        new_col2[1,2,3,4,5]     [0.43,0.01,0.45,0.22,0.001]     [5,2,4,1,3]     [0.001,0.01,0.22,0.43,0.45]Time taken: 34.642 seconds, Fetched: 1 row(s)

编辑:同一脚本的简化版本,您可以不用第二次posexplode,而是按位置使用直接引用

d.col2[c1.i] as c2_x

with your_data as(select array(1,2,3,4,5) as col1,array(0.43,0.01,0.45,0.22,0.001)as col2)select original_col1,original_col2, collect_list(c1_x) as new_col1, collect_list(c2_x) as new_col2from(select d.col1 as original_col1,d.col2 as original_col2, c1.x as c1_x, d.col2[c1.i] as c2_x, c1.i as c1_i   from your_data d      lateral view posexplode(col1) c1 as i,xdistribute by original_col1,original_col2sort by c2_x)sgroup by original_col1,original_col2;


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4980560.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存