Create a consumer group

Updated at:

Call the CreateConsumerGroup operation to create a consumer group.

Use a consumer group when you consume Simple Log Service data in real time through SDKs from third-party software, multilingual applications, cloud services, or stream processing frameworks. Consumer groups handle the implementation details of Simple Log Service, manage load balancing and failover between consumers, and provide second-level real-time consumption.

Prerequisites

Parameters

The create_consumer_group method has the following syntax:

def create_consumer_group(self, project, logstore, consumer_group, timeout, in_order=False):

Request parameters

The following table describes the request parameters.

Parameter

Type

Required

Description

project

String

Yes

The name of the project.

logstore

String

Yes

The name of the LogStore.

consumer_group

String

Yes

The name of the consumer group. The name must be unique within the LogStore.

timeout

int

Yes

The timeout period, in seconds. If no heartbeat is received from a consumer within this period, the consumer is deleted.

in_order

bool

No

Specifies whether data is consumed in order. This parameter affects only the consumption order across shards before and after a shard split or merge. Default value: False.

The in_order parameter supports the following values:

  • True: Consume data in order.

    • After a shard is split, data in the original shard is consumed first. Consumption of the newly split shards begins only after the data in the original shard is fully consumed.

    • After shards are merged, data in the original shards is consumed first. Consumption of the merged shard begins only after the data in the original shards is fully consumed.

  • False: Consume data out of order. All shards start consumption at the same time. New shards generated by a split or merge are also consumed immediately.

Response parameters

For details about the response parameters, see CreateConsumerGroup.

Sample code

The following sample code calls create_consumer_group to create a consumer group:

from aliyun.log import LogClient
import os

def main():
    # Retrieve the AccessKey ID and AccessKey Secret from environment variables.
    access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
    access_key_secret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
    # The Simple Log Service endpoint. Replace it with the endpoint of the region where your resources reside.
    endpoint = "cn-hangzhou.log.aliyuncs.com"

    client = LogClient(endpoint, access_key_id, access_key_secret)

    # Replace the following values with the names of your own resources.
    project = "ali-test-project"
    logstore = "test-logstore"
    consumer_group = "consumer_group_test"
    timeout = 30
    in_order = False

    try:
        response = client.create_consumer_group(project, logstore, consumer_group, timeout, in_order)
        response.log_print()
    except Exception as e:
        print(f"An error occurred while creating the consumer group: {e}")

if __name__ == '__main__':
    main()

References