Kafka Command Line Tools: Create Topics, Produce, Consume and Inspect
Start Here
All tools are in the bin folder and need --bootstrap-server host:port, the address of any broker. We use localhost:9092. Keep your broker from the previous lesson running.
bash
export BS=localhost:9092 # shortcut for this terminal
Create and Inspect Topics
bash
bin/kafka-topics.sh --bootstrap-server $BS --create --topic orders --partitions 3 --replication-factor 1
# Created topic orders.
bin/kafka-topics.sh --bootstrap-server $BS --list
bin/kafka-topics.sh --bootstrap-server $BS --describe --topic orders
text
Topic: orders TopicId: kQ3x... PartitionCount: 3 ReplicationFactor: 1 Configs:
Topic: orders Partition: 0 Leader: 1 Replicas: 1 Isr: 1
Topic: orders Partition: 1 Leader: 1 Replicas: 1 Isr: 1
Topic: orders Partition: 2 Leader: 1 Replicas: 1 Isr: 1
| Column | Meaning |
|---|---|
Leader | The broker that handles reads and writes for this partition |
Replicas | Brokers that hold a copy |
Isr | In-sync replicas: copies that are fully caught up |
bash
# change settings, add partitions, delete
bin/kafka-topics.sh --bootstrap-server $BS --alter --topic orders --partitions 6 # can only INCREASE partitions
bin/kafka-configs.sh --bootstrap-server $BS --alter --entity-type topics --entity-name orders --add-config retention.ms=86400000
bin/kafka-configs.sh --bootstrap-server $BS --describe --entity-type topics --entity-name orders
bin/kafka-topics.sh --bootstrap-server $BS --delete --topic orders
Partitions can only go up: You can add partitions but never remove them. Adding partitions also changes which partition a key goes to, which can break ordering for existing keys. Plan the partition count early.
Produce Messages
bash
bin/kafka-console-producer.sh --bootstrap-server $BS --topic orders
>first order
>second order
>^C (Ctrl+C to stop)
# with keys: type key:value
bin/kafka-console-producer.sh --bootstrap-server $BS --topic orders --property parse.key=true --property key.separator=:
>c1:{"order":1001,"amount":250}
>c2:{"order":1002,"amount":120}
>c1:{"order":1003,"amount":80}
Consume Messages
bash
# read new messages only (default)
bin/kafka-console-consumer.sh --bootstrap-server $BS --topic orders
# read everything from the start, show key, partition and offset
bin/kafka-console-consumer.sh --bootstrap-server $BS --topic orders --from-beginning \
--property print.key=true --property print.partition=true --property print.offset=true --property print.timestamp=true
# read one partition from a certain offset, stop after 5 messages
bin/kafka-console-consumer.sh --bootstrap-server $BS --topic orders --partition 0 --offset 2 --max-messages 5
text
CreateTime:1731750001000 Partition:1 Offset:0 c1 {"order":1001,"amount":250}
CreateTime:1731750005000 Partition:1 Offset:1 c1 {"order":1003,"amount":80}
CreateTime:1731750003000 Partition:2 Offset:0 c2 {"order":1002,"amount":120}
Notice: Both messages with key
c1 went to the same partition (1) and are in order. The key decides the partition, which is how Kafka keeps related events together.
Consumer Groups
bash
# start two consumers with the same group in two terminals
bin/kafka-console-consumer.sh --bootstrap-server $BS --topic orders --group email-service
bin/kafka-console-consumer.sh --bootstrap-server $BS --topic orders --group email-service
# the 3 partitions are shared between the two consumers
bin/kafka-consumer-groups.sh --bootstrap-server $BS --list
bin/kafka-consumer-groups.sh --bootstrap-server $BS --describe --group email-service
text
GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID
email-service orders 0 5 5 0 console-consumer-1a...
email-service orders 1 3 4 1 console-consumer-1a...
email-service orders 2 2 2 0 console-consumer-9f...
| Column | Meaning |
|---|---|
CURRENT-OFFSET | Next offset the group will read |
LOG-END-OFFSET | Next offset to be written |
LAG | How far the group is behind (log end minus current). The key health number |
Reset Offsets (Replay Data)
bash
# stop the consumers first, then preview with --dry-run
bin/kafka-consumer-groups.sh --bootstrap-server $BS --group email-service --topic orders --reset-offsets --to-earliest --dry-run
bin/kafka-consumer-groups.sh --bootstrap-server $BS --group email-service --topic orders --reset-offsets --to-earliest --execute
# other options: --to-latest, --to-offset N, --to-datetime 2026-11-16T10:00:00.000, --shift-by -100
Other Useful Tools
bash
bin/kafka-get-offsets.sh --bootstrap-server $BS --topic orders # latest offset per partition
bin/kafka-log-dirs.sh --bootstrap-server $BS --describe --topic-list orders # disk usage
bin/kafka-run-class.sh kafka.tools.DumpLogSegments --files /var/lib/kafka/data/orders-0/00000000000000000000.log --print-data-log
bin/kafka-broker-api-versions.sh --bootstrap-server $BS | head -3
bin/kafka-metadata-quorum.sh --bootstrap-server $BS describe --status # KRaft controller status
bin/kafka-producer-perf-test.sh --topic perf --num-records 1000000 --record-size 1000 --throughput -1 --producer-props bootstrap.servers=$BS
bin/kafka-consumer-perf-test.sh --bootstrap-server $BS --topic perf --messages 1000000
Practice
- Create a topic
clickswith 4 partitions - Produce 10 keyed messages with 3 different keys and see which partition each key lands on
- Start two consumers in one group and then a third one, and watch
--describe - Reset the group to the beginning and read everything again
Conclusion
Six tools cover daily work: kafka-topics, kafka-console-producer, kafka-console-consumer, kafka-consumer-groups, kafka-configs and the performance test scripts. Keys decide partitions, groups share partitions, and lag shows how far a group is behind.
Quick Summary: topics create/describe; keys -> same partition; groups share partitions; lag = end - current; reset-offsets to replay.

