今天在写kafka的java api例子时候,遇到一个问题,比如我创建了一个test主题,往里面写了1,2,3,4,5条消息,在这个时候,我用一个新的group启动了一个消费者,发现该消费者只能读到5以后的消息,而1到5的消息是读不到的,于是感到疑惑,就去官网上找了一下,看到这这样一个配置项“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.
需要注意的是,这个配置只在group第一次订阅主题的时候触发,一旦这个offset值被确定下来了,你再把这个配置改成“earliest”就没效果了,因为他已经不符合这个条件了(这个group在这个主题下已经能拿到offset值了)
在使用Kafka Java API时,发现新创建的消费者组无法获取已存在主题的历史消息。通过研究发现,这与配置项'auto.offset.reset'有关。该配置在没有初始偏移量或当前偏移量在服务器上不存在时决定行为:'earliest'为从最早消息开始,'latest'为从最新消息开始,'none'则抛出异常。此设置仅在组首次订阅主题时生效,之后更改无效。
2万+

被折叠的 条评论
为什么被折叠?



