Create a consumer group
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
Simple Log Service is activated.
The Simple Log Service SDK for Python is initialized. For more information, see Initialize the SLS Python SDK.
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 |
| String | Yes | The name of the project. |
| String | Yes | The name of the LogStore. |
| String | Yes | The name of the consumer group. The name must be unique within the LogStore. |
| int | Yes | The timeout period, in seconds. If no heartbeat is received from a consumer within this period, the consumer is deleted. |
| 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: |
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
For log consumption API operations, see: