bootstrap引导全量/增量历史数据.
准备工作:mysql插入历史数据。
CREATE TABLE `emp` (
`id` int(11) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
`deg` varchar(100) DEFAULT NULL,
`salary` int(11) DEFAULT NULL,
`dept` varchar(10) DEFAULT NULL,
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`is_delete` bigint(20) DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
insert into `emp`(`id`,`name`,`deg`,`salary`,`dept`,`create_time`,`update_time`,`is_delete`) values (1201,'gopal','manager',50000,'TP','2018-06-17 18:54:32','2019-01-17 11:19:32',1),(1202,'manishahello','Proof reader',50000,'TPP','2018-06-15 18:54:32','2018-06-17 18:54:32',0),(1203,'khalillskjds','php dev',30000,'AC','2018-06-17 18:54:32','2019-03-14 09:18:27',1),(1204,'prasanth_xxx','php dev',30000,'AC','2018-06-17 18:54:32','2019-04-07 09:09:24',1),(1205,'kranthixxx','admin',20000,'TP','2018-06-17 18:54:32','2018-12-08 11:50:33',0),(1206,'garry','manager',50000,'TPC','2018-12-10 21:41:09','2018-12-10 21:41:09',1),(1207,'oliver','php dev',2000,'AC','2018-12-15 13:49:13','2018-12-15 13:49:13',1),(1208,'hello','phpDev',200,'TP','2018-12-16 09:41:48','2018-12-16 09:41:48',1),(1209,'ABC','HELLO',300,NULL,'2018-12-16 09:42:04','2018-12-16 09:42:24',1),(1210,'HELLO','HELLO',5800,'TP','2019-01-24 09:02:43','2019-01-24 09:02:43',1),(1211,'WORLD','TEST',8800,'AC','2019-01-24 09:03:15','2019-01-24 09:03:15',1),(1212,'sdfs','sdfsdf',8500,'AC','2019-03-13 22:01:38','2019-03-13 22:01:38',1),(1213,NULL,'sdfsdf',9800,'sdfsdf','2019-03-14 09:08:31','2019-03-14 09:08:54',1),(1214,'xxx','sdfsdf',9500,NULL,'2019-03-14 09:13:32','2019-03-14 09:13:44',0),(1215,'sdfsf','sdfsdfsdf',9870,'TP','2019-04-07 09:10:39','2019-04-07 09:11:18',0),(1216,'hello','HELLO',5600,'AC','2019-04-07 09:37:05','2019-04-07 09:37:05',1),(1217,'HELLO2','hello2',7800,'TP','2019-04-07 09:37:40','2019-04-07 09:38:17',1);
maxwell提供了引导类的工具。也就是可以进行初始化表的操作。我们可以通过maxwell-bootstrap来对表进行全量拉取操作。
1.首先启动maxwell.
脚本如下:
vim maxwell.sh
#!/bin/bash
/home/hadoop/app/maxwell-1.22.1/bin/maxwell --user='maxwell' \
--password='maxwell%123' \
--host='192.168.72.111'
--producer=kafka \
--kafka.bootstrap.servers='192.168.72.112:6667' \
--kafka_topic=maxwell_kafka \
--log_level=debug \
--kafka_partition_hash=murmur3 \
--kafka_key_format=hash \
--kafka.compression.type=snappy \
--kafka.retries=5 \
--kafka.acks=all \
--producer_partition_by=primary_key \
--bootstrapper=async \
#--filter='exclude: *.*, include: maxwell.emp'
启动maxwell:
2.进行引导表sql操作。
在mysql数据库: database: maxwell -> bootstrap 表 记录了每次需要引导操作的记录。我们只需要往该表中增加需要初始化的动作即可.
这里初始化2019年到2020年的历史数据:
INSERT INTO maxwell.bootstrap (database_name, table_name,where_clause) VALUES ('testdb', 'emp','create_time <= \'2020-01-01 00:00:00\' and create_time >=\'2019-01-01 00:00:00\'');
3.查看指定topic的数据是否存在。
type类型为bootstrap-insert,即是我们全量拉取到的历史数据。
注意这里的 type 是
bootstrap-insert
,而不是 insert
。
Bootstrap 的过程是 bootstrap-start -> bootstrap-insert -> bootstrap-complete
,其中,start和complete的data字段为空,不携带数据。
--bootstrapper=sync
时,在处理bootstrap时,会阻塞正常的binlog解析;--bootstrapper=async
时,不会阻塞。
maxwell实时同步,进行增删改操作
注意:不能更新maxwell所在库下的表。
实时插入:
insert into emp (id,name,deg,salary,dept,create_time,update_time,is_delete)values(1218,'zhanshang','hello3',500,'TP',now(),now(),0);
修改:
update emp set name='whirly' where id=1201;
删除:
delete from emp where id=1201;