使用Python阅读特定的Kafka主题

时间:2017-10-20 20:34:24

标签: python apache-kafka kafka-python

我有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.

有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

您可以从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/