tencent cloud

Feedback

VPC Access

Last updated: 2024-01-09 15:00:32

    Overview

    This document describes how to access CKafka to send/receive messages with the SDK for Python in a VPC.

    Prerequisites

    Directions

    Upload the pythonkafkademo in the downloaded demo to the Linux server, log in to the server, and enter the pythonkafkademo directory.

    Step 1. Add the Python dependency library

    Run the following command to install:
    pip install kafka-python

    Step 2. Produce a message

    1. Modify the configuration parameters in the message production program producer.py.
    #coding:utf8
    from kafka import KafkaProducer
    import json
    producer = KafkaProducer(
    bootstrap_servers = ['$domainName:$port'],
    api_version = (0,10,0)
    )
    message = "Hello World! Hello Ckafka!"
    msg = json.dumps(message).encode()
    producer.send('topic_name',value = msg)
    print("produce message " + message + " success.")
    producer.close()
    
    Parameter
    Description
    bootstrap_servers
    Accessed network, which can be copied in the Network column in the Access Mode section on the instance details page in the console.
    
    
    
    topic_name
    Topic name, which can be copied from the Topic Management page in the console.
    
    
    
    2. Compile and run producer.py.
    3. View the operation result.
    
    
    
    4. On the Topic Management tab on the instance details page in the CKafka console, select the target topic and click More > Message Query to view the message just sent.
    
    
    

    Step 3. Consume the message

    1. Modify the configuration parameters in the message consumption program consumer.py.
    #coding:utf8
    from kafka import KafkaConsumer
    
    consumer = KafkaConsumer(
    '$topic_name',
    group_id = "$group_id",
    bootstrap_servers = ['$domainName:$port'],
    api_version = (0,10,0)
    )
    
    for message in consumer:
    print ("Topic:[%s] Partition:[%d] Offset:[%d] Value:[%s]" % (message.topic, message.partition, message.offset, message.value))
    
    Parameter
    Description
    bootstrap_servers
    Accessed network, which can be copied in the Network column in the Access Mode section on the instance details page in the console.
    
    
    
    group_id
    Consumer group ID, which can be customized according to the business needs
    topic_name
    Topic name, which can be copied from the Topic Management page in the console.
    
    
    
    2. Compile and run consumer.py.
    3. View the operation result.
    
    
    
    4. On the Consumer Group tab in the CKafka console, select the corresponding consumer group, enter the topic name, and click View Details to view the consumption details.
    
    
    
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support