I've one kafka topic with 8 partitions, subscribing the topic from single consumer and I've unique consumer group for the consumer. Now I tried to consume only the recent messages (in my case 3 mins before from current time) from all partitions. I used offsetForTimes method like below.
List<PartitionInfo> partitionInfos = consumer.partitionsFor(topic);
List<TopicPartition> topicPartions = partitionInfos.stream().......collect(Collectors.toList());
Long value = Instant.now().minus(120,ChronoUnit.SECONDS).toEpochMillis();
Map<TopicPartion,Long> topicPartitionTime = topicPartions.stream().collect(COllectors.toMap(tp -> tp,(value)));
Map<TopicPartition, OffsetAndTimeStamp> offsets = consumer.offsetsForTimes(topicPartitionTime);
now question is offsetsForTimes only returns one or two partitions offset positions and returns null for remaining.
I want to consume all partitions recent messages not one or two partitions.
I tried below also
consumer.unsubscribe();
consumer.assign(allPartitions);
Map<TopicPartition, OffsetAndTimeStamp> offsets = consumer.offsetsForTimes(topicPartitionTime);
but still getting only one or two offset positions.In worst case some times null offsets for all partitons.
if offsetForTimes works only with one/two partition, How to poll all partition recent records from single consumer ?
EDITED : I'm using Kafka cluster. 8 partitions shared on 3-4 machines.
Additional Inputs:- I am able to reproduce the problem with below scenario.
- Create three topics A (1-Partition), B(10-Partition) and C(10-Partition)
- KafkaStreams consuming message from A and pushing messages to B & C.
- Pushed some 100 Messages to A topic. KafkaStreams consumed and pushed to B&C topics. I can see Messages are spread over all partitions in B&C (ie. 10 partitions contains some 10 messages).
- I created single KafkaConsumer, Consuming B topic. Now I call offsetForTimes method with all partition and timestamp is 5 minutes minus from current.
- ensured consumer.assignment() returns all partitions before offsetForTimes.
- offsetForTimes returns single partition with offset position but when I call consumer.poll method it returns messages from other partitions too.
using apache kafka version - 2.11-2.2.0 Kafka clients jar - 2.0.1
Appreciate the help in advance.