Hive - 在多行上拆分分隔列,select 基于位置

Hive - Split delimited columns over multiple rows, select based on position

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

id  col1  col2
1   5,6   7,8

我要得到结果

id col1 col2
1  5    7
1  6    8

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

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

查询:

SELECT col3, col4
FROM test ext 
lateral VIEW explode(split(col1,'[=13=]2')) col1 AS col3
lateral VIEW explode(split(col2,'[=13=]2')) col2 AS col4

结果:

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

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

SELECT id, col3, col4
  FROM test
  lateral VIEW posexplode(split(col1,'[=10=]2')) col1 AS pos3, col3
  lateral VIEW posexplode(split(col2,'[=10=]2')) col2 AS pos4, col4
  WHERE pos3 = pos4;

输出:

id col3 col4
1  5    7
1  6    8

参考:Hive language manual - posexplode()