Bird
Raised Fist0
Kafkadevops~5 mins

Kafka CLI tools overview - Commands & Configuration

Choose your learning style10 modes available
Introduction
Kafka CLI tools help you manage and interact with Kafka servers and topics using simple commands. They solve the problem of controlling Kafka without needing a complex interface.
When you want to create or delete Kafka topics quickly from the command line.
When you need to send messages to a Kafka topic for testing or data input.
When you want to read messages from a Kafka topic to check data flow.
When you need to check the status or configuration of Kafka topics and brokers.
When you want to monitor Kafka consumer groups and their offsets.
Commands
This command creates a new Kafka topic named 'example-topic' with 3 partitions and a replication factor of 1 on the Kafka server running at localhost:9092.
Terminal
kafka-topics.sh --bootstrap-server localhost:9092 --create --topic example-topic --partitions 3 --replication-factor 1
Expected OutputExpected
Created topic example-topic.
→
--bootstrap-server - Specifies the Kafka server address to connect to
→
--create - Indicates that a new topic should be created
→
--topic - Names the topic to create
This command lists all the topics currently available on the Kafka server at localhost:9092.
Terminal
kafka-topics.sh --bootstrap-server localhost:9092 --list
Expected OutputExpected
example-topic __consumer_offsets
→
--bootstrap-server - Specifies the Kafka server address to connect to
→
--list - Lists all topics
This command starts a producer that sends messages to the 'example-topic'. You can type messages and press Enter to send them.
Terminal
kafka-console-producer.sh --broker-list localhost:9092 --topic example-topic
Expected OutputExpected
No output (command runs silently)
→
--broker-list - Specifies the Kafka broker address to connect to
→
--topic - Specifies the topic to send messages to
This command reads messages from the 'example-topic' starting from the beginning and stops after reading 3 messages.
Terminal
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic example-topic --from-beginning --max-messages 3
Expected OutputExpected
First message Second message Third message
→
--bootstrap-server - Specifies the Kafka server address to connect to
→
--topic - Specifies the topic to read messages from
→
--from-beginning - Reads messages from the start of the topic
→
--max-messages - Limits the number of messages to read
This command lists all consumer groups currently registered on the Kafka server.
Terminal
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
Expected OutputExpected
example-group console-consumer-12345
→
--bootstrap-server - Specifies the Kafka server address to connect to
→
--list - Lists all consumer groups
Key Concept

If you remember nothing else from Kafka CLI tools, remember: they let you create, send, read, and monitor Kafka topics and groups directly from the command line.

Common Mistakes
Using --zookeeper flag instead of --bootstrap-server with newer Kafka versions
The --zookeeper flag is deprecated and may not work with recent Kafka versions.
Always use --bootstrap-server to specify the Kafka server address.
Not specifying --from-beginning when consuming messages
Without --from-beginning, the consumer reads only new messages, missing existing ones.
Add --from-beginning to read all messages from the start.
Not creating a topic before producing messages
Producing to a non-existent topic will fail or create a topic with default settings, which may not be desired.
Create the topic explicitly with desired partitions and replication before producing.
Summary
Use kafka-topics.sh to create and list Kafka topics.
Use kafka-console-producer.sh to send messages to a topic.
Use kafka-console-consumer.sh to read messages from a topic.
Use kafka-consumer-groups.sh to list and monitor consumer groups.

Practice

(1/5)
1. Which Kafka CLI tool is used to create a new topic?
easy
A. kafka-topics.sh --create
B. kafka-console-producer.sh
C. kafka-console-consumer.sh
D. kafka-configs.sh

Solution

  1. Step 1: Identify the tool for topic management

    The kafka-topics.sh tool is used to manage topics including creation, deletion, and listing.
  2. Step 2: Confirm the create option usage

    Using --create with kafka-topics.sh creates a new topic.
  3. Final Answer:

    kafka-topics.sh --create -> Option A
  4. Quick Check:

    Topic creation = kafka-topics.sh --create [OK]
Hint: Topic creation always uses kafka-topics.sh with --create [OK]
Common Mistakes:
  • Confusing producer or consumer tools for topic creation
  • Using kafka-configs.sh which manages configs, not topics
  • Omitting --create option
2. Which of the following is the correct syntax to start a Kafka console producer connecting to a server at localhost:9092?
easy
A. kafka-console-producer.sh --connect localhost:9092 --topic mytopic
B. kafka-console-producer.sh --broker-list localhost:9092 --topic mytopic
C. kafka-console-producer.sh --server localhost:9092 --topic mytopic
D. kafka-console-producer.sh --bootstrap-server localhost:9092 --topic mytopic

Solution

  1. Step 1: Identify the correct option for specifying Kafka server

    Modern Kafka CLI tools use --bootstrap-server to specify the Kafka server address.
  2. Step 2: Check the full command syntax

    The correct syntax includes kafka-console-producer.sh --bootstrap-server localhost:9092 --topic mytopic.
  3. Final Answer:

    kafka-console-producer.sh --bootstrap-server localhost:9092 --topic mytopic -> Option D
  4. Quick Check:

    Use --bootstrap-server for server address [OK]
Hint: Always use --bootstrap-server to specify Kafka server [OK]
Common Mistakes:
  • Using deprecated --broker-list instead of --bootstrap-server
  • Using incorrect flags like --server or --connect
  • Omitting the --topic option
3. What will be the output of this command if the topic test-topic has 3 messages: kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test-topic --from-beginning --max-messages 2?
medium
A. It will print all 3 messages from test-topic and then exit.
B. It will print the first 2 messages from test-topic and then exit.
C. It will print no messages because --max-messages is ignored.
D. It will print only the last message from test-topic.

Solution

  1. Step 1: Understand the --max-messages option

    The --max-messages 2 option limits the consumer to read only 2 messages before exiting.
  2. Step 2: Analyze the command behavior

    The command reads from the beginning and stops after printing 2 messages, even though the topic has 3 messages.
  3. Final Answer:

    It will print the first 2 messages from test-topic and then exit. -> Option B
  4. Quick Check:

    --max-messages limits output count [OK]
Hint: --max-messages limits how many messages are consumed [OK]
Common Mistakes:
  • Assuming all messages will be printed ignoring --max-messages
  • Thinking --max-messages prints last messages only
  • Confusing --from-beginning with consuming only latest messages
4. You run this command but get an error: kafka-console-producer.sh --topic mytopic. What is missing?
medium
A. The --bootstrap-server option specifying the Kafka server address.
B. The --create option to create the topic.
C. The --from-beginning option to read messages.
D. The --max-messages option to limit output.

Solution

  1. Step 1: Identify required options for kafka-console-producer.sh

    The producer needs to know which Kafka server to connect to, specified by --bootstrap-server.
  2. Step 2: Check the given command

    The command lacks --bootstrap-server, so it cannot connect and throws an error.
  3. Final Answer:

    The --bootstrap-server option specifying the Kafka server address. -> Option A
  4. Quick Check:

    Producer needs --bootstrap-server to connect [OK]
Hint: Always include --bootstrap-server to avoid connection errors [OK]
Common Mistakes:
  • Assuming topic creation is automatic without --create
  • Confusing producer options with consumer options
  • Adding irrelevant options like --from-beginning for producer
5. You want to list all topics on a Kafka server at localhost:9092. Which command correctly lists the topics?
hard
A. kafka-console-producer.sh --list --bootstrap-server localhost:9092
B. kafka-console-consumer.sh --list --bootstrap-server localhost:9092
C. kafka-topics.sh --list --bootstrap-server localhost:9092
D. kafka-configs.sh --list --bootstrap-server localhost:9092

Solution

  1. Step 1: Identify the tool for listing topics

    The kafka-topics.sh tool manages topics and supports the --list option to show all topics.
  2. Step 2: Confirm the correct command syntax

    Using --bootstrap-server localhost:9092 specifies the Kafka server to connect to.
  3. Final Answer:

    kafka-topics.sh --list --bootstrap-server localhost:9092 -> Option C
  4. Quick Check:

    List topics with kafka-topics.sh --list [OK]
Hint: Use kafka-topics.sh with --list to see all topics [OK]
Common Mistakes:
  • Using consumer or producer tools to list topics
  • Omitting --bootstrap-server causing connection failure
  • Using kafka-configs.sh which lists configs, not topics
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard