gpt4 book ai didi

python - 使用 Python 读取特定的 Kafka 主题

转载 作者:太空宇宙 更新时间:2023-11-03 14:37:04 25 4
gpt4 key购买 nike

我的主题有 3 个分区,我尝试使用以下代码从每个特定分区读取内容

from kafka import KafkaConsumer, TopicPartition

brokers = 'localhost:9092'
topic = 'b3'

m = KafkaConsumer(topic, bootstrap_servers=['localhost:9092'])
par = TopicPartition(topic=topic, partition=1)
m.assign(par)

但我收到此错误:

    raise IllegalStateError(self._SUBSCRIPTION_EXCEPTION_MESSAGE)
kafka.errors.IllegalStateError: IllegalStateError: You must choose only one way to configure your consumer: (1) subscribe to specific topics by name, (2) subscribe to topics matching a regex pattern, (3) assign itself specific topic-partitions.

有人可以帮我解决这个问题吗?

最佳答案

您可以从 KafkaConsumer() 中删除主题参数并重试吗?

示例:

# manually assign the partition list for the consumer
from kafka import TopicPartition, KafkaConsumer
consumer = KafkaConsumer(bootstrap_servers='localhost:1234')
consumer.assign([TopicPartition('foobar', 2)])
msg = next(consumer)

引用:http://kafka-python.readthedocs.io/en/master/

关于python - 使用 Python 读取特定的 Kafka 主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46856937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com