Here are some  commands often be used when we work with Apache Kafka command line interface (CLI).

1. Start the Kafka server

We needs 2 steps:

1.1 Start the ZooKeeper

1.2. Start the Kafka server

2. List all topics

The output in my console:

3. Create a topic

The output is:
Above command will create a “hello-topic“, with replication-factor = 1 and the number of partitions is 1. Note that the replication-factor controls how many servers will replicate each message that is written; therefore, it should be less than or equal the number of Kafka servers/brokers.

4. Describe a topic

The output is as below:

5. Publish messages to a topic

One of the most interesting command.

After this command, you can add any messages to the console, line by line. For ex:

 You can stop the console consumer via Ctrl-C

6. Consume messages

Below command will consume messages from the topic: hello-topic

The output on my console is:

You can stop the console consumer via Ctrl-C

7. Alter Apache Kafka Topics

7.1. Add more partitions to the topic

Below command will add 10 more partitions to the hello-topic topic. Note that before, the topic has only 1 partition.

The output in my console:

7.2. Delete a topic

7.3. Add configurations to the Kafka topic

The general syntax is:

For example,  below command will set the max message size = 128000 bytes for the hello-topic topic.

Here is an example output:

7.4. Add configurations to the Kafka topic

To remove above overridden configuration, we can use command:

For the list of configurations, please reference Apache Kafka page

8. Conclusion

We have seen some popular commands that provided by Apache Kafka command line interface. They are very essential when we work with Apache Kafka. I hope this post will bring you a list for easy copying and pasting.

Below are the articles related to Apache Kafka. If you’re interested in them, you can refer to the following links:

Apache Kafka Tutorial

Getting started with Apache Kafka 0.9

Using Apache Kafka Docker

Apache Kafka 0.9 Java Client API Example

Create Multi-threaded Apache Kafka Consumer

How To Write A Custom Serializer in Apache Kafka

Write An Apache Kafka Custom Partitioner

Apache Kafka Connect Example

Apache Kafka Connect MQTT Source Tutorial

Apache Flume Kafka Source And HDFS Sink Tutorial

Spring Kafka Tutorial




0 0 vote
Article Rating