ApsaraDB for SelectDB integrates with SeaTunnel, letting you sync data from upstream sources such as MySQL, Hive, and Kafka directly into ApsaraDB for SelectDB using the SelectDB Sink connector.
How it works
SeaTunnel reads data from a source connector, serializes it as JSON or CSV, and writes it to ApsaraDB for SelectDB through the SelectDB Sink. The sink buffers records in memory and flushes them to Object Storage Service (OSS) when the buffer limit is reached. With two-phase commit (2PC) enabled (the default), the sink guarantees exactly-once semantics.
Prerequisites
Before you begin, make sure you have:
SeaTunnel 2.3.1 or later installed
An ApsaraDB for SelectDB instance with a cluster configured
Network access from your SeaTunnel host to the ApsaraDB for SelectDB instance (VPC or public endpoint)
Sink configuration
All sink configurations use the SelectDB (or SelectDBCloud) block. The required parameters and the selectdb.config block differ depending on the data format.
JSON format
sink {
SelectDB {
load-url="<host>:<http_port>"
jdbc-url="<host>:<mysql_port>"
cluster-name="<cluster_name>"
table.identifier="<db_name>.<table_name>"
username="<username>"
password="<password>"
selectdb.config {
file.type="json"
file.strip_outer_array="false"
}
}
}CSV format
sink {
SelectDB {
load-url="<host>:<http_port>"
jdbc-url="<host>:<mysql_port>"
cluster-name="<cluster_name>"
table.identifier="<db_name>.<table_name>"
username="<username>"
password="<password>"
selectdb.config {
file.type="csv"
file.column_separator=","
file.line_delimiter="\n"
}
}
}Parameters
| Parameter | Required | Description |
|---|---|---|
load-url | Yes | The HTTP endpoint of the ApsaraDB for SelectDB instance, in the format <host>:<http_port>. On the Instance Details page, go to Network Information and combine the VPC Endpoint or Public Endpoint with the HTTP Port. Example: selectdb-cn-4xl3jv1****.selectdbfe.rds.aliyuncs.com:8080 |
jdbc-url | Yes | The MySQL endpoint of the ApsaraDB for SelectDB instance, in the format <host>:<mysql_port>. On the same page, combine the VPC Endpoint or Public Endpoint with the MySQL Port. Example: selectdb-cn-4xl3jv1****.selectdbfe.rds.aliyuncs.com:9030 |
cluster-name | Yes | The name of the target cluster within the instance. An instance can contain multiple clusters. |
username | Yes | The username used to connect to the instance. |
password | Yes | The password used to connect to the instance. |
table.identifier | Yes | The target table, in the format <database_name>.<table_name>. Example: test_db.test_table |
selectdb.config | Yes | Data format settings. See the JSON and CSV examples above. |
sink.enable-delete | No | Enables bulk deletion. Only supported for the unique key model. |
sink.buffer-size | No | Maximum buffer size in bytes. Default: 10 MB. When the buffer is full, its contents are flushed to OSS. We recommend that you use the default value. |
sink.buffer-count | No | Maximum number of records held in the buffer. Default: 10,000. When exceeded, the buffer is flushed to OSS. We recommend that you use the default value. |
sink.max-retries | No | Maximum number of retries during the commit phase. Default: 3. |
sink.enable-2pc | No | Enables two-phase commit to guarantee exactly-once semantics. Default: true. |
Example: sync data from MySQL to ApsaraDB for SelectDB
This example walks through syncing an employees table from a MySQL database to ApsaraDB for SelectDB using the SeaTunnel local engine in batch mode.
Environment versions used in this example:
| Component | Version |
|---|---|
| Java Development Kit (JDK) | 1.8 |
| SeaTunnel | 2.3.3 |
| ApsaraDB for SelectDB | 3.0.4 |
Step 1: Set up SeaTunnel
Download and extract the SeaTunnel installation package.
wget https://dlcdn.apache.org/seatunnel/2.3.3/apache-seatunnel-2.3.3-bin.tar.gz tar -xzvf apache-seatunnel-2.3.3-bin.tar.gzEdit
$SEATUNNEL_HOME/config/plugin_configto include only the connectors needed for this example.--connectors-v2-- connector-cdc-mysql connector-selectdb-cloud connector-jdbc connector-fake connector-console connector-assert --end--Install the connector plugins.
sh bin/install-plugin.shDownload the MySQL JDBC driver and place it in the
lib/directory.cd lib/ wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.28/mysql-connector-java-8.0.28.jar
Step 2: Prepare the source data in MySQL
Create the source table in your MySQL database.
CREATE TABLE `employees` ( `emp_no` INT NOT NULL, `birth_date` DATE NOT NULL, `first_name` VARCHAR(14) NOT NULL, `last_name` VARCHAR(16) NOT NULL, `gender` ENUM('M','F') NOT NULL, `hire_date` DATE NOT NULL, PRIMARY KEY (`emp_no`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;Populate the table with test data. You can use Data Management (DMS) to generate test data.
Step 3: Configure ApsaraDB for SelectDB
Connect to the instance over the MySQL protocol, then create the target database and table.
CREATE DATABASE test_db; USE test_db; CREATE TABLE employees ( emp_no INT NOT NULL, birth_date DATE, first_name VARCHAR(20), last_name VARCHAR(20), gender CHAR(2), hire_date DATE ) UNIQUE KEY(`emp_no`) DISTRIBUTED BY HASH(`emp_no`) BUCKETS 1;Apply for a public endpoint so SeaTunnel can reach the instance over the internet.
Add the public IP address of your SeaTunnel host to the IP address whitelist of the instance.
Step 4: Run the sync job
Create the job configuration file
mysqlToSelectDB.conf.env { execution.parallelism = 2 job.mode = "BATCH" checkpoint.interval = 10000 } source { jdbc { url = "jdbc:mysql://<mysql_host>:<mysql_port>/test_db" driver = "com.mysql.cj.jdbc.Driver" user = "<mysql_username>" password = "<mysql_password>" query = "select * from employees" } } sink { SelectDBCloud { load-url="selectdb-cn-pe33hab****-public.selectdbfe.rds.aliyuncs.com:8080" jdbc-url="selectdb-cn-pe33hab****-public.selectdbfe.rds.aliyuncs.com:9030" cluster-name="new_cluster" table.identifier="test_db.employees" username="<selectdb_username>" password="<selectdb_password>" selectdb.config { file.type="json" } } }Replace the placeholder values with your actual connection details. The
load-urlandjdbc-urlvalues are available on the Instance Details page under Network Information.Submit the job.
sh ./bin/seatunnel.sh --config ./mysqlToSelectDB.conf -e localAfter the job completes, query the
test_db.employeestable in ApsaraDB for SelectDB to verify that the data was imported successfully.