Working with Apache Kafka Topic using CLI

After installing Apache Kafka, you can use the Apache Kafka CLI tool to work with topics in the Apache Kafka server.

Create a new topic

We will use the Apache Kafka CLI’s kafka-topics.sh file to work with topics in the Apache Kafka server.

To create a new topic, you can use the command with the following syntax:

In there:

  • topic_name is the topic name that we need to create.
  • kafka_server is the address of the Apache Kafka server with the format host:port.
  • partition_number is the number of partitions we need to create.
  • replication_number is the number of replications we want for each partition. The value of this parameter will depend on how many Apache Kafka servers we have.

For example, I run the following command:

The result will be as follows:

To check the topic we just created above in the Apache Kafka server, you can run the following command:

The meaning of the parameters as I mentioned above.

My example is as follows:

Result:

As you can see, my topic huongdanjava has 2 partitions and the number of ReplicationFactor is 1 as the command I ran above.

View all topics

To check all topics contained in an Apache Kafka server, you can run the following command:

An example is as follows:

You will see the topic we just created above as follows:

View all messages in a topic

To see all messages in an Apache Kafka topic, you can use the following command:

For example:

The result when I run this command is as follows:
Currently, in my example topic, there are no messages but if a new message arrives, it will be displayed here!

Delete a topic

To delete a topic in the Apache Kafka server, we will use the following command:

My example is as follows:

Result:

Add Comment