Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading from beginning (offset = 0) of topic with Spring Kafka / Spring Boot

How can I start reading the topic from the beginning every time my application starts, if I have a @KafkaListener annotation ?

like image 425
user1511956 Avatar asked Nov 28 '25 11:11

user1511956


1 Answers

One way is to use an anonymous group management: no id() and no groupId() for that @KafkaListener. If idIsGroup() == true (default), of course.

And you also need use a @TopicPartition like explained here: https://docs.spring.io/spring-kafka/docs/2.6.2/reference/html/#manual-assignment

Starting with version 2.5.5, you can apply an initial offset to all assigned partitions:

topicPartitions = { @TopicPartition(topic = "topic1", 
         partitionOffsets = @PartitionOffset(partition = "*", initialOffset = "0"))
    })

Another way is to implement an AbstractConsumerSeekAware for your @KafkaListener class and call that ConsumerSeekCallback.seekToBeginning() when you get partitions assigned in the onPartitionsAssigned(Map<TopicPartition, Long> assignments, ConsumerSeekCallback callback).

like image 149
Artem Bilan Avatar answered Nov 30 '25 06:11

Artem Bilan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!