tencent cloud

Feedback

SDK for Python

Last updated: 2023-03-13 17:04:49

    Overview

    This document uses Python as an example to describe how to connect to TDMQ for Pulsar to send and receive messages over the HTTP protocol.

    Prerequisites

    You have created the required resources as instructed in Resource Creation and Preparation.
    You have installed Python.
    You have installed pip.
    You have downloaded the demo.

    Directions

    1. Prepare the environment. For more information, see Python.
    pip install --upgrade tencentcloud-sdk-python
    2. Create the TDMQ client.
    # Authentication information
    cred = credential.Credential(SECRET_ID, SECRET_KEY)
    httpProfile = HttpProfile()
    httpProfile.endpoint = ENDPOINT
    
    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    # Create the TDMQ client
    client = tdmq_client.TdmqClient(cred, REGION, clientProfile)
    Parameter
    Description
    SECRET_ID and SECRET_KEY
    TencentCloud API key, which can be copied in Access Key > Manage API Key in the CAM console.
    
    ENDPOINT
    API request domain name: tdmq.tencentcloudapi.com.
    REGION
    Cluster region.
    3. Send the message.
    # Message sending request parameters
    req = models.SendMessagesRequest()
    params = {
    # Authorized role token
    "StringToken": token,
    # Topic name in the format of cluster (tenant) ID/namespace/topic name
    "Topic": topicName,
    # Message content
    "Payload": "this is a new message.",
    # Authorized role name
    "ProducerName": userName,
    # Sending timeout period
    "SendTimeout": 3000,
    }
    req.from_json_string(json.dumps(params))
    
    # Send the message
    resp = client.SendMessages(req)
    Parameter
    Description
    token
    Role token, which can be copied in the Token column on the Role Management page.
    
    userName
    Role name, which can be copied on the Role Management page. There is no need to specify ProducerName. If your business requires it, make sure that the userName is globally unique; otherwise, message sending will fail.
    topicName
    Topic name in the format of cluster (tenant) ID/namespace/topic name, such as pulsar-xxx/sdk_http/topic1. You can directly copy it on the Topic page in the console.
    4. Consume the message.
    # Message receiving request parameters
    req = models.ReceiveMessageRequest()
    params = {
    # Topic name in the format of cluster (tenant) ID/namespace/topic name
    "Topic": topicName,
    # Subscription name
    "SubscriptionName": subName,
    # Messages received by the consumer will first be stored in the `receiverQueueSize` queue to tune the message receiving rate.
    "ReceiverQueueSize": 10,
    # It is used to determine the position where the consumer initially receives messages. Valid values: `Earliest`, `Latest`.
    "SubInitialPosition": "Latest"
    }
    req.from_json_string(json.dumps(params))
    
    # Receive the message
    resp = client.ReceiveMessage(req)
    Parameter
    Description
    topicName
    Topic name in the format of cluster (tenant) ID/namespace/topic name, such as pulsar-xxx/sdk_http/topic1. You can directly copy it on the Topic page in the console.
    subName
    Subscription name, which can be copied on the Cluster > Consumer tab in the console.
    5. Acknowledge the message.
    # Message acknowledging request parameters
    req = models.AcknowledgeMessageRequest()
    params = {
    # ID of the message to be acknowledged
    "MessageId": messageId,
    # Topic name in the format of cluster (tenant) ID/namespace/topic name
    "AckTopic": topicName,
    # Subscription name
    "SubName": subName
    }
    req.from_json_string(json.dumps(params))
    
    # Acknowledge the message
    resp = client.AcknowledgeMessage(req)
    Parameter
    Description
    messageId
    Message ID obtained after message consumption.
    topicName
    Topic name in the format of cluster (tenant) ID/namespace/topic name, such as pulsar-xxx/sdk_http/topic1. You can directly copy it on the Topic page in the console.
    subName
    Subscription name, which can be copied on the Cluster > Consumer tab in the console.
    Notes
    Above is a brief introduction to message sending and receiving operations. For more information, see Demo or API Explorer.
    
    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