| Crates.io | kcli |
| lib.rs | kcli |
| version | 0.1.0 |
| created_at | 2025-11-07 18:11:09.900458+00 |
| updated_at | 2025-11-07 18:11:09.900458+00 |
| description | A Kafka client CLI tool for debugging and inspecting Kafka clusters |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1921958 |
| size | 81,789 |
A lightweight command-line tool for debugging and managing Kafka clusters using the native Kafka protocol.
cargo build --release
The binary will be available at target/release/kc.
kc <BROKER> <COMMAND> [ARGUMENTS]
All commands require the broker address as the first argument.
Retrieve metadata information about the Kafka cluster, including brokers, topics, and partitions.
# Get metadata for all topics
kc localhost:9092 get-metadata
# Get metadata for a specific topic
kc localhost:9092 get-metadata --topic my-topic
# Connect to a remote broker
kc kafka.example.com:9092 get-metadata
Get detailed information about a specific topic.
kc localhost:9092 get-topic my-topic
Shows:
Create a new topic with an optional partition count.
# Create topic with 1 partition (default)
kc localhost:9092 create-topic my-topic
# Create topic with 3 partitions
kc localhost:9092 create-topic my-topic:3
Behavior:
Delete an existing topic.
kc localhost:9092 delete-topic my-topic
Verifies the topic exists before attempting deletion.
Publish a message to a topic with a key and optional value.
# Publish to partition 0 (default)
kc localhost:9092 publish my-topic key1 value1
# Publish to specific partition
kc localhost:9092 publish my-topic:2 key2 value2
# Publish with key only (no value)
kc localhost:9092 publish my-topic key3
Features:
Fetch and display messages from a topic partition.
# Fetch from partition 0, starting at offset 0
kc localhost:9092 fetch my-topic:0
# Fetch from specific offset
kc localhost:9092 fetch my-topic:0:100
# Fetch from partition 2, offset 50
kc localhost:9092 fetch my-topic:2:50
Features:
The client automatically handles API version negotiation:
This ensures compatibility across different Kafka broker versions without hardcoding version numbers.
# Set broker for convenience
BROKER=localhost:9092
# Check cluster metadata
kc $BROKER get-metadata
# Create a topic with 3 partitions
kc $BROKER create-topic orders:3
# Verify topic was created
kc $BROKER get-topic orders
# Publish some messages
kc $BROKER publish orders:0 order-1 '{"item":"laptop","price":999}'
kc $BROKER publish orders:1 order-2 '{"item":"mouse","price":25}'
kc $BROKER publish orders:2 order-3 '{"item":"keyboard","price":75}'
# Fetch messages
kc $BROKER fetch orders:0:0
kc $BROKER fetch orders:1:0
kc $BROKER fetch orders:2:0
# Delete the topic
kc $BROKER delete-topic orders
Enable detailed logging with the RUST_LOG environment variable:
RUST_LOG=debug kc localhost:9092 create-topic my-topic:3
This will show:
The core client implementation includes:
kafka-protocol crate1. Client connects to broker
2. On first API call:
- Fetch API versions from broker
- Cache version information
3. For each request:
- Negotiate compatible API version
- Encode request with negotiated version
- Send request over TCP
- Receive and decode response
For maximum compatibility, the tool uses conservative API versions:
tokio - Async runtime for I/O operationskafka-protocol - Kafka protocol serialization/deserializationanyhow - Error handlingclap - Command-line argument parsingbytes - Byte buffer utilitiestracing - Structured loggingThe project includes comprehensive unit tests covering:
Run all tests:
cargo test
Run tests with output:
cargo test -- --nocapture
Run specific test:
cargo test test_record_batch_encoding
Current test suite includes 16+ tests covering:
cargo build --release
src/main.rs - Main entry point, KafkaClient implementation, and testsCargo.toml - Project dependencies and metadataThis tool implements the Kafka wire protocol directly:
[4 bytes: size][request/response data][RequestHeader][Request Body][ResponseHeader][Response Body]Common Kafka error codes you might encounter:
If you encounter connection problems:
# Verify broker is accessible
telnet localhost 9092
# Check with debug logging
RUST_LOG=debug kc localhost:9092 get-metadata
When creating a topic that already exists:
If publish succeeds but fetch returns no messages:
kc <broker> get-topic <topic>RUST_LOG=debug to see detailed error informationIf you see API version errors:
The client caches API versions per connection, so:
Contributions are welcome! Please ensure:
Apache-2.0