Keywords: Kafka | partition_offsets | command_line_tools
Abstract: This article provides an in-depth exploration of command line tools for monitoring topics and partition offsets in Apache Kafka. It covers the usage of kafka-topics.sh and kafka-consumer-groups.sh, compares differences between old and new API versions, and demonstrates practical examples for dynamically obtaining partition offset information. The paper also analyzes message consumption behavior in multi-partition environments with single consumers, offering practical guidance for Kafka cluster monitoring.
Overview of Kafka Monitoring Tools
Apache Kafka provides a suite of command line tools for monitoring and managing cluster status. These tools enable developers to gain real-time insights into topic distribution, partition status, and consumer offsets.
Listing Topics
Use the kafka-topics.sh script to list all topics in the cluster:
./bin/kafka-topics.sh --list --zookeeper localhost:2181
test_topic_1
test_topic_2
...
This command retrieves topic metadata through ZooKeeper connection, providing clean output suitable for further processing.
Legacy Consumer Offset Checking
For Kafka versions prior to 0.9, the ConsumerOffsetChecker tool is available:
./bin/kafka-run-class.sh kafka.tools.ConsumerOffsetChecker --broker-info --group test_group --topic test_topic --zookeeper localhost:2181
Group Topic Pid Offset logSize Lag Owner
test_group test_topic 0 698020 698021 1 test_group-0
test_group test_topic 1 235699 235699 0 test_group-1
test_group test_topic 2 117189 117189 0 test_group-2
This tool displays current offset, log size, and consumption lag for each partition, helping identify consumption bottlenecks.
New Consumer Group Management
Kafka 0.9 and later versions introduced new consumer APIs with corresponding kafka-consumer-groups.sh tool:
./bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group count_errors --describe
GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG OWNER
count_errors logs 2 2908278 2908278 0 consumer-1_/10.8.0.55
count_errors logs 3 2907501 2907501 0 consumer-1_/10.8.0.43
count_errors logs 4 2907541 2907541 0 consumer-1_/10.8.0.177
count_errors logs 1 2907499 2907499 0 consumer-1_/10.8.0.115
count_errors logs 0 2907469 2907469 0 consumer-1_/10.8.0.126
The new tool connects directly through bootstrap servers, eliminating ZooKeeper dependency and providing clearer consumption status overview.
Consumer Partition Assignment Mechanism
In practical usage, the relationship between consumers and partitions is crucial. Reference articles indicate that when using high-level consumers, a single consumer instance in a group typically consumes only a subset of partitions rather than all partitions. This behavior differs from some Python libraries and requires special attention.
Dynamic Monitoring Implementation
For dynamic monitoring, combine these commands with shell scripts for periodic execution:
#!/bin/bash
while true; do
./bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group my_group --describe
sleep 30
done
This approach enables continuous tracking of offset changes and timely detection of consumption delays.
Tool Selection Recommendations
Choose appropriate tools based on Kafka version and specific requirements:
- Kafka < 0.9: Use
ConsumerOffsetChecker - Kafka ≥ 0.9: Prefer
kafka-consumer-groups.sh - Production environments: Recommend integrating with monitoring systems for automated alerts
Conclusion
Kafka's command line tools provide robust support for cluster monitoring. Through proper utilization of these tools, developers can efficiently obtain topic and partition offset information, ensuring stable operation of messaging systems. Understanding consumer partition assignment mechanisms further aids in optimizing consumer design.