RocketMQ Connect in Action 1

MySQL Source(CDC) - >RocketMQ Connect -> MySQL Sink(JDBC)

Preparation

Start RocketMQ

  1. Linux/Unix/Mac
  2. 64bit JDK 1.8+;
  3. Maven 3.2.x+;
  4. Start RocketMQ;

tips : ${ROCKETMQ_HOME} locational instructions

bin-release.zip version:/rocketmq-all-4.9.4-bin-release

source-release.zip version:/rocketmq-all-4.9.4-source-release/distribution

Start Connect

Compiling Connector Plugin

Debezium RocketMQ Connector

  1. $ cd rocketmq-connect/connectors/rocketmq-connect-debezium/
  2. $ mvn clean package -Dmaven.test.skip=true

Move the compiled Debezium MySQL RocketMQ Connector package into the Runtime loading directory. The command is as follows:

  1. mkdir -p /usr/local/connector-plugins
  2. cp rocketmq-connect-debezium-mysql/target/rocketmq-connect-debezium-mysql-0.0.1-SNAPSHOT-jar-with-dependencies.jar /usr/local/connector-plugins

JDBC Connector

Move the compiled JDBC Connector package into the Runtime loading directory. The command is as follows:

  1. $ cd rocketmq-connect/connectors/rocketmq-connect-jdbc/
  2. $ mvn clean package -Dmaven.test.skip=true
  3. cp rocketmq-connect-jdbc/target/rocketmq-connect-jdbc-0.0.1-SNAPSHOT-jar-with-dependencies.jar /usr/local/connector-plugins

Start Connect Runtime

  1. cd rocketmq-connect
  2. mvn -Prelease-connect -DskipTests clean install -U

Modify the configuration connect-standalone.conf, the main configuration is as follows

  1. $ cd distribution/target/rocketmq-connect-0.0.1-SNAPSHOT/rocketmq-connect-0.0.1-SNAPSHOT
  2. $ vim conf/connect-standalone.conf
  1. workerId=standalone-worker
  2. storePathRootDir=/tmp/storeRoot
  3. ## Http port for user to access REST API
  4. httpPort=8082
  5. # Rocketmq namesrvAddr
  6. namesrvAddr=localhost:9876
  7. # RocketMQ acl
  8. aclEnable=false
  9. accessKey=rocketmq
  10. secretKey=12345678
  11. autoCreateGroupEnable=false
  12. clusterName="DefaultCluster"
  13. # Core configuration, configure the plugin directory of the previously compiled debezium package here
  14. # Source or sink connector jar file dir,The default value is rocketmq-connect-sample
  15. pluginPaths=/usr/local/connector-plugins
  1. cd distribution/target/rocketmq-connect-0.0.1-SNAPSHOT/rocketmq-connect-0.0.1-SNAPSHOT
  2. sh bin/connect-standalone.sh -c conf/connect-standalone.conf &

MySQL image

Use debezium’s MySQL docker environment to set up the MySQL database

  1. docker run -it --rm --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=debezium -e MYSQL_USER=mysqluser -e MYSQL_PASSWORD=mysqlpw quay.io/debezium/example-mysql:1.9

MySQL information

Port:3306

Account:root/debezium

slave:debezium/dbz

Test data

Log in to the database with the root/debezium account

Source database table:inventory.employee

  1. CREATE database inventory;
  2. use inventory;
  3. CREATE TABLE `employee` (
  4. `id` bigint NOT NULL AUTO_INCREMENT,
  5. `name` varchar(128) DEFAULT NULL,
  6. `howold` int DEFAULT NULL,
  7. `male` int DEFAULT NULL,
  8. `company` varchar(128) DEFAULT NULL,
  9. `money` double DEFAULT NULL,
  10. `begin_time` datetime DEFAULT NULL,
  11. `modify_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'modify time',
  12. `decimal_test` decimal(11,2) DEFAULT NULL COMMENT 'test decimal type',
  13. PRIMARY KEY (`id`)
  14. ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;
  15. INSERT INTO `employee` VALUES (1, 'name-01', 24, 6, 'company', 9987, '2021-12-22 08:00:00', '2022-06-14 18:20:11', 321.11);
  16. INSERT INTO `employee` VALUES (2, 'name-02', 19, 7, 'company', 32232, '2021-12-29 08:00:00', '2022-06-14 18:18:47', 77.12);
  17. INSERT INTO `employee` VALUES (8, 'name-03', 20, 1, NULL, 0, NULL, '2022-06-14 18:26:05', 11111.00);
  18. INSERT INTO `employee` VALUES (9, 'name-04', 21, 1, 'company', 12345, '2021-12-24 20:44:10', '2022-06-14 18:20:02', 123.12);
  19. INSERT INTO `employee` VALUES (11, 'name-05', 50, 2, 'company', 33333, '2021-12-24 22:14:52', '2022-06-14 18:19:58', 123.12);
  20. INSERT INTO `employee` VALUES (12, 'name-06', 19, 3, NULL, 0, NULL, '2022-06-14 18:26:12', 111233.00);
  21. INSERT INTO `employee` VALUES (13, 'name-07', 20, 4, 'company', 3237, '2021-12-29 01:31:03', '2022-06-14 18:19:27', 52.00);
  22. INSERT INTO `employee` VALUES (14, 'name-08', 25, 15, 'company', 32255, '2022-02-08 19:06:39', '2022-06-14 18:18:32', 0.00);
  23. INSERT INTO `employee` VALUES (15, NULL, 0, 0, NULL, 0, NULL, '2022-06-14 20:13:29', NULL);

Target database:inventory_2.employee

  1. CREATE database inventory_2;
  2. use inventory_2;
  3. CREATE TABLE `employee` (
  4. `id` bigint NOT NULL AUTO_INCREMENT,
  5. `name` varchar(128) DEFAULT NULL,
  6. `howold` int DEFAULT NULL,
  7. `male` int DEFAULT NULL,
  8. `company` varchar(128) DEFAULT NULL,
  9. `money` double DEFAULT NULL,
  10. `begin_time` datetime DEFAULT NULL,
  11. `modify_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  12. `decimal_test` decimal(11,2) DEFAULT NULL COMMENT 'test decimal type',
  13. PRIMARY KEY (`id`)
  14. ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;

Start Connector

Start Debezium source connector

Synchronize original table data:inventory.employee Purpose: Parse MySQL binlog and encapsulate into a generic ConnectRecord object and send to RocketMQ Topic.

  1. curl -X POST -H "Content-Type: application/json" http://127.0.0.1:8082/connectors/MySQLCDCSource -d '{
  2. "connector.class": "org.apache.rocketmq.connect.debezium.mysql.DebeziumMysqlConnector",
  3. "max.task": "1",
  4. "connect.topicname": "debezium-mysql-source-topic",
  5. "kafka.transforms": "Unwrap",
  6. "kafka.transforms.Unwrap.delete.handling.mode": "none",
  7. "kafka.transforms.Unwrap.type": "io.debezium.transforms.ExtractNewRecordState",
  8. "kafka.transforms.Unwrap.add.headers": "op,source.db,source.table",
  9. "database.history.skip.unparseable.ddl": true,
  10. "database.history.name.srv.addr": "localhost:9876",
  11. "database.history.rocketmq.topic": "db-history-debezium-topic",
  12. "database.history.store.only.monitored.tables.ddl": true,
  13. "include.schema.changes": false,
  14. "database.server.name": "dbserver1",
  15. "database.port": 3306,
  16. "database.hostname": "database ip",
  17. "database.connectionTimeZone": "UTC",
  18. "database.user": "debezium",
  19. "database.password": "dbz",
  20. "table.include.list": "inventory.employee",
  21. "max.batch.size": 50,
  22. "database.include.list": "inventory",
  23. "snapshot.mode": "when_needed",
  24. "database.server.id": "184054",
  25. "key.converter": "org.apache.rocketmq.connect.runtime.converter.record.json.JsonConverter",
  26. "value.converter": "org.apache.rocketmq.connect.runtime.converter.record.json.JsonConverter"
  27. }'

Start JDBC sink connector

Purpose: Consume data from the Topic and write to the destination table through the JDBC protocol.

  1. curl -X POST -H "Content-Type: application/json" http://127.0.0.1:8082/connectors/jdbcmysqlsinktest -d '{
  2. "connector.class": "org.apache.rocketmq.connect.jdbc.connector.JdbcSinkConnector",
  3. "max.task": "2",
  4. "connect.topicnames": "debezium-mysql-source",
  5. "connection.url": "jdbc:mysql://database ip:3306/inventory_2",
  6. "connection.user": "root",
  7. "connection.password": "debezium",
  8. "pk.fields": "id",
  9. "table.name.from.header": "true",
  10. "pk.mode": "record_key",
  11. "insert.mode": "UPSERT",
  12. "db.timezone": "UTC",
  13. "table.types": "TABLE",
  14. "errors.deadletterqueue.topic.name": "dlq-topic",
  15. "errors.log.enable": "true",
  16. "errors.tolerance": "ALL",
  17. "delete.enabled": "true",
  18. "key.converter": "org.apache.rocketmq.connect.runtime.converter.record.json.JsonConverter",
  19. "value.converter": "org.apache.rocketmq.connect.runtime.converter.record.json.JsonConverter"
  20. }'

After the above two Connector tasks are successfully created, log in to the database with the root/debezium account.

Insert, delete or update data to the source database table: inventory.employee, then the data will be synchronized to the destination table inventory_2.employee.