Sling Academy
Home/DevOps/Apache Kafka: How to List and Inspect Topics

Apache Kafka: How to List and Inspect Topics

Last updated: January 30, 2024

Introduction to Apache Kafka Topics

Apache Kafka is a distributed streaming platform that is commonly used for building real-time messaging systems. Topics, a core component of the Kafka ecosystem, are categories or feed names where records are stored and published. Each topic is split into partitions, which contain records in an immutable sequence. In this tutorial, we will explore how to list and inspect the various aspects of Kafka topics, providing step-by-step guidance on essential Kafka topic operations.

Prerequisites

  • Apache Kafka is set up and running
  • Access to the Kafka command-line tools
  • Basic understanding of Kafka topics and partitions

Listing Kafka Topics

Before we dive into the commands, let’s quickly review what Kafka topics are. Topics are simply the channels through which producers send records to consumers. Topics are further divided into partitions for scalability and parallelism.

Listing the topics in a Kafka cluster is one of the most fundamental operations. The kafka-topics.sh script, which comes with the Kafka distribution, provides this functionality. To list all topics, use the following command:

bin/kafka-topics.sh --list --bootstrap-server localhost:9092

This will output a list of all topics in the cluster. If you are connecting to a Kafka cluster that is not running on localhost or the default port, replace ‘localhost:9092’ with the appropriate server and port.

Inspecting Topic Details

To delve deeper into a specific topic’s configuration, you can describe it using the kafka-topics.sh script. The following command provides information such as the number of partitions, replication factor, and more:

bin/kafka-topics.sh --describe --topic your-topic-name --bootstrap-server localhost:9092

The output will look something like this:

Topic:your-topic-name	PartitionCount:1	ReplicationFactor:1	Configs:
Topic: your-topic-name	Partition: 0	Leader: 0	Replicas: 0	Isr: 0

This tells you detailed information about each partition within the topic, which broker is the leader, the replicas and the In-Sync Replicas (ISR).

Creating Topics

Before you can list and inspect topics, you may need to create them. Here’s how you do it with the kafka-topics.sh tool:

bin/kafka-topics.sh --create --topic new-topic-name --bootstrap-server localhost:9092 --partitions 3 --replication-factor 1

The above command creates a new topic with a name ‘new-topic-name’ that has 3 partitions and a replication factor of 1.

Advanced Topic Configurations

In addition to the basic operations, you may want to tune your Kafka topics for better performance and reliability. Topic configurations can be altered when creating a topic or updated afterward:

bin/kafka-topics.sh --alter --topic your-topic-name --config max.message.bytes=10485760 --bootstrap-server localhost:9092

This command sets the maximum message size allowed for ‘your-topic-name’ to 10MB.

Making use of Patterns

Kafka allows you to list topics using regular expression patterns. This is particularly useful when working with a large number of topics:

bin/kafka-topics.sh --list --bootstrap-server localhost:9092 --topic 'test.*'

This will list all the topics that begin with ‘test’.

Deleting Topics

When a topic is no longer needed, you can delete it with the following command:

bin/kafka-topics.sh --delete --topic your-old-topic-name --bootstrap-server localhost:9092

Note that topic deletion must be enabled in the broker configuration (delete.topic.enable=true).

Conclusion

In conclusion, managing Kafka topics is a fundamental skill when working with Kafka. This tutorial covered how to list and inspect Kafka topics, along with creating, altering, and deleting them. While these operations are straightforward, understanding their output is key to effectively managing and debugging your Kafka setup.

Next Article: An Introduction to Producer and Consumer in Apache Kafka

Previous Article: Kafka: How to Create and Manage Topics

Series: Apache Kafka Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide