hive的表生成函数
explode函数
explode(col) : 将hive中array、map结构拆分成多行
explode(ARRAY) : 将数组的每一个元素都生成一行
explode(AMP) : 将map的每一个K-V对都生成一行,k为一行,v为一行
准备数据
1 jx|zx|yx
2 wj|wz|yy
3 wx|wx|yx
建表
create table emp(
id int,
name array<string>
)
row format delimited fields terminated by '\t'
collection items terminated by '|';
加载数据
load data inpath '/data/data.txt' into table emp;
加载完数据就可以查询到数据了
select * from emp;
使用explode进行查询
select explode(name) name from emp;
LATERAL VIEW
能够将一列数据拆分成多行,可以对拆分之后的数据进行聚合
hive (default)> select id, n from emp lateral view explode(name) tmp_name as n;
OK
id n
1 jx
1 zx
1 yx
2 wj
2 wz
2 yy
3 wx
3 wx
3 yx
Reflect
reflet函数支持在sql中调用java自带的函数
准备数据
1 3
2 1
6 3
create table tmp1(col1 int,col2 int) row format delimited fields terminated by '\t';
load data inpath '/data/data.txt' into table tmp1;
调用函数
hive (default)> select reflect ("java.lang.Math", "max", col1, col2) from tmp1;
3
2
6