使用split() 拆分您的字符串,使用posexplode() 分解并使用横向视图。
由空字符串分割'' 将生成带有额外空元素的数组,将它们过滤掉,通过它们在数组中的位置连接项目和数字。
演示(将 s 子查询替换为您的表):
select i.itm as item, n.nbr as number
from
(--replace this subquery with your table
--this is two rows example
select stack(2,'ABC', '123',
'DEF', '456') as (item, number)
) s --your_table
lateral view posexplode(split(s.item,'')) i as pos, itm
lateral view posexplode(split(s.number,'')) n as pos, nbr
where i.itm!='' and n.nbr!='' and i.pos=n.pos ;
结果:
OK
item number
A 1
B 2
C 3
D 4
E 5
F 6
Time taken: 0.094 seconds, Fetched: 6 row(s)
hive>
更新:简化版
select i.itm as item, substr(s.number,i.pos+1,1) as number
from
(--replace this subquery with your table
--this is two rows example
select stack(2,'ABC', '123',
'DEF', '456') as (item, number)
) s --your_table
lateral view posexplode(split(s.item,'')) i as pos, itm
where i.itm!='';