Simple Kafka Consumer not receiving messages

后端 未结 6 2261
萌比男神i
萌比男神i 2021-02-09 15:20

I am a newbie to Kafka and running a simple kafka consumer/producer example as given on KafkaConsumer and KafkaProducer. When I am running consumer from terminal, consumer is re

6条回答
  •  深忆病人
    2021-02-09 15:53

    First check what all the groups are available by using :

    ./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
    

    Then check for which group your topic belongs by using below cmd :

    ./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group  --describe
    

    Once you find your topic and associated group name (just replace group.id with your group if it not belongs to default group) then try with below prop and let me know if it works :

      props.put("bootstrap.servers", "localhost:9092");
      props.put("group.id", "test-consumer-group"); // default topic name
      props.put("enable.auto.commit", "true");
      props.put("auto.commit.interval.ms", "1000");
      props.put("session.timeout.ms", "30000");
      props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
      props.put("value.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
      KafkaConsumer consumer = new KafkaConsumer(props);
    
      //Kafka Consumer subscribes list of topics here.
      consumer.subscribe(Arrays.asList(topicName));  // replace you topic name
    
      //print the topic name
    
      java.util.Map> listTopics = consumer.listTopics();
      System.out.println("list of topic size :" + listTopics.size());
    
      for(String topic : listTopics.keySet()){
          System.out.println("topic name :"+topic);
      }
    

提交回复
热议问题