How to create, delete, describe, or change a Kafka topic using the kafka-topics command line interface.

How to create a Kafka topic?

To create a Kafka topic, you need to provide:
  • Kafka hostname and port (e.g., localhost:9092)
  • Topic name
  • Number of partitions
  • Replication factor

Example

Creating a topic named first_topic with 3 partitions and replication factor of 1: For Kafka v2.2+:
kafka-topics --bootstrap-server localhost:9092 --topic first_topic --create --partitions 3 --replication-factor 1

Important gotchas

  • Cannot specify a replication factor greater than the number of brokers
  • No default values for partitions and replication factor
  • Topic name must contain only ASCII alphanumerics, ’.’, ’_’ and ’-‘

How to list Kafka topics?

Use kafka-topics with the --list option.

Example

For Kafka v2.2+:
kafka-topics --bootstrap-server localhost:9092 --list

How to describe a Kafka topic?

Use kafka-topics with the --describe option.

Example

For Kafka v2.2+:
kafka-topics --bootstrap-server localhost:9092 --describe --topic first_topic

How to increase the number of partitions?

Use kafka-topics with the --alter option.
Caution: Increasing the number of partitions in a Kafka topic is a DANGEROUS OPERATION if your applications are relying on key-based ordering.

Example

kafka-topics --bootstrap-server localhost:9092 --alter --topic first_topic --partitions 5

How to delete a Kafka topic?

Use kafka-topics with the --delete option.

Example

kafka-topics --bootstrap-server localhost:9092 --delete --topic first_topic
Ensure delete.topic.enable=true is set on brokers for deletion to work properly.