UDTF's are not supported outside the SELECT clause, nor nested in expressions (state=42000,code=1008
hive操作时,报错如下:
原因:当使用UDTF函数的时候,hive只允许对拆分字段进行访问。
正确命令:select explode(location) from test_message;
错误命令:select name,explode(location) from test_message;
如果想访问除了拆分字段以外 的字段,怎么办呢?
用lateral view侧视图!
lateral view为侧视图,意义是为了配合UDTF来使用,把某一行数据拆分成多行数据.不加lateral view的UDTF只能提取单个字段拆分,并不能塞会原来数据表中.加上lateral view就可以将拆分的单个字段数据与原始表数据关联上.
注意:在使用lateral view的时候需要指定视图别名和生成的新列别名
--表名 lateral view UDTF(xxx) 视图别名(虚拟表名) as a,b,c
select subview.* from test_message lateral view explode(location) subview;
select subview.* from test_message lateral view explode(location) subview as lc;
--subview为视图别名,lc为指定新列别名
select name,subview.* from test_message lateral view explode(location) subview as lc;
--lateral view explode 相当于一个拆分location字段的虚表,然后与原表进行关联.