CDC Connectors for Apache Flink® is a set of source connectors for Apache Flink®, ingesting changes from different databases using change data capture (CDC). CDC Connectors for Apache Flink® integrates Debezium as the engine to capture data changes. So it can fully leverage the ability of Debezium. See more about what is Debezium.
This README is meant as a brief walkthrough on the core features of CDC Connectors for Apache Flink®. For a fully detailed documentation, please see Documentation.
Connector | Database | Driver |
---|---|---|
mongodb-cdc | MongoDB Driver: 4.3.4 | |
mysql-cdc | JDBC Driver: 8.0.28 | |
oceanbase-cdc | OceanBase Driver: 2.4.x | |
oracle-cdc | Oracle Driver: 19.3.0.0 | |
postgres-cdc | JDBC Driver: 42.5.1 | |
sqlserver-cdc | JDBC Driver: 9.4.1.jre8 | |
tidb-cdc | JDBC Driver: 8.0.27 | |
Db2-cdc | Db2 Driver: 11.5.0.0 | |
Vitess-cdc | MySql JDBC Driver: 8.0.26 |
- Supports reading database snapshot and continues to read transaction logs with exactly-once processing even failures happen.
- CDC connectors for DataStream API, users can consume changes on multiple databases and tables in a single job without Debezium and Kafka deployed.
- CDC connectors for Table/SQL API, users can use SQL DDL to create a CDC source to monitor changes on a single table.
The example shows how to continuously synchronize data, including snapshot data and incremental data, from multiple business tables in MySQL database to Doris for creating the ODS layer.
- Download and extract the flink-cdc-3.0.tar file to a local directory.
- Download the required CDC Pipeline Connector JAR from Maven and place it in the lib directory.
- Configure the FLINK_HOME environment variable to load the Flink cluster configuration from the flink-conf.yaml file located in the $FLINK_HOME/conf directory.
export FLINK_HOME=/path/to/your/flink/home
- Write Flink CDC task YAML.
source:
type: mysql
host: localhost
port: 3306
username: admin
password: pass
tables: db0.commodity, db1.user_table_[0-9]+, [app|web]_order_\.*
sink:
type: doris
fenodes: FE_IP:HTTP_PORT
username: admin
password: pass
pipeline:
name: mysql-sync-doris
parallelism: 4
- Submit the job to Flink cluster.
# Submit Pipeline
$ ./bin/flink-cdc.sh mysql-to-doris.yaml
Pipeline "mysql-sync-doris" is submitted with Job ID "DEADBEEF".
During the execution of the flink-cdc.sh script, the CDC task configuration is parsed and translated into a DataStream job, which is then submitted to the specified Flink cluster.
We need several steps to setup a Flink cluster with the provided connector.
-
Setup a Flink cluster with version 1.14+ and Java 8+ installed.
-
Download the connector SQL jars from the Download page (or build yourself).
-
Put the downloaded jars under
FLINK_HOME/lib/
. -
Restart the Flink cluster.
The example shows how to create a MySQL CDC source in Flink SQL Client and execute queries on it.
-- creates a mysql cdc table source
CREATE TABLE mysql_binlog (
id INT NOT NULL,
name STRING,
description STRING,
weight DECIMAL(10,3)
) WITH (
'connector' = 'mysql-cdc',
'hostname' = 'localhost',
'port' = '3306',
'username' = 'flinkuser',
'password' = 'flinkpw',
'database-name' = 'inventory',
'table-name' = 'products'
);
-- read snapshot and binlog data from mysql, and do some transformation, and show on the client
SELECT id, UPPER(name), description, weight FROM mysql_binlog;
Include following Maven dependency (available through Maven Central):
<dependency>
<groupId>com.ververica</groupId>
<!-- add the dependency matching your database -->
<artifactId>flink-connector-mysql-cdc</artifactId>
<!-- The dependency is available only for stable releases, SNAPSHOT dependencies need to be built based on master or release- branches by yourself. -->
<version>2.5-SNAPSHOT</version>
</dependency>
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import com.ververica.cdc.debezium.JsonDebeziumDeserializationSchema;
import com.ververica.cdc.connectors.mysql.source.MySqlSource;
public class MySqlSourceExample {
public static void main(String[] args) throws Exception {
MySqlSource<String> mySqlSource = MySqlSource.<String>builder()
.hostname("yourHostname")
.port(yourPort)
.databaseList("yourDatabaseName") // set captured database
.tableList("yourDatabaseName.yourTableName") // set captured table
.username("yourUsername")
.password("yourPassword")
.deserializer(new JsonDebeziumDeserializationSchema()) // converts SourceRecord to JSON String
.build();
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
// enable checkpoint
env.enableCheckpointing(3000);
env
.fromSource(mySqlSource, WatermarkStrategy.noWatermarks(), "MySQL Source")
// set 4 parallel source tasks
.setParallelism(4)
.print().setParallelism(1); // use parallelism 1 for sink to keep message ordering
env.execute("Print MySQL Snapshot + Binlog");
}
}
- Prerequisites:
- git
- Maven
- At least Java 8
git clone https://github.com/apache/flink-cdc.git
cd flink-cdc-connectors
mvn clean install -DskipTests
The dependencies are now available in your local .m2
repository.
The code in this repository is licensed under the Apache Software License 2.
CDC Connectors for Apache Flink® welcomes anyone that wants to help out in any way, whether that includes reporting problems, helping with documentation, or contributing code changes to fix bugs, add tests, or implement new features. You can report problems to request features in the GitHub Issues.
- Left comment under the issue that you want to take
- Fork Flink CDC project to your GitHub repositories
- Clone and compile your Flink CDC project
git clone https://github.com/your_name/flink-cdc-connectors.git
cd flink-cdc-connectors
mvn clean install -DskipTests
- Check to a new branch and start your work
git checkout -b my_feature
-- develop and commit
- Push your branch to your github
git push origin my_feature
- Open a PR to https://github.com/apache/flink-cdc
You need to install the google-java-format plugin. Spotless together with google-java-format is used to format the codes.
It is recommended to automatically format your code by applying the following settings:
- Go to "Settings" → "Other Settings" → "google-java-format Settings".
- Tick the checkbox to enable the plugin.
- Change the code style to "Android Open Source Project (AOSP) style".
- Go to "Settings" → "Tools" → "Actions on Save".
- Under "Formatting Actions", select "Optimize imports" and "Reformat file".
- From the "All file types list" next to "Reformat code", select "Java".
For earlier IntelliJ IDEA versions, the step 4 to 7 will be changed as follows.
- 4.Go to "Settings" → "Other Settings" → "Save Actions".
- 5.Under "General", enable your preferred settings for when to format the code, e.g. "Activate save actions on save".
- 6.Under "Formatting Actions", select "Optimize imports" and "Reformat file".
- 7.Under "File Path Inclusions", add an entry for
.*\.java
to avoid formatting other file types. Then the whole project could be formatted by commandmvn spotless:apply
.
Checkstyle is used to enforce static coding guidelines.
- Go to "Settings" → "Tools" → "Checkstyle".
- Set "Scan Scope" to "Only Java sources (including tests)".
- For "Checkstyle Version" select "8.14".
- Under "Configuration File" click the "+" icon to add a new configuration.
- Set "Description" to "Flink cdc".
- Select "Use a local Checkstyle file" and link it to the file
tools/maven/checkstyle.xml
which is located within your cloned repository. - Select "Store relative to project location" and click "Next".
- Configure the property
checkstyle.suppressions.file
with the valuesuppressions.xml
and click "Next". - Click "Finish".
- Select "Flink cdc" as the only active configuration file and click "Apply".
You can now import the Checkstyle configuration for the Java code formatter.
- Go to "Settings" → "Editor" → "Code Style" → "Java".
- Click the gear icon next to "Scheme" and select "Import Scheme" → "Checkstyle Configuration".
- Navigate to and select
tools/maven/checkstyle.xml
located within your cloned repository.
Then you could click "View" → "Tool Windows" → "Checkstyle" and find the "Check Module" button in the opened tool window to validate checkstyle. Or you can use the command mvn clean compile checkstyle:checkstyle
to validate.
Flink cdc documentations locates at docs/content
.
The contribution step is the same as the code contribution. We use markdown as the source code of the document.
-
DingTalk Chinese User Group
You can search the group number [33121212] or scan the following QR code to join in the group.
To get started, please see https://apache.github.io/flink-cdc/