配置单元 - 在多行上拆分分隔列,根据位置进行选择 [英] Hive - Split delimited columns over multiple rows, select based on position

查看:112
本文介绍了配置单元 - 在多行上拆分分隔列,根据位置进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  id col1 col2 
1 5,6 7,8

我想得到结果

  id col1 col2 
1 5 7
1 6 8

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



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



查询

  SELECT col3,col4 
FROM test ext
横向VIEW爆炸(split(col1,'\002'))col1 AS col3
lateral VIEW爆炸(split(col2,'\002'))col2 AS col4


$ b

结果

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


解决方案拆分数组。然后,只选择位置指数相等的那些行。

  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语言手册 - posexplode()


I'm Looking for a way to split the column based on comma delimited data. Below is my dataset

id  col1  col2
1   5,6   7,8

I want to get the result

id col1 col2
1  5    7
1  6    8

The position of the index should match because I need to fetch results accordingly.

I tried the below query but it returns the cartesian product.

Query:

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

Result:

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

解决方案

You can use posexplode() to create position index columns for your split arrays. Then, select only those rows where the position indices are equal.

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;

Output:

id col3 col4
1  5    7
1  6    8

Reference: Hive language manual - posexplode()

这篇关于配置单元 - 在多行上拆分分隔列,根据位置进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆