VPC Access

Last updated: 2021-07-22 10:45:26

    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 from the Network column in the Access Mode section on the Instance Details page in the console.
    img
    topic_name Topic name, which can be copied from the Topic Management page in the console.
    img
    1. Compile and run producer.py.
    2. View the operation result.
    3. On the Topic Management page in the CKafka console, select the corresponding topic and click More > Message Query to view the just sent message.

    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 from the Network column in the Access Mode section on the Instance Details page in the console.
    img
    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.
    img
    1. Compile and run consumer.py.

    2. View the operation result.

    3. On the Consumer Group page in the CKafka console, select the corresponding consumer group, enter the topic name in Topic Name, and click Query Details to view the consumption details.