lateral view的语 法:
lateralView: LATERAL VIEWudtf(expression) tableAlias AScolumnAlias (','columnAlias)
fromClause: FROM baseTable (lateralView)
描述:
lateralview用于和split、explode等UDTF一起使用的,能将一行数据拆分成多行数据,在此基础上可以对拆分的数据进行聚合,lateralview首先为原始表的每行调用UDTF,UDTF会把一行拆分成一行或者多行,lateralview在把结果组合,产生一个支持别名表的虚拟表。
例子
1、一个lateral view的例子
create table tbl_split_test(dateid string,name string,typestring) ROW FORMAT DELIMITED FILEDS TERMINATED BY 't';
load data local inpath '/home/sun/test.txt' overwrite into tabletbl_split_test;
select dateid ,name,cc from tbl_split_test lateral viewexplode(split(type,"//|")) tbl_split_test as ccwhere cc=4;
结果:
执行过程是先执行from到 ascloumn的列过程,在执行select 和where后边的语句
2、多个lateral view
select dateid,aa,bb from tbl_split_test2
lateral view explode(split(name,"//|")) tbl1 as aa
lateral view explode(split(type,"//|")) tbl2 as bb
where aa='rr' and bb=2;
这里的执行顺序按照lateralview的出现顺序去执行。
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+LateralView