TopicCommand — Topic Management on Command Line

kafka.admin.TopicCommand is a command-line application that can alter, create, delete, describe and list topics in a Kafka cluster.

Table 1. TopicCommand’s Actions
Action Description


Alters the number of partitions, replica assignment, and/or configuration of a topic or topics


Creates a new topic


Deletes a topic or topics


Describes a topic or topics


Lists available topics

TopicCommand can be executed using kafka-topics shell script (i.e. bin/ or bin\windows\kafka-topics.bat).

$ ./bin/
Create, delete, describe, or change a topic.
Table 2. TopicCommand’s Options
Option Description


A topic configuration override for the topic being worked on (as name=value)


Disable rack aware replica assignment


When creating a topic(s), the action will only execute if the topic does not already exist


The number of partitions of the topic being created or altered

If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected.


A list of manual partition-to-broker assignments for the topic being created or altered


The replication factor for each partition of the topic being created


A topic or topics to be altered, created, deleted, or listed and described (through getTopics)

$ ./bin/ \
  --create \
  --zookeeper :2181 \
  --replication-factor 1 \
  --partitions 1 \
  --topic my-topic

$ ./bin/ --list --zookeeper :2181

$ ./bin/ \
  --zookeeper :2181 \
  --describe \
  --topic my-topic
Topic:my-topic  PartitionCount:1  ReplicationFactor:1  Configs:
  Topic: my-topic  Partition: 0  Leader: 0  Replicas: 0  Isr: 0

alterTopic Method

alterTopic(zkClient: KafkaZkClient, opts: TopicCommandOptions): Unit


alterTopic is used when…​FIXME

Creating Topic — createTopic Method

createTopic(zkClient: KafkaZkClient, opts: TopicCommandOptions): Unit

createTopic takes the values of topic, config and if-not-exists options.

createTopic creates a new AdminZkClient (with the input KafkaZkClient).

createTopic branches off per replica-assignment option’s existence:

In the end, createTopic prints out the following:

Created topic "[topic]"
createTopic is used exclusively when TopicCommand is executed with --create action.

deleteTopic Method

deleteTopic(zkClient: KafkaZkClient, opts: TopicCommandOptions): Unit


deleteTopic is used when…​FIXME

describeTopic Method

describeTopic(zkClient: KafkaZkClient, opts: TopicCommandOptions): Unit


describeTopic is used when…​FIXME

listTopics Method

listTopics(zkClient: KafkaZkClient, opts: TopicCommandOptions): Unit


listTopics is used when…​FIXME

Executing Standalone Application — main Method

main(args: Array[String]): Unit

main is the entry point of the TopicCommand standalone application when launched on command line (e.g. from bin/

main creates a TopicCommandOptions.

main prints out the following message and exits when no command-line arguments were given:

Create, delete, describe, or change a topic.

main prints out the following message and exits when no action was specified:

Command must include exactly one action: --list, --describe, --create, --alter or --delete

main makes sure the action and the options were all valid.

main creates a new KafkaZkClient (with the zookeeper option).

main branches off per the action.

In case of any exception, main prints out the following and exits with 1 exit code:

Error while executing topic command : [message]

getTopics Internal Method

getTopics(zkClient: KafkaZkClient, opts: TopicCommandOptions): Seq[String]


getTopics is used when TopicCommand is requested to alterTopic, listTopics, deleteTopic, describeTopic.

results matching ""

    No results matching ""