hive里通常通过collect_set和collect_list来进行列转行,其中collect_list为不去重转换,collect_set为去重转换。
创建一个测试表
CREATE table stu_score(
stu_id string comment '学号',
stu_name string comment '姓名',
course string comment '科目',
score string comment '分数'
) comment '学生成绩表';
数据如下:

列转行实现:
select
stu_id,
stu_name,
concat_ws(’,’,collect_set(course)) as course,
concat_ws(’,’,collect_set(score)) as score
from stu_score
group by stu_id, stu_name;
结果:

本文介绍在Hive中如何使用collect_set和collect_list函数进行列转行操作,通过实例展示了如何将多个列的数据合并成一行,适用于学生成绩表等场景的数据整理。

6万+

被折叠的 条评论
为什么被折叠?



