题目:
假设现有一张Hive表,数据格式为:
col1 | col2 |
---|---|
a,b,c | 1:00,2:00,3:00 |
d,e,f | 4:00,5:00,6:00 |
… | … |
表中两个字段均为String类型,现在需要将行数据拆分转为多列如下
col1 | col2 |
---|---|
a | 1:00 |
b | 2:00 |
c | 3:00 |
d | 4:00 |
e | 5:00 |
… | … |
该如何实现
解决方法:
select
split(col1,",")[idx] as col1 ,type as col2
from test t
lateral view posexplode(split(col2,",")) t1 as idx, type -- idx为list(split(col2,","))中对应的每个索引,另外一个list根据该索引取值,就能做到一一匹配