Overview
In data subscription (Kafka Edition, where the current Kafka Server version is 2.6.0), you can consume the subscribed data through Kafka 0.11 or later available at DOWNLOAD. This document provides client consumption demos for Java, Go, and Python for you to quickly test the process of data consumption and understand the method of data parsing. When configuring the subscription task, you can select different formats of subscribed data, including ProtoBuf, Avro, and JSON. ProtoBuf and Avro adopt the binary format with a higher consumption efficiency, while JSON adopts the easier-to-use lightweight text format. The reference demo varies by the selected data format.
This document provides a demo of the Avro format. The demo already contains the Avro protocol file, so you don't need to download it separately.
Notes
Currently, data consumption over the Avro protocol is supported only for TencentDB for MySQL and TDSQL-C for MySQL.
Notes
The demo only prints out the consumed data and does not contain any usage instructions. You need to write your own data processing logic based on the demo. You can also use Kafka clients in other languages to consume and parse data.
Currently, data subscription to Kafka for consumption can be implemented over the Tencent Cloud private network but not the public network. In addition, the subscribed database instance and the data consumer must be in the same region.
The Kafka built in DTS has a certain upper limit for processing individual messages. When a single row of data in the source database exceeds 10 MB, this row may be discarded.
Downloading a consumption demo
Instructions for the Java demo
Compilation environment: Maven or Gradle and JDK 8. You can choose a desired package management tool. The following takes Maven as an example.
Runtime environment: Tencent Cloud CVM (which can access the private network address of the Kafka server only if it is in the same region as the subscribed instance). Install JRE 8.
Directions:
3. Download the Java demo and decompress it.
4. Access the decompressed directory. Maven model and pom.xml files are placed in the directory for your use as needed.
Package with Maven by running mvn clean package
.
5. Run the demo.
After packaging the project with Maven, go to the target folder target
and run java -jar consumerDemo-avro-1.0-SNAPSHOT.jar --brokers xxx --topic xxx --group xxx --user xxx --password xxx --trans2sql
.
broker
is the private network access address for data subscription to Kafka, and topic
is the subscription topic, which can be viewed on the Subscription details page as instructed in Viewing Subscription Details.group
, user
, and password
are the name, account, and password of the consumer group, which can be viewed on the Consumption Management page as instructed in Managing Consumer Group.trans2sql
indicates whether to enable conversion to SQL statement. In Java code, if this parameter is carried, the conversion will be enabled.
Notes
If trans2sql
is carried, javax.xml.bind.DatatypeConverter.printHexBinary()
will be used to convert byte values to hex values. You should use JDK 1.8 or later to avoid incompatibility. If you don't need SQL conversion, comment this parameter out.
6. Observe consumption.
Instructions for the Go demo
Compiling environment: Go 1.12 or later, with the Go module environment configured.
Runtime environment: Tencent Cloud CVM (which can access the private network address of the Kafka server only if it is in the same region as the subscribed instance).
Directions:
3. Download the Go demo and decompress it.
4. Access the decompressed directory and run go build -o subscribe ./main/main.go
to generate the executable file subscribe
.
5. Run ./subscribe --brokers=xxx --topic=xxx --group=xxx --user=xxx --password=xxx --trans2sql=true
.
broker
is the private network access address for data subscription to Kafka, and topic
is the subscription topic, which can be viewed on the Subscription details page as instructed in Viewing Subscription Details.group
, user
, and password
are the name, account, and password of the consumer group, which can be viewed on the Consumption Management page as instructed in Managing Consumer Group.trans2sql
indicates whether to enable conversion to SQL statement.
6. Observe consumption.
Instructions for the Python 3 demo
Compilation and runtime environment: Tencent Cloud CVM (which can access the private network address of the Kafka server only if it is in the same region as the subscribed instance). Install Python 3 and pip3 (for dependency package installation).
Use pip3
to install the dependency package:
pip install flag
pip install kafka-python
pip install avro
Directions:
3. Download the Python 3 demo and decompress it.
4. Run python main.py --brokers=xxx --topic=xxx --group=xxx --user=xxx --password=xxx --trans2sql=1
.
broker
is the private network access address for data subscription to Kafka, and topic
is the subscription topic, which can be viewed on the Subscription details page as instructed in Viewing Subscription Details.group
, user
, and password
are the name, account, and password of the consumer group, which can be viewed on the Consumption Management page as instructed in Managing Consumer Group.trans2sql
indicates whether to enable conversion to SQL statement.
5. Observe consumption.
Was this page helpful?