Data Reading From Kafka

23 views Asked by At

I want to read data from a topic using kafkaconsumer , we can read data using seektobeginning and seektoend methonds but what I want that while reading the data if I stop in between and restart after sometime then I want that kafka starts reading from where it has left , What methods can I use for this so that I do not have to read the whole data again.

1

There are 1 answers

0
kopaka On

Simply do not seek at all.

The behaviour you want is explicitly something that Kafka manages for you: your consumer does belong to a consumer group and the Kafka broker stores the last read offset per consumer group by default. If your consumer dies or is stopped, the offset is kept for 1 week by default IIRC. Once you start a consumer in the same consumer group again, it will continue reading from the last position that was read successfully.

If there is no offset for a consumer to continue reading from (e.g. when starting for the first time), it will fall back to either beginning of the topic or the end. This is controlled by the consumer configuration auto.offset.reset, which would need to be set to earliest. By default it is configured as latest, so keep that in mind.