We have a kafka topic with 2 producers. I am trying to build a simple kafka tool in Java to consume and view the message from the topic. Idea is to get the offset and partition from user and to seek that particular offset message and show it to the user. My problem is my tool is able to find offsets of only one of the producers.
For Example : Producer 1 message is at Partition 3 and Offset 514124 , Producer 2 message is at Partition 3 and offset 547007. Both were produced around the same time back to back. When I seek offset 514124, it is working, but when I seek 547007 it is showing the below error
[MSG][Error consuming Kafka message: null][STACK][java.util.NoSuchElementException
I am assuming it is because of the gap in the offset ( 514124 and 547007) . How do I ensure that my tool consumes whatever offset I seek in a topic irrespective of which producer is producing it?
I have set a group id, I have not given anything for enable auto commit and auto offset reset
try {
java.util.Properties props = new java.util.Properties();
props.put("bootstrap.servers", "sample"); // Replace with your Kafka broker URL
props.put("group.id", "sample"); // Consumer group ID
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("security.protocol", "SSL");
props.put("ssl.truststore.location", truststorePath);
props.put("ssl.truststore.password", truststorePassword);
props.put("ssl.keystore.location", keystorePath);
props.put("ssl.keystore.password", keystorePassword);
org.apache.kafka.clients.consumer.KafkaConsumer<String, Object> consumer = new org.apache.kafka.clients.consumer.KafkaConsumer<>(props);
String offsetStr = "OS"; // User Input offset
String partitionStr = "PP"; // User Input Partition
try {
// Configure topic partition and offset
int partition = Integer.parseInt(partitionStr);
long offset = Long.parseLong(offsetStr);
org.apache.kafka.common.TopicPartition topicPartition = new org.apache.kafka.common.TopicPartition(TN, partition); // User Input topic
consumer.assign(java.util.Collections.singletonList(topicPartition));
consumer.seek(topicPartition, offset);
// Consume Kafka message
org.apache.kafka.clients.consumer.ConsumerRecord<String, Object> record = consumer.poll(1000).iterator().next();
}
}
kafka-console-consumer --topic T --partition X --offset Y --max-messages 1?