在 Hive 中的多个列上爆炸 [英] Explode on multiple columns in Hive

查看:19
本文介绍了在 Hive 中的多个列上爆炸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Hive 中分解多列中的记录.

I'm trying to explode records in multiple columns in Hive.

例如,如果我的数据集看起来像这样 -

For example, if my dataset looks like this -

COL_01  COL_02     COL_03
1       A, B       X, Y, Z
2       D, E, F    V, W

我想要这个作为输出 -

I want this as the output -

COL_01  COL_02  COL_03
1       A        X
1       B        Y
1       NULL     Z
2       D        V
2       E        W
2       F        NULL

有没有办法在 Hive 中做到这一点?

Is there a way to do this in Hive?

我看到了一些关于单列爆炸的帖子,但没有像这种情况下的多列爆炸.

I saw some posts on exploding for a single column but not for multiple columns like in this case.

推荐答案

在子查询中单独分解并使用完全连接加入它们.

Explode separately in subqueries and join them using full join.

with your_data as (
select stack(2,
1, 'A, B',     'X, Y, Z',
2, 'D, E, F',  'V, W'
) as (col_01, col_02, col_03)
) 

select nvl(s1.col_01,s2.col_01) as col_01, --do the same nvl() for all not exploded columns
       s1.col_02, s2.col_03
from
(select d.col_01, c2.pos2, c2.col_02 --explode col_02
  from your_data d
       lateral view outer posexplode(split(col_02,', ?')) c2 as pos2, col_02
)s1

full join

(select d.col_01, c3.pos3, c3.col_03 --explode col_03
  from your_data d
       lateral view outer posexplode(split(col_03,', ?')) c3 as pos3, col_03
)s2
on s1.col_01=s2.col_01 
   and s2.pos3=s1.pos2 --match position

结果:

col_01  s1.col_02   s2.col_03   
1          A           X    
1          B           Y    
1          NULL        Z    
2          D           V    
2          E           W    
2          F           NULL 

这篇关于在 Hive 中的多个列上爆炸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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