Apache Kafka provides a set of command-line interface (CLI) tools to interact with Kafka clusters. These commands allow you to perform various administrative tasks, such as managing topics, producing and consuming messages, and checking cluster status.
The Kafka CLI tools are located in the bin
directory of your Kafka installation. Common tools include kafka-topics.sh
, kafka-console-producer.sh
, and kafka-console-consumer.sh
.
To create, list, and delete topics, use the kafka-topics.sh
command.
# Create a new topic named "my-topic" with 1 partition and 1 replica
bin/kafka-topics.sh --create --topic my-topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
# List all topics in the Kafka cluster
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
# Describe a specific topic
bin/kafka-topics.sh --describe --topic my-topic --bootstrap-server localhost:9092
# Delete a topic
bin/kafka-topics.sh --delete --topic my-topic --bootstrap-server localhost:9092
To produce messages to a topic, use the kafka-console-producer.sh
command.
# Start a producer that sends messages to the "my-topic" topic
bin/kafka-console-producer.sh --topic my-topic --bootstrap-server localhost:9092
# To send a message, type it and press Enter
# Example message: "Hello, Kafka!"
To consume messages from a topic, use the kafka-console-consumer.sh
command.
# Start a consumer to read messages from the "my-topic" topic
bin/kafka-console-consumer.sh --topic my-topic --bootstrap-server localhost:9092 --from-beginning
# To start reading from the beginning of the topic, add the --from-beginning flag
To check the status of the Kafka cluster, use the kafka-broker-api-versions.sh
command.
# Get the API versions supported by brokers
bin/kafka-broker-api-versions.sh --bootstrap-server localhost:9092
Here is a quick example of producing and consuming messages:
# Start a Kafka console producer
bin/kafka-console-producer.sh --topic my-topic --bootstrap-server localhost:9092
# Type a message and press Enter to send it to "my-topic"
# In a new terminal, start a Kafka console consumer
bin/kafka-console-consumer.sh --topic my-topic --bootstrap-server localhost:9092 --from-beginning
# The message sent by the producer will be displayed in the consumer
Using Kafka's CLI tools allows you to efficiently manage topics, produce and consume messages, and check the status of your Kafka cluster. Familiarity with these commands is essential for effective Kafka administration and troubleshooting.