auto_offset_reset介绍

What to do when there is no initial offset in Kafka or if the current offset does not exist any more on the server (e.g. because that data has been deleted):

  • earliest: automatically reset the offset to the earliest offset
  • latest: automatically reset the offset to the latest offset
  • none: throw exception to the consumer if no previous offset is found for the consumer's group
  • anything else: throw exception to the consumer.

翻译:

当Kafka中没有初始偏移或如果当前偏移在服务器上不再存在时(例如,因为该数据已被删除),该怎么办:

  1. 最早:自动将偏移重置为最早的偏移
  2. 最新:自动将偏移重置为最新偏移
  3. none:如果没有为消费者组找到以前的偏移,则向消费者抛出异常
  4. 任何其他:抛出异常到消费者。
“auto.offset.reset”的值只能是:[latest, earliest, none]中的一个,默认是"latest"


代码示例:

from kafka import KafkaConsumer


consumer=KafkaConsumer('test',auto_offset_reset='earliest',bootstrap_servers=['172.21.10.136:9092'])                   
for message in consumer:
    print ("%s:%d:%d: key=%s value=%s" % (message.topic, message.partition,message.offset, message.key,message.value))


Logo

Kafka开源项目指南提供详尽教程,助开发者掌握其架构、配置和使用,实现高效数据流管理和实时处理。它高性能、可扩展,适合日志收集和实时数据处理,通过持久化保障数据安全,是企业大数据生态系统的核心。

更多推荐