一、全量加载
开始前先创建两个数据表,full_sourse和full_target两表,具体内容如下:
full_sourse
create table `full_source` (
`no` int (10),
`book_name` varchar (60),
`author` varchar (30),
`tag` varchar (60)
);
insert into `full_source` (`no`, `book_name`, `author`, `tag`) values('1','小王子','圣艾克苏佩里','外国文学');
insert into `full_source` (`no`, `book_name`, `author`, `tag`) values('2','解忧杂货店','东野圭吾','小说');
insert into `full_source` (`no`, `book_name`, `author`, `tag`) values('3','白夜行','东野圭吾','小说');
insert into `full_source` (`no`, `book_name`, `author`, `tag`) values('4','梦里花落知多少','郭敬明','小说');
insert into `full_source` (`no`, `book_name`, `author`, `tag`) values('5','看见','柴静','随笔');
insert into `full_source` (`no`, `book_name`, `author`, `tag`) values('6','围城','钱钟书','小说');
full_target
create table `full_target` (
`no` int (10),
`book_name` varchar (60),
`author` varchar (30),
`tag` varchar (60)
);
insert into `full_target` (`no`, `book_name`, `author`, `tag`) values('1','小王子','圣艾克苏佩里','外国文学');
insert into `full_target` (`no`, `book_name`, `author`, `tag`) values('2','解忧杂货店','东野圭吾','小说');
insert into `full_target` (`no`, `book_name`, `author`, `tag`) values('5','看见','柴静','随笔');
1、创建转换
打开kettle,创建转换如图1-1所示。
图1-1 创建转换
2、配置控件
配置“执行SQL”脚本控件。首先通过新建,连接数据库,可用“测试”检测是否连接成功,然后再SQL数据框中编写删除数据库full_target,具体配置如图1-2、图1-3所示。
图1-2 连接数据库
图1-3 配置“执行SQL脚本”控件
点击确定,继续配置下一个。
配置“表输入”配件,先连接数据库,如图1-4所示。
图1-4 配置“表输入”配件
配置“表输出”配件,连接数据库,如图1-5所示。
图1-5 配置“表输出”配件
3、执行结果
运行此转换,结果如图1-6所示。
图1-6 执行结果
二、增量加载
开始前先创建两个数据表,incremental_source和incremental_target_copy两表,具体内容如下:
incremental_source
create table `incremental_source` (
`id` int (20),
`name` varchar (60),
`age` int (20),
`create_time` datetime
);
insert into `incremental_source` (`id`, `name`, `age`, `create_time`) values('1','Isabella','18','2019-08-20 13:14:20');
insert into `incremental_source` (`id`, `name`, `age`, `create_time`) values('2','Jack','20','2019-08-21 13:14:21');
insert into `incremental_source` (`id`, `name`, `age`, `create_time`) values('3','Nicholas','22','2019-08-20 13:14:22');
insert into `incremental_source` (`id`, `name`, `age`, `create_time`) values('4','Jasmine','19','2019-08-20 13:14:23');
insert into `incremental_source` (`id`, `name`, `age`, `create_time`) values('5','Mia','20','2019-08-20 13:14:24');
incremental_target_copy
reate table `incremental_target_copy` (
`id` int (20),
`name` varchar (60),
`age` int (20),
`create_time` datetime
);
insert into `incremental_target_copy` (`id`, `name`, `age`, `create_time`) values('1','Isabella','18','2019-08-20 13:14:20');
insert into `incremental_target_copy` (`id`, `name`, `age`, `create_time`) values('2','Jack','20','2019-08-21 13:14:21');
insert into `incremental_target_copy` (`id`, `name`, `age`, `create_time`) values('3','Nicholas','22','2019-08-20 13:14:22');
insert into `incremental_target_copy` (`id`, `name`, `age`, `create_time`) values('4','Jasmine','19','2019-08-20 13:14:23');
insert into `incremental_target_copy` (`id`, `name`, `age`, `create_time`) values('5','Mia','20','2019-08-20 13:14:24');
1、创建转换
创建转换如图2-1所示。
2、配置控件
配置“表输入”控件,首先连接数据库,具体配置如2-2、图2-3所示。
图2-2 数据库连接
图2-3 配置“表输入”控件
预览数据:
图2-4 预览数据
配置“插入/更新”控件,要连数据库的别忘了。
图2-5 配置“插入/更新”控件
修改数据表incremental_source,增加一条新的数据:
图2-6 数据库增加数据
3、运行结果
运行,结果如下图:
图2-7 执行结果
上两题为《数据清洗》第七章的课后操作题。