Using SeaTunnel to import data
SeaTunnel integrates with ApsaraDB for SelectDB, allowing you to use the SeaTunnel SelectDB Sink to import table data into ApsaraDB for SelectDB.
Overview
SeaTunnel is a distributed data integration platform that supports real-time synchronization of large-scale data. It can read data from sources such as MySQL, Hive, and Kafka, and write the data to ApsaraDB for SelectDB.
Prerequisites
SeaTunnel 2.3.1 or later.
Usage
SeaTunnel supports writing upstream data to ApsaraDB for SelectDB in JSON or CSV format. The configuration syntax for each format is shown below.
JSON format
sink {
SelectDB {
load-url="ip:http_port"
jdbc-url="ip:mysql_port"
cluster-name="Cluster"
table.identifier="test_db.test_table"
username="admin"
password="****"
selectdb.config {
file.type="json"
}
}
}
CSV format
sink {
SelectDB {
load-url="ip:http_port"
jdbc-url="ip:mysql_port"
cluster-name="Cluster"
table.identifier="test_db.test_table"
username="admin"
password="****"
selectdb.config {
file.type="csv"
file.column_separator=","
file.line_delimiter="\n"
}
}
}
The following table describes the parameters.
|
Parameter |
Required |
Description |
|
load-url |
Yes |
The endpoint and ApsaraDB for SelectDB of the ApsaraDB for SelectDB instance. In the ApsaraDB for SelectDB console, find the Instance Details (or Network Information) and VPC Endpoint on the Public Endpoint > HTTP Port page. Example: |
|
jdbc-url |
Yes |
The endpoint and ApsaraDB for SelectDB of the ApsaraDB for SelectDB instance. You can obtain the VPC Endpoint (or Public Endpoint) and MySQL Port from the Instance Details > Network Information section of the ApsaraDB for SelectDB console. Example: |
|
cluster-name |
Yes |
The name of the ApsaraDB for SelectDB in the ApsaraDB for SelectDB instance. If an instance contains multiple clusters, specify the one that you want to use. |
|
username |
Yes |
The username for your ApsaraDB for SelectDB instance. |
|
password |
Yes |
The password for the username of the ApsaraDB for SelectDB instance. |
|
table.identifier |
Yes |
The name of the destination table in the ApsaraDB for SelectDB instance. The format is |
|
selectdb.config |
Yes |
The properties of the write task:
|
|
sink.enable-delete |
No |
Enables or disables bulk deletion. Supported only for tables that use the Unique Key model. |
|
sink.buffer-size |
No |
The maximum size of the buffer, in bytes. Default value: 10 MB. When the buffer exceeds this size, its contents are flushed to Object Storage Service (OSS). Do not change this value unless necessary. |
|
sink.buffer-count |
No |
The maximum number of records in the buffer. Default value: 10,000. When the buffer exceeds this count, its contents are flushed to OSS. Do not change this value unless necessary. |
|
sink.max-retries |
No |
The maximum number of retries for the commit phase. Default value: 3. |
|
sink.enable-2pc |
No |
Enables two-phase commit to ensure exactly-once semantics. Default value: true. |
Example
The following example imports data from MySQL into ApsaraDB for SelectDB by using SeaTunnel. It uses the following software versions:
|
Component |
Version |
|
JDK |
1.8 |
|
SeaTunnel |
2.3.3 |
|
SelectDB |
3.0.4 |
Prepare the environment
-
Set up the SeaTunnel environment.
-
Download and extract the SeaTunnel installation package,
apache-seatunnel-2.3.3-bin.tar.gz.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.gz -
Edit the SEATUNNEL_HOME/config/plugin_config file to keep only the required connector plugins.
--connectors-v2-- connector-cdc-mysql connector-selectdb-cloud connector-jdbc connector-fake connector-console connector-assert --end-- -
Install the SeaTunnel connector plugins.
sh bin/install-plugin.sh -
Download the MySQL driver and move it to the SEATUNNEL_HOME/lib/ directory.
cd lib/ wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.28/mysql-connector-java-8.0.28.jar
-
-
Prepare the data to import. This example uses a small sample dataset from MySQL.
-
Create a test table in MySQL.
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 -
To build test data by using DMS, see Building test data.
-
-
Configure the ApsaraDB for SelectDB instance.
-
Create an ApsaraDB for SelectDB instance. For more information, see Create an instance.
-
Connect to the ApsaraDB for SelectDB instance over the MySQL protocol. For more information, see Connect to an instance.
-
Create a test database and a test table.
-
Create a test database.
CREATE DATABASE test_db; -
Create a test table.
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;
-
-
Enable the public endpoint for the ApsaraDB for SelectDB instance. For more information, see Apply for and release a public endpoint.
-
Add the public IP address of the SeaTunnel environment to the IP address whitelist. For more information, see Configure an IP address whitelist.
-
Synchronize data by using the local engine
-
Create a configuration file named
mysqlToSelectDB.confand configure the job.env { execution.parallelism = 2 job.mode = "BATCH" checkpoint.interval = 10000 } source{ jdbc { url = "jdbc:mysql://host:ip/test_db" driver = "com.mysql.cj.jdbc.Driver" user = "admin" 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="admin" password="****" selectdb.config { file.type="json" } } } -
Submit the job on the command line.
sh ./bin/seatunnel.sh --config ./mysqlToSelectDB.conf -e local