一、 示例脚本:
# 创建表
create table score(
id bigint(20) auto_increment,
sname varchar(255) comment "学生姓名",
cname varchar(255) comment "课程名称",
score int(11) comment "分数",
primary key(id)
)engine=innodb;
# 插入数据
insert into score values(1,"小孙","mysql",55);
insert into score values(2,"小孙","python",66);
insert into score values(3,"小孙","java",47);
insert into score values(4,"小郭","mysql",70);
insert into score values(5,"小郭","python",85);
insert into score values(6,"小林","java",67);
insert into score values(7,"小林","mysql",77);
二、 实现目的
本文主要实现列转行的转换方式,对于数据维度很大的表使用kettle组件来实现列转行比较方便。
三、实现流程
- 表输入组件
将刚刚插入表中的数据抽取到工作流
- 列转行组件
关键字段:用来查找关键字值的字段。
分组字段:以哪个字段来进行分组。
目标字段:建立一个字段用来接收数据。
数据字段:接收哪个字段的数据。
关键字值:用来查找关键字值(你输入的)。
- 字段选择
点击获取字段,可以对字段取舍。
我觉得这是一个骚操作(往下看)。
点击字段选择
组件,并点击小眼睛一样的按钮,选择快速启动
,最后呈现的结果就可以对数据进行预览。
如果任务出现了BUG,我们既可以查看日志来进行排查,也可以预览每个组件对数据处理过的结果集。
- Excel输出
选择保存的Excel文件,并保存启动这个转换任务。
5. 结果集
最后Excel中的数据就是列转行之后的结果。
HIVE行转列
select t.name
,t.kv ["age"] as age
,t.kv ["sex"] as age
from (
select name
,str_to_map(
concat_ws(
","
,collect_set(concat(index_code ,"-" ,index_value))
)
,","
,"-"
) kv
from (
-- 数据集
select "张三" as name
,"age" as index_code
,"15" as index_value
union all
select "张三" as name
,"sex" as index_code
,"男" as index_value
union all
select "李四" as name
,"age" as index_code
,"18" as index_value
union all
select "李四" as name
,"sex" as index_code
,"女" as index_value
) a
group by a.name
) t;
相关内容
ETL工具Kettle的发送邮件功能:
https://blog.csdn.net/weixin_43932609/article/details/108766325
ETL工具Kettle性能优化:
https://blog.csdn.net/weixin_43932609/article/details/108749304
=========================================================
人生得意须尽欢,莫使金樽空对月!
__一个热爱说唱的程序员。
=========================================================