Kafka & Java: Consumer List Topics

(Last Updated On: )

In this tutorial I will show you how to list all topics in Kafka. Before you begin you will need Maven/Eclipse all setup and a project ready to go. Also you should go through this tutorial to setup the consumer.

Imports

  1. import java.util.Map;
  2. import java.util.List;
  3. import org.apache.kafka.common.PartitionInfo;

Consumer List Topics

  1. Map<String, List> listTopics = consumer.listTopics();
  2. System.out.println("list of topic size :" + listTopics.size());
  3.  
  4. for (String topic : listTopics.keySet()) {
  5. System.out.println("topic name :" + topic);
  6. }