Say, I want to check the offset of the first and last message in Kafka for a particular partition. My idea was to use the assign(…) method along with the seekToBeginning(…) and seekToEnd(…). Unfortunately, this doesn't work.
If I set AUTO_OFFSET_RESET_CONFIG to "latest", the seekToBeginning(…) has no effect; if I set it to "earliest", seekToEnd(…) doesn't work. It seems that the only thing that matters for my consumer is AUTO_OFFSET_RESET_CONFIG.
I've seen a similar topic but the problem dealt with the subscribe(), not with the assign() method. The proposed solution was to implement the ConsumerRebalanceListner and pass it as a parameter to the subscribe() method. Unfortunately, the assign() method has only one signature and can only take a list of topic partitions.
The question is: Is it possible to use the seekToBeginning() or seekToEnd() with the assign() method. If yes, how? If no, why?
A relevant fragment of my code:
KafkaConsumer<String, ProtoMeasurement> consumer = createConsumer();
TopicPartition zeroP = new TopicPartition(TOPIC, 1);
List<TopicPartition> partitions = Collections.singletonList(zeroP);
consumer.assign(partitions);
consumer.poll(Duration.ofSeconds(1));
consumer.seekToBeginning(partitions);
long currOffsetPos = consumer.position(zeroP);
LOGGER.info("Current offset {}.", currOffsetPos);
ConsumerRecords<String, ProtoMeasurement> records = consumer.poll(Duration.ofMillis(100));
// ...
The logger prints the offset n, which is the biggest (latest) offset of the considered topic.