Search code examples
javaapache-kafkakafka-consumer-apiconsumer

Kafka Consumer returns null value


I am trying to make a kafka consumer in Java but the consumer.poll(5000) method call return null value no matter what. here is the code:

package com.apache.kafka.consumer;


import java.util.Map;
import java.util.Properties;

import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.log4j.Logger;

import org.apache.kafka.clients.consumer.ConsumerRecords;

public class Consumer {
    public static void main(String[] args) throws Exception {
        final Logger logger = Logger.getLogger(Consumer.class);
          //Kafka consumer configuration settings
          String topicName = "mytopic";
          Properties props = new Properties();
      
      
           props.put("bootstrap.servers", "localhost:9092");
           props.put("group.id", "test");
           props.put("enable.auto.commit", "true");
           props.put("auto.offset.reset","earliest");
           props.put("auto.commit.interval.ms", "1000");
           props.put("key.deserializer", 
           "org.apache.kafka.common.serialization.StringDeserializer");
           props.put("value.deserializer", 
           "org.apache.kafka.common.serialization.StringDeserializer");
           props.put("partition.assignment.strategy", "range");
           KafkaConsumer<String, String> consumer = new 
           KafkaConsumer<String, String>(props);
   
      //Kafka Consumer subscribes list of topics here.
      consumer.subscribe("sampletopic");
      while (true) {
          Map<String,ConsumerRecords<String, String>> records = consumer.poll(0);
          for (ConsumerRecords<String, String> record : records.values()) {
              System.out.println(records);
          }
     }
}

}

Please help!!!

I have created the topic already and also have added some data in it plus the zookeeper and kafka are running perfectly. I don;t know why the poll() method is returning null.


Solution

  • The call to poll needs to be in a loop, that's why the literature calls it the poll loop.

    If its returning null its either polling too early and exiting the main or no data is in the topic

    See the usage examples here https://kafka.apache.org/22/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html

         Properties props = new Properties();
         props.setProperty("bootstrap.servers", "localhost:9092");
         props.setProperty("group.id", "test");
         props.setProperty("enable.auto.commit", "true");
         props.setProperty("auto.commit.interval.ms", "1000");
         props.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
         props.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
         KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
         consumer.subscribe(Arrays.asList("foo", "bar"));
         while (true) {
             ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
             for (ConsumerRecord<String, String> record : records)
                 System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
         }
    

    Notice the loop ^