hive一行变多行及多行变一行
场景
name | alias |
---|---|
zhaoqiansun | abc def ghi |
处理数据时需要将上表处理成为下面的形式:
name | alias |
---|---|
zhaoqiansun | abc |
zhaoqiansun | def |
zhaoqiansun | ghi |
办法
- 使用
Lateral view
和explode()
来实现,命令如下:
hive> select name, single_alias from test lateral view explode(split(alias, ' ')) test_alias as single_alias;
-
explode()
是将array数据分解为单独行的函数,explode([1, 2, 3]) 处理后就变成了:1 2 2 -
split是将string按照指定字符分解的函数返回结果为array
-
lateral view的功能是将某数据作为一个虚拟的column
多行变一行,逆操作
列转为行演示:
hive (hive)> select name,concat_ws(',',collect_set(subject)) from student group by name;
huahua chinese,math
xiaoming english,chinese,mathhive (hive)> select name,concat_ws(',',collect_set(concat(subject,'=',score))) from student group by name;
huahua chinese=80,math=89.5
xiaoming english=92,chinese=98,math=89.5
如果不使用 concat_ws(连接函数)是这样的:
hive> select * from test;
OK
zhao 15 20170807
zhao 14 20170809
hive> select name, collect_set(age) from test group by name;
zhao [15,14]