gpt4 book ai didi

hadoop - Hive - 在多行上拆分分隔列,根据位置选择

转载 作者:可可西里 更新时间:2023-11-01 14:22:00 27 4
gpt4 key购买 nike

我正在寻找一种基于逗号分隔数据拆分列的方法。下面是我的数据集

id  col1  col2
1 5,6 7,8

我要得到结果

id col1 col2
1 5 7
1 6 8

索引的位置应该匹配,因为我需要相应地获取结果。

我尝试了以下查询,但它返回了笛卡尔积。

查询:

SELECT col3, col4
FROM test ext
lateral VIEW explode(split(col1,'\002')) col1 AS col3
lateral VIEW explode(split(col2,'\002')) col2 AS col4

结果:

id col1 col2
1 5 7
1 5 8
1 6 7
1 6 8

最佳答案

您可以使用 posexplode() 为拆分数组创建位置索引列。然后,只选择位置索引相等的那些行。

SELECT id, col3, col4
FROM test
lateral VIEW posexplode(split(col1,'\002')) col1 AS pos3, col3
lateral VIEW posexplode(split(col2,'\002')) col2 AS pos4, col4
WHERE pos3 = pos4;

输出:

id col3 col4
1 5 7
1 6 8

引用:Hive language manual - posexplode()

关于hadoop - Hive - 在多行上拆分分隔列,根据位置选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37585638/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com