CLI Basics
Producing, consuming, groups, offsets, and configs from the command line
The command-line tools are how you'll inspect and fix things in production, so they're worth knowing before any client code. All commands below assume the helper from Setup:
K() { docker exec -i kafka-1 /opt/kafka/bin/"$@"; }
B="--bootstrap-server localhost:19092" # bash; in zsh, type it out or use an arrayTopics
You can add partitions but never remove them. And adding them changes
which partition each key maps to: hash(key) % 6 and hash(key) % 12
differ. All future records for a key may land in a new partition, so
per-key ordering breaks across the change. Choose the partition count up
front with growth in mind.
Producing and consuming
# produce lines from stdin; key:value with a separator
printf 'k0:v1\nk1:v2\nk0:v3\n' | K kafka-console-producer.sh $B --topic demo \
--reader-property parse.key=true --reader-property key.separator=:
# consume from the beginning, showing keys, partitions, and offsets
K kafka-console-consumer.sh $B --topic demo --from-beginning \
--formatter-property print.key=true \
--formatter-property print.partition=true \
--formatter-property print.offset=trueThe console consumer without --group uses a throwaway group, reads, and
commits nothing. Add --group my-app to consume as part of a real group.
(Older tutorials use --property for these options; Kafka 4.3 still
accepts it but warns that it's deprecated.)
Offsets
K kafka-get-offsets.sh $B --topic demo
demo:0:0
demo:1:0
demo:2:5 # partition 2 holds offsets 0-4; the next record gets offset 5All five keyed records above went to partition 2 in this lab: keys k0
and k1 happen to hash to the same partition with 3 partitions. That's
fine, and a reminder that keys spread records only statistically.
Consumer groups
GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID
analytics clicks 0 342 1370 1028 rdkafka-13b4...
analytics clicks 1 294 1217 923 rdkafka-13b4...LAG = log-end offset − committed offset: how many records the group hasn't processed yet. It's the single most important consumer metric.
Offset resets only work on a group with no active members. Stop the
consumers first, run the reset with --dry-run to see the result, then
--execute. Resetting a production group to --to-earliest reprocesses
everything in retention, so make sure the consumers are idempotent.
Configs
Topic and broker settings can be changed at runtime:
Inspecting the data on disk
docker exec kafka-1 /opt/kafka/bin/kafka-dump-log.sh \
--files /var/lib/kafka/data/demo-2/00000000000000000000.log --print-data-log
baseOffset: 0 lastOffset: 4 count: 5 baseSequence: 0 lastSequence: 4 producerId: 2001 ...
| offset: 0 CreateTime: 1791799990488 keySize: 2 valueSize: 2 sequence: 0 key: k0 payload: v1
| offset: 1 ... key: k1 payload: v2
...kafka-dump-log.sh must run on a broker that holds that replica (here,
demo-2 exists on every broker because the replication factor is 3).
You'll use it in the next chapter.
Performance tools
K kafka-producer-perf-test.sh --topic demo --num-records 500000 --record-size 200 \
--throughput -1 --command-property bootstrap.servers=localhost:19092 acks=all
K kafka-consumer-perf-test.sh $B --topic demo --messages 500000Lab
- Create
orderswith 6 partitions, produce 10 keyed records, and consume them showing partition and offset. - Consume with
--group laband stop with Ctrl-C. Describe the group. - Produce 5 more, describe again, and read the LAG.
- Reset the group to the earliest offset with
--dry-run, then--execute, and consume again. - Set
retention.ms=60000onorders, then delete the override.