Hive函数之explode和inline

explode和inline函数可以将单列扩展成多列或者多行。
1.explode将单列扩展成多行

select explode(subordinates) from employees;

《Hive函数之explode和inline》 image.png

explode的参数可以是array还可以是map,如果是map,将生成2个字段,一个是map的键,字段名为key,一个是map的值,字段的名为:value,如下:

select explode(deductions) from employees ;

《Hive函数之explode和inline》 image.png

explode的限制,如下sql不支持:

  • 不能和其他字段一起使用
select name,explode(subordinates) from employees; 

《Hive函数之explode和inline》 image.png

  • 不支持函数嵌套
select explode(explode(subordinates)) from employees 

《Hive函数之explode和inline》 image.png

  • 不能和group by、sort by 和cluster by一起使用
select explode(subordinates) 
from employees 
group by explode(subordinates)

《Hive函数之explode和inline》 image.png

2.inline
inline的参数形式:inline(ARRAY<STRUCT[,STRUCT]>)
inline一般结合lateral view使用

select t1.col1 as name,t1.col2 as sub1
from employees 
lateral view inline(array(struct(name,subordinates[0]))) t1

《Hive函数之explode和inline》 image.png

inline 嵌套多个struct,

select t1.col1 as name,t1.col2 as sub
from employees 
lateral view inline(array(struct(name,subordinates[0]),
                          struct(name,subordinates[1]))) t1
where t1.col2 is not null

《Hive函数之explode和inline》 image.png

还可以给inline的字段取别名:

select t1.name,t1.sub
from employees 
lateral view inline(array(struct(name,subordinates[0]),
                          struct(name,subordinates[1]))) t1 as name,sub
where t1.sub is not null

《Hive函数之explode和inline》 image.png

    原文作者:pptb
    原文地址: https://www.jianshu.com/p/02c2b8906893
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞