This topic describes how to use the SDK for Python to connect to ApsaraMQ for Kafka to send and receive messages on a Linux server.
Before you start
- Note
Python 2.7 and 3.x are supported. In this topic, Python 3.9 is used.
Install the Python dependency library
Run the following command to install the Python dependency library:
pip install confluent-kafka==1.9.2
We recommend that you install confluent-kafka 1.9.2 or earlier. Otherwise, the SSL_HANDSHAKE
error is returned when you send messages over the Internet.
Prepare a configuration file
Download the demo project, modify the corresponding configurations based on the endpoint that you use, and then upload the demo project to the Linux server.
Go to the aliware-kafka-demos page. Click the
icon and select Download ZIP to download the demo project. Then, decompress the package of the demo project.
NoteThe downloaded package of the demo project includes the SSL root certificate. If you want to separately use the SSL root certificate, click Download the SSL root certificate.
In the decompressed demo project, find the kafka-confluent-python-demo folder and modify the setting.py configuration file based on the endpoint that you use.
Default endpoint
In the vpc directory, modify the setting.py configuration file.
kafka_setting = { 'bootstrap_servers': 'XXX:xxx,XXX:xxx', 'topic_name': 'XXX', 'group_name': 'XXX' }
Parameter
Description
bootstrap_servers
The default endpoint of the ApsaraMQ for Kafka instance. You can obtain the endpoint in the Endpoint Information section of the Instance Details page in the ApsaraMQ for Kafka console.
topic_name
The topic name. You can obtain the topic name on the Topics page in the ApsaraMQ for Kafka console.
group_name
The group name. You can obtain the group name on the Groups page in the ApsaraMQ for Kafka console.
SSL endpoint
In the vpc-ssl directory, modify the setting.py configuration file.
kafka_setting = { 'sasl_plain_username': 'XXX', 'sasl_plain_password': 'XXX', 'ca_location': '/XXX/mix-4096-ca-cert', 'bootstrap_servers': 'XXX:xxx,XXX:xxx', 'topic_name': 'XXX', 'group_name': 'XXX' }
Parameter
Description
sasl_plain_username
The username of the Simple Authentication and Security Layer (SASL) user.
NoteIf the ACL feature is not enabled for the ApsaraMQ for Kafka instance, you can obtain the username and password of the SASL user from the Username and Password parameters in the Configuration Information section of the Instance Details page in the ApsaraMQ for Kafka console.
If the ACL feature is enabled for the ApsaraMQ for Kafka instance, make sure that the SASL user is authorized to send and receive messages by using the instance. For more information, see Grant permissions to SASL users.
sasl_plain_password
The password of the SASL user.
ca_location
The path to which the SSL root certificate is saved. Replace XXX in the sample code with the local path. Example: /home/kafka-confluent-python-demo/vpc-ssl/mix-4096-ca-cert.
bootstrap_servers
The SSL endpoint of the ApsaraMQ for Kafka instance. You can obtain the endpoint in the Endpoint Information section of the Instance Details page in the ApsaraMQ for Kafka console.
topic_name
The topic name. You can obtain the topic name on the Topics page in the ApsaraMQ for Kafka console.
group_name
The group name. You can obtain the group name on the Groups page in the ApsaraMQ for Kafka console.
Upload the kafka-confluent-python-demo folder to the /home directory on the Linux server.
Send messages
Send messages based on the endpoint that you use.
Default endpoint
Run the following command to access the /home/kafka-confluent-python-demo/vpc subdirectory:
cd /home/kafka-confluent-python-demo/vpc
Run the following command to send messages:
python kafka_producer.py
The following sample code provides an example of kafka_producer.py:
SSL endpoint
Run the following command to access the /home/kafka-confluent-python-demo/vpc-ssl subdirectory:
cd /home/kafka-confluent-python-demo/vpc-ssl
Run the following command to send messages:
python kafka_producer.py
The following sample code provides an example of kafka_producer.py:
Subscribe to messages
Subscribe to messages based on the endpoint that you use.
Default endpoint
Run the following command to access the /home/kafka-confluent-python-demo/vpc subdirectory:
cd /home/kafka-confluent-python-demo/vpc
Run the following command to subscribe to messages:
python kafka_consumer.py
The following sample code provides an example of kafka_consumer.py:
SSL endpoint
Run the following command to access the /home/kafka-confluent-python-demo/vpc-ssl subdirectory:
cd /home/kafka-confluent-python-demo/vpc-ssl
Run the following command to subscribe to messages:
python kafka_consumer.py
The following sample code provides an example of kafka_consumer.py: