使用KafkaConsumer kafka-python kafka client

时间:2015-09-01 12:23:35

标签: apache-kafka kafka-python

我尝试使用以下命令读取kafka consumer中的消息:

bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning

这里我们可以阅读大约4天的旧消息,因为我们已将kafka服务器配置文件中的保留时间设置为7天。但是我们正在尝试使用kaka-python客户端库的KafkaConsumer来读取消息,如下所示:

cons = KafkaConsumer("localhost:9092", "test","smallest")
cons.fetch_messages()

我们正在获得今天的消息,只有一些偏移。我不知道如何在Kafka中获取最旧的消息,就像我们上面的kafka消费者shell脚本一样。请帮忙。

1 个答案:

答案 0 :(得分:0)

The docs show the config passed in via namedtuples.

consumer = KafkaConsumer('topic1', 'topic2',
                         bootstrap_servers=['localhost:9092'],
                         group_id='my_consumer_group',
                         auto_commit_enable=True,
                         auto_commit_interval_ms=30 * 1000,
                         auto_offset_reset='smallest')