python消费kafka关键点---offset

本文介绍了Kafka中auto_offset_reset配置的作用,当消费者找不到初始偏移量或数据已删除时,可以选择自动重置到最早的偏移量、最新的偏移量,或者抛出异常。通过代码示例展示了如何设置此参数。
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))


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值