Overview
In Data Subscription for Kafka (the current Kafka Server version is V2.6.0), you can consume subscription data through a Kafka client of version 0.11 or later. This document provides a consumption Demo to help you quickly test the data consumption process and understand how to parse the data format. Must-Knows
The Demo does not include a demonstration of how to consume data. It only prints the data, so you need to write your own data processing logic based on it. You can also use Kafka clients in other languages to consume and parse data.
Currently, consuming data by connecting to the Kafka instance of the data subscription over the public network is not supported. Access is supported only through the Tencent Cloud private network, and the region of the subscribed database instance must be the same as the region where data is consumed.
Consumption Demo Download
MongoDB data subscription currently supports only the JSON data format. The JSON protocol file is included in the following Demo example, so you do not need to download it separately.
Java Demo Steps
Compilation environment: Maven package management tool and JDK8. You can choose a packaging tool as needed. Maven is used as an example in the following introduction.
Runtime environment: a Tencent Cloud CVM instance (which must be in the same region as the subscription instance to access the private network address of the Kafka server) with JRE8 installed.
The operation steps are as follows:
3. Download the Java Demo and decompress it.
4. Go to the decompressed directory. For your convenience, the directory contains a Maven model file and a pom.xml file respectively, and you can choose either as needed.
Use Maven to package the project: mvn clean package.
5. Run the Demo.
After packaging with Maven, go to the target folder and run the following code:
java -jar consumerDemo-json-1.0-SNAPSHOT.jar --brokers xxx --topic xxx --group xxx --user xxx --password xxx
brokers is the private network access address of the data subscription Kafka instance, and topic is the subscription topic of the data subscription task. You can view both on the Subscription Details page.group, user, and password are the name, account, and password of the consumption group respectively. You can view them on the Consumption Management page.Golang Demo Steps
Compilation environment: Golang 1.12 or later with the Go Module environment configured.
Runtime environment: a Tencent Cloud CVM instance (which must be in the same region as the subscription instance to access the private network address of the Kafka server).
The operation steps are as follows:
3. Download the Golang Demo and decompress it.
4. Go to the decompressed directory and run go build -o subscribe ./main/main.go to generate the executable file subscribe.
5. Run the following code: ./subscribe --brokers=xxx --topic=xxx --group=xxx --user=xxx --password=xxx
brokers is the private network access address of the data subscription Kafka instance, and topic is the subscription topic of the data subscription task. You can view both on the Subscription Details page.group, user, and password are the name, account, and password of the consumption group respectively. You can view them on the Consumption Management page.Python3 Demo Steps
Compilation and runtime environment: a Tencent Cloud CVM instance (which must be in the same region as the subscription instance to access the private network address of the Kafka server), with Python 3 and pip3 installed (for dependency package installation).
Use pip3 to install the dependency packages:
pip install flag
pip install kafka-python
The operation steps are as follows:
3. Download the Python 3 Demo and decompress it.
4. Run the following code: python main.py --brokers=xxx --topic=xxx --group=xxx --user=xxx --password=xxx
brokers is the private network access address of the data subscription Kafka instance, and topic is the subscription topic of the data subscription task. You can view both on the Subscription Details page.group, user, and password are the name, account, and password of the consumption group respectively. You can view them on the Consumption Management page.