Hive将一行记录拆分成多行或者多列

1、拆分为多行 --> 数据为map,json

模拟map: 
with temp2 as  (select  '2' as id ,map('a1','aa','b1','cc') as jsonstring)
select m.id, tf.key, tf.value from temp2 m lateral view explode(m.jsonstring) tf as key,value;


str_to_map 函数 
with temp1 as  (select  '1' as id ,str_to_map('a1:123,b1:234',',',':')   as jsonstring )

select m.id, tf.key, tf.value , '1' as json_lv from temp1 m lateral view explode(m.jsonstring) tf as key,value
select m.name, tf.key, tf.value from testMap m lateral view explode(m.info) tf as key,value;

2、拆分为多行 --> 数据为数组

select name,ss.ssmc from testArray lateral view explode(testArray.ssmc) ss as ssmc;

3、拆分为多行 -->数据为String

select name,ss.ssmc from testStr lateral view explode(split(ssmc,"\\|")) ss as ssmc;

4. 拆分为多列 -->数据为Array

select name,ssmc[0] as ssmc1,ssmc[1] as ssmc2,ssmc[2] as ssmc3 from testArray;

5.  拆分为多列 -->数据为String

select t.name,t.ssmcs[0] as ssmc1,t.ssmcs[1] as ssmc2,t.ssmcs[2] as ssmc3 from (
 
select name,split(ssmc,"\\|") as ssmcs from testArray) t

如果值是null 或者 空, 结果中没有这条数据 

猜你喜欢

转载自blog.csdn.net/zhuchunyan_aijia/article/details/117292020