Quotes

Friday, October 11, 2019

Kafka Commands

Apache Kafka Command Line Interface (CLI)

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