Import data using Kafka
ApsaraDB for SelectDB supports using the Doris Kafka Connector to automatically subscribe to and synchronize data from Kafka. This topic describes how to use the Doris Kafka Connector to synchronize data to ApsaraDB for SelectDB.
Background information
Kafka Connect is a tool for reliably streaming data between Apache Kafka and other systems. You can define connectors to move large datasets into or out of Kafka.
The Kafka connector provided by the Doris community runs in a Kafka Connect cluster. It reads data from a Kafka topic and writes the data to ApsaraDB for SelectDB.
In business scenarios, users typically use the Debezium Connector to push database change data to Kafka, or call an API to write JSON-formatted data to Kafka in real time. The Doris Kafka Connector automatically subscribes to data in Kafka and synchronizes this data to ApsaraDB for SelectDB.
Kafka Connect running modes
Kafka Connect has two running modes.
Standalone mode
Standalone mode is not recommended for production environments.
Configure standalone
Configure the connect-standalone.properties file.
# Modify the broker address
bootstrap.servers=127.0.0.1:9092In the Kafka config directory, create a connect-selectdb-sink.properties file and add the following content:
name=test-selectdb-sink
connector.class=org.apache.doris.kafka.connector.DorisSinkConnector
topics=topic_test
doris.topic2table.map=topic_test:test_kafka_tbl
buffer.count.records=10000
buffer.flush.time=120
buffer.size.bytes=5000000
doris.urls=selectdb-cn-4xl3jv1****-public.selectdbfe.rds.aliyuncs.com
doris.http.port=8030
doris.query.port=9030
doris.user=admin
doris.password=****
doris.database=test_db
key.converter=org.apache.kafka.connect.storage.StringConverter
value.converter=org.apache.kafka.connect.json.JsonConverterStart in standalone
$KAFKA_HOME/bin/connect-standalone.sh -daemon $KAFKA_HOME/config/connect-standalone.properties $KAFKA_HOME/config/connect-selectdb-sink.propertiesDistributed mode
Configure distributed
Configure the connect-distributed.properties file.
# Modify the broker address
bootstrap.servers=127.0.0.1:9092
# Modify group.id. The ID must be the same for all workers in the same cluster.
group.id=connect-clusterStart in distributed
$KAFKA_HOME/bin/connect-distributed.sh -daemon $KAFKA_HOME/config/connect-distributed.propertiesAdd a connector
curl -i http://127.0.0.1:8083/connectors -H "Content-Type: application/json" -X POST -d '{
"name":"test-selectdb-sink-cluster",
"config":{
"connector.class":"org.apache.doris.kafka.connector.DorisSinkConnector",
"topics":"topic_test",
"doris.topic2table.map": "topic_test:test_kafka_tbl",
"buffer.count.records":"10000",
"buffer.flush.time":"120",
"buffer.size.bytes":"5000000",
"doris.urls":"selectdb-cn-4xl3jv1****-public.selectdbfe.rds.aliyuncs.com",
"doris.user":"admin",
"doris.password":"***",
"doris.database":"test_db",
"doris.http.port":"8030",
"doris.query.port":"9030",
"key.converter":"org.apache.kafka.connect.storage.StringConverter",
"value.converter":"org.apache.kafka.connect.json.JsonConverter"
}
}'Parameters
Parameter | Description |
name | The name of the connector. It must be a string without ISO control characters and must be unique within the Kafka Connect environment. |
connector.class | The class name or alias of the connector. Set this to |
topics | A comma-separated list of source topics. |
doris.topic2table.map | The mapping between topics and tables. Separate multiple mappings with a comma (,). Example: |
buffer.count.records | The number of records buffered in memory for each Kafka partition before being flushed to ApsaraDB for SelectDB. The default is 10,000 records. |
buffer.flush.time | The interval in seconds for flushing the in-memory buffer. The default is 120. |
buffer.size.bytes | The cumulative size in bytes of records to buffer in memory for each Kafka partition. The default is 5,000,000. |
doris.urls | ApsaraDB for SelectDB connection endpoint. You can obtain the relevant parameters on the Instance Details > Network Information page in the ApsaraDB for SelectDB console. Example: selectdb-cn-4xl3jv1****-public.selectdbfe.rds.aliyuncs.com |
doris.http.port | The default HTTP port for ApsaraDB for SelectDB is 8080. |
doris.query.port | ApsaraDB for SelectDB's MySQL protocol port is 9030 by default. |
doris.user | ApsaraDB for SelectDB username. |
doris.password | ApsaraDB for SelectDB password. |
doris.database | The ApsaraDB for SelectDB database to which data is written. |
key.converter | The JSON converter class for the key. |
value.converter | The JSON converter class for the value. |
jmx | Specifies whether to retrieve internal connector metrics through JMX. For more information, see Doris-Connector-JMX. The default is true. |
enable.delete | Specifies whether to synchronize delete operations. The default is false. |
label.prefix | The label prefix for data imported using Stream Load. The default is the connector application name. |
auto.redirect | When enabled, the connector redirects Stream Load requests through the frontend (FE) to a target backend (BE), which avoids the need to retrieve BE information. |
load.model | The data import method. The following methods are supported:
The default is |
sink.properties.* | The import parameters for Stream Load. Example: To specify a column separator, use For more information, see Stream Load. |
delivery.guarantee | Specifies the data consistency guarantee when you consume Kafka data and import it into ApsaraDB for SelectDB. Supported levels are Currently, ApsaraDB for SelectDB can only guarantee that data imported using copy into is |
enable.2pc | Specifies whether to enable two-phase commit to ensure exactly-once semantics. |
For other common Kafka Connect sink configurations, see Configuring Connectors.
Examples
Prerequisites
Install an Apache Kafka cluster or Confluent Cloud, version 2.4.0 or later. This example uses a single-node Kafka environment.
# Download and decompress the package wget https://archive.apache.org/dist/kafka/2.4.0/kafka_2.12-2.4.0.tgz tar -zxvf kafka_2.12-2.4.0.tgz cd kafka_2.12-2.4.0/ bin/zookeeper-server-start.sh -daemon config/zookeeper.properties bin/kafka-server-start.sh -daemon config/server.propertiesDownload doris-kafka-connector-1.0.0.jar and place the JAR file in the KAFKA_HOME/libs directory.
Create an ApsaraDB for SelectDB instance. For more information, see Create an instance.
Connect to an ApsaraDB for SelectDB instance by using 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;
Example 1: Sync JSON data
Configure the SelectDB sink
Using standalone mode as an example, create a selectdb-sink.properties file in the Kafka config directory and add the following content:
name=selectdb_sink connector.class=org.apache.doris.kafka.connector.DorisSinkConnector topics=test_topic doris.topic2table.map=test_topic:example_tbl buffer.count.records=10000 buffer.flush.time=120 buffer.size.bytes=5000000 doris.urls=selectdb-cn-4xl3jv1****-public.selectdbfe.rds.aliyuncs.com doris.http.port=8030 doris.query.port=9030 doris.user=admin doris.password=*** doris.database=test_db key.converter=org.apache.kafka.connect.storage.StringConverter value.converter=org.apache.kafka.connect.json.JsonConverter #Optional: Configure a dead-letter queue errors.tolerance=all errors.deadletterqueue.topic.name=test_error errors.deadletterqueue.context.headers.enable = true errors.deadletterqueue.topic.replication.factor=1Start Kafka Connect
bin/connect-standalone.sh -daemon config/connect-standalone.properties config/selectdb-sink.properties
Example 2: Use Debezium to synchronize MySQL data to ApsaraDB for SelectDB
In many business scenarios, you need to sync data from an operational database in real time. This requires using the database's change data capture (CDC) mechanism.
Debezium is a CDC tool based on Kafka Connect that can connect to various databases such as MySQL, PostgreSQL, SQL Server, Oracle, and MongoDB. It continuously sends data changes to a Kafka topic in a unified format for real-time consumption by downstream sinks. This example uses MySQL.
Download Debezium.
wget https://repo1.maven.org/maven2/io/debezium/debezium-connector-mysql/1.9.8.Final/debezium-connector-mysql-1.9.8.Final-plugin.tar.gzDecompress the downloaded file.
tar -zxvf debezium-connector-mysql-1.9.8.Final-plugin.tar.gzPlace all the extracted JAR files into the KAFKA_HOME/libs directory.
Configure the MySQL source.
Create a mysql-source.properties file in the Kafka config directory and add the following content:
name=mysql-source connector.class=io.debezium.connector.mysql.MySqlConnector database.hostname=rm-bp17372257wkz****.rwlb.rds.aliyuncs.com database.port=3306 database.user=testuser database.password=**** database.server.id=1 # A unique identifier for this client in Kafka database.server.name=test123 # The databases and tables to sync. By default, all databases and tables are synced. database.include.list=test table.include.list=test.test_table database.history.kafka.bootstrap.servers=localhost:9092 # The Kafka topic used to store database schema changes database.history.kafka.topic=dbhistory transforms=unwrap # See https://debezium.io/documentation/reference/stable/transformations/event-flattening.html transforms.unwrap.type=io.debezium.transforms.ExtractNewRecordState # Record delete events transforms.unwrap.delete.handling.mode=rewriteAfter configuration, the default Kafka topic name format is
SERVER_NAME.DATABASE_NAME.TABLE_NAME.NoteFor Debezium configurations, see Debezium connector for MySQL.
Configure ApsaraDB for SelectDB sink.
Create a selectdb-sink.properties file in the Kafka config directory and add the following content:
name=selectdb-sink connector.class=org.apache.doris.kafka.connector.DorisSinkConnector topics=test123.test.test_table doris.topic2table.map=test123.test.test_table:test_table buffer.count.records=10000 buffer.flush.time=120 buffer.size.bytes=5000000 doris.urls=selectdb-cn-4xl3jv1****-public.selectdbfe.rds.aliyuncs.com doris.http.port=8030 doris.query.port=9030 doris.user=admin doris.password=**** doris.database=test key.converter=org.apache.kafka.connect.json.JsonConverter value.converter=org.apache.kafka.connect.json.JsonConverter #Optional: Configure a dead-letter queue #errors.tolerance=all #errors.deadletterqueue.topic.name=test_error #errors.deadletterqueue.context.headers.enable = true #errors.deadletterqueue.topic.replication.factor=1NoteWhen you synchronize data to ApsaraDB for SelectDB, you must create a database and a table in advance.
Start Kafka Connect.
bin/connect-standalone.sh -daemon config/connect-standalone.properties config/mysql-source.properties config/selectdb-sink.propertiesNoteAfter startup, you can check the logs/connect.log file to verify that the service started successfully.
Advanced usage
Connector operations
# Check the connector status
curl -i http://127.0.0.1:8083/connectors/test-selectdb-sink-cluster/status -X GET
# Delete the current connector
curl -i http://127.0.0.1:8083/connectors/test-selectdb-sink-cluster -X DELETE
# Pause the current connector
curl -i http://127.0.0.1:8083/connectors/test-selectdb-sink-cluster/pause -X PUT
# Resume the current connector
curl -i http://127.0.0.1:8083/connectors/test-selectdb-sink-cluster/resume -X PUT
# Restart tasks within the connector
curl -i http://127.0.0.1:8083/connectors/test-selectdb-sink-cluster/tasks/0/restart -X POSTFor more information, see Connect REST Interface.
Dead-letter queue
By default, a conversion error causes the connector to fail. However, you can configure the connector to tolerate such errors by skipping them. You can also write the error details, the failed operation, and the problematic record to a dead-letter queue for later analysis.
errors.tolerance=all
errors.deadletterqueue.topic.name=test_error_topic
errors.deadletterqueue.context.headers.enable=true
errors.deadletterqueue.topic.replication.factor=1For more information, see Error Reporting in Connect.
Connect to an SSL-enabled Kafka cluster
To access an SSL-enabled Kafka cluster via Kafka Connect, you need to provide a certificate file (client.truststore.jks) to authenticate the public key of the Kafka broker. You can add the following configuration to your connect-distributed.properties file:
# Connect worker
security.protocol=SSL
ssl.truststore.location=/var/ssl/private/client.truststore.jks
ssl.truststore.password=test1234
# Embedded consumer for sink connectors
consumer.security.protocol=SSL
consumer.ssl.truststore.location=/var/ssl/private/client.truststore.jks
consumer.ssl.truststore.password=test1234For more information about configuring Kafka Connect to connect to an SSL-enabled Kafka cluster, see Configure Kafka Connect.