tencent cloud

TDMQ for CKafka

Release Notes and Announcements
Release Notes
Broker Release Notes
Announcement
Product Introduction
Introduction and Selection of the TDMQ Product Series
What Is TDMQ for CKafka
Strengths
Scenarios
Technology Architecture
Product Series Introduction
Apache Kafka Version Support Description
Comparison with Apache Kafka
High Availability
Use Limits
Regions and AZs
Related Cloud Services
Billing
Billing Overview
Pricing
Billing Example
Changing from Postpaid by Hour to Monthly Subscription
Renewal
Viewing Consumption Details
Overdue Payments
Refund
Getting Started
Guide for Getting Started
Preparations
VPC Network Access
Public Domain Name Access
User Guide
Usage Process Guide
Configuring Account Permission
Creating Instance
Configuring Topic
Connecting Instance
Managing Messages
Managing Consumer Group
Managing Instance
Changing Instance Specification
Configuring Traffic Throttling
Configuring Elastic Scaling Policy
Configuring Advanced Features
Viewing Monitoring Data and Configuring Alarm Rules
Synchronizing Data Using CKafka Connector
Use Cases
Cluster Resource Assessment
Client Practical Tutorial
Log Integration
Open-Source Ecosystem Integration
Replacing Supporting Route (Old)
Migration Guide
Migration Solution Overview
Migrating Cluster Using Open-Source Tool
Troubleshooting
Topics
Clients
Messages
​​API Reference
History
Introduction
API Category
Making API Requests
Other APIs
ACL APIs
Instance APIs
Routing APIs
DataHub APIs
Topic APIs
Data Types
Error Codes
SDK Reference
SDK Overview
Java SDK
Python SDK
Go SDK
PHP SDK
C++ SDK
Node.js SDK
SDK for Connector
Security and Compliance
Permission Management
Network Security
Deletion Protection
Event Record
CloudAudit
FAQs
Instances
Topics
Consumer Groups
Client-Related
Network-Related
Monitoring
Messages
Agreements
CKafka Service Level Agreements
Contact Us
Glossary

Python SDK

PDF
포커스 모드
폰트 크기
마지막 업데이트 시간: 2024-09-09 21:25:33

Overview

This task uses the Python client as an example to guide you on how to use the elastic Topic of TDMQ for CKafka and send and receive messages.

Prerequisites

Directions

Step 1: Preparing the Environment

Run the following command to install the Python dependency database.
pip install kafka-python

Step 2: Creating a Topic and Subscription Relationship

1. On the Elastic Topic list page of the console, create a Topic.


2. Click the ID of the Topic to enter the Basic Information page and obtain the username, password, and address information.

3. In the Subscription Relationships tab, create a subscription relationship (consumption group).


Step 3: Producing Messages

1. Modify the configuration parameters in the message production program producer.py.
producer = KafkaProducer(
bootstrap_servers = ['xx.xx.xx.xx:port'],$address
api_version = (1, 1),
security_protocol = "SASL_PLAINTEXT",
sasl_mechanism = "PLAIN",
sasl_plain_username = "username",# username
sasl_plain_password = "password",# password
)

message = "Hello World! Hello Ckafka!"
msg = json.dumps(message).encode()
producer.send('topic_name', value = msg)#Topic name
print("produce message " + message + " success.")
producer.close()
Parameter
Description
bootstrapServers
The connection address. It can be obtained from the basic information page of an elastic Topic in the console.

sasl_plain_username
The username. It can be obtained from the basic information page of an elastic Topic in the console.
sasl_plain_password
The user password. It can be obtained from the basic information page of an elastic Topic in the console.
topic_name
The topic name. It can be obtained from the basic information page of an elastic Topic in the console.
2. Compile and run producer.py.
3. View the execution result.


Step 4: Consuming Messages

1. Modify the configuration parameters in the message consumption program consumer.py.
consumer = KafkaConsumer(
'topic_name',#topic name
group_id = "group_id",#Consumption group
bootstrap_servers = ['xx.xx.xx.xx:port'],# address
api_version = (1,1),

security_protocol = "SASL_PLAINTEXT",
sasl_mechanism = 'PLAIN',
sasl_plain_username = "username",# username
sasl_plain_password = "password",# password
)

for message in consumer:
print ("Topic:[%s] Partition:[%d] Offset:[%d] Value:[%s]" %
(message.topic, message.partition, message.offset, message.value))
Parameter
Description
bootstrapServers
The connection address. It can be obtained from the basic information page of an elastic Topic in the console.

sasl_plain_username
The username. It can be obtained from the basic information page of an elastic Topic in the console.
sasl_plain_password
The user password. It can be obtained from the basic information page of an elastic Topic in the console.
topic_name
The topic name. It can be obtained from the basic information page of an elastic Topic in the console.
group.id
The consumption group name. It can be obtained from the subscription relationship list of an elastic Topic in the console.

2. Compile and run consumer.py.
3. View the execution result.



도움말 및 지원

문제 해결에 도움이 되었나요?

피드백