Kafka List Topics

Apache Kafka, a distributed event-streaming platform, organizes data into topics. It is important to understand how to manage and monitor these topics. Listing topics in Kafka is a common task for checking existing configurations, debugging issues, or ensuring that producers and consumers are correctly interacting with the cluster.

This documentation provides detailed instructions on listing topics in Kafka using the Kafka command-line interface (CLI) and programmatic approaches. It also explains how to filter results, troubleshoot common issues, and implement best practices for managing Kafka topics.



TL;DR

  • Use the kafka-topics.sh CLI to list topics in a Kafka cluster:
  • Filter topics programmatically using Kafka client libraries in Java or Python.
  • Use kafka-configs.sh or REST APIs to fetch detailed topic configurations.

Listing Topics Using the Kafka CLI

Kafka provides the kafka-topics.sh command-line tool for managing topics. This tool is located in the bin directory of your Kafka installation and supports various operations, including listing topics.

Basic Command to List Topics

To list all topics in the Kafka cluster, use the --list option with kafka-topics.sh:

  • --bootstrap-server: Specifies the Kafka brokers to connect to (e.g., localhost:9092)

Example:

Output:

This command lists all topics, including internal topics such as __consumer_offsets.


Filtering Topic Names

Using Shell Utilities

To filter topics by name, pipe the output of kafka-topics.sh to grep:

Example Output:



Fetching Detailed Topic Information

Listing topics only provides the names. To fetch detailed configurations, use the kafka-configs.sh tool.

Example: Fetching Topic Configuration

Example Output:


Programmatically Listing Kafka Topics

For dynamic use cases, you can programmatically list Kafka topics using client libraries. Below are examples in Java and Python.

1. Using the Kafka Java Client

The Kafka Java client library provides a straightforward way to list topics.

Code Example:

2. List Using the Python confluent-kafka Library

Python users can leverage the confluent-kafka library to list topics.

Code Example:


Common Issues and Troubleshooting

1. No Topics Listed

  • Cause: Misconfigured --bootstrap-server.
  • Solution: Ensure the broker address is correct and reachable.

2. Authentication Errors

  • Cause: SASL/SSL authentication is required but not configured.
  • Solution: Pass the --command-config option with a properties file:

3. Lagging Internal Topics

  • Internal topics like __consumer_offsets are automatically created and managed by Kafka. Use the --exclude-internal flag if your tool or custom script supports it.


  1. Apache Kafka Documentation: Managing Topics
  2. Kafka GitHub Repository
  3. Python Client for Kafka
  4. Java Kafka Client API

Leave a Reply

Your email address will not be published. Required fields are marked *