![](/img/trans.png)
[英]Find the topics under a consumer group in Kafka using Kafka-Python
[英]Reset kafka LAG (change offset) within consumer group in Kafka-python
我在使用 kafka-consumer-groups.sh 工具重置 LAG 的地方找到了這個如何更改主題的起始偏移量? 但我需要在應用程序中重置它。 我找到了這個例子,但它似乎沒有重置它。 在消費者重啟示例后,kafka-python 從最后生成的消息中讀取
consumer = KafkaConsumer("MyTopic", bootstrap_servers=self.kafka_server + ":" + str(self.kafka_port),
enable_auto_commit=False,
group_id="MyTopic.group")
consumer.poll()
consumer.seek_to_end()
consumer.commit()
... continue on with other code...
運行bin\\windows\\kafka-consumer-groups.bat --bootstrap-server localhost:9092 --group MyTopic.group --describe
仍然顯示兩個分區都有 LAG。 我怎樣才能讓當前偏移“快進”到最后?
TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
MyTopic 0 52110 66195 14085 kafka-python-1.4.2-6afb6901-c651-4534-a482-15358db42c22 /Host1 kafka-python-1.4.2
MyTopic 1 52297 66565 14268 kafka-python-1.4.2-c70e0a71-7d61-46a1-97bc-aa2726a8109b /Host2 kafka-python-1.4.2
為了“快進”消費者組的偏移量,意味着清除LAG,您需要創建將加入同一組的新消費者。
控制台命令是:
kafka-console-consumer.sh --bootstrap-server <brokerIP>:9092 --topic <topicName> --consumer-property group.id=<groupName>
同時,您可以運行該命令以查看您描述的延遲,您將看到延遲已被消除。
你可能想要這個:
def consumer_from_offset(topic, group_id, offset):
"""return the consumer from a certain offset"""
consumer = KafkaConsumer(bootstrap_servers=broker_list, group_id=group_id)
tp = TopicPartition(topic=topic, partition=0)
consumer.assign([tp])
consumer.seek(tp, offset)
return consumer
consumer = consumer_from_offset('topic', 'group', 0)
for msg in consumer:
# it will consume the msg beginning from offset 0
print(msg)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.