kafka-http
A Rust client library for interacting with Kafka REST Proxy over HTTP.
Motivation
The Rust kafka binary clients are pretty bad to work with, buggy, incomplete, and the popular rdkafka crate requires a lot of dependencies and forces the use of C bindings and the C++ standard library, build system, etc. that is very heavyweight for a simple Kafka client.
The other projects such as kafka-rust and samsa are also not very ergonomic along with being effectively unmaintained. After working with them, I decided to write my own client via the http client proxy to have a lightweight and simple client that is easy to use without any large dependencies or C bindings.
Note that some features are missing, such as admin APIs, etc. but feel free to extend, fork, etc.
Overview
kafka-http provides a simple and ergonomic interface for working with Apache Kafka through the Kafka REST Proxy. It supports consumer group management, message consumption, message production, and offset management.
Features
- ✅ Create and manage Kafka consumers
- ✅ Subscribe to topics
- ✅ Poll for messages
- ✅ Produce messages to topics
- ✅ Commit offsets
- ✅ Async/await support with Tokio
- ✅ Configurable timeouts
- ✅ Comprehensive error handling
- ✅ Built-in tracing/logging support
Installation
Add this to your Cargo.toml:
[]
= "0.1" # or the latest version
Quick Start
Consuming Messages
Note that this example uses the redpanda kafka rest proxy. If you want to use Kafka, confluence HTTP Proxy, etc. the port usually is 8082 but should be set to whatever your port is.
use error;
use Error;
use ;
async
Producing Messages
use error;
use Error;
use ;
async
Committing Offsets
use error;
use Error;
use ;
async
API Documentation
KafkaHttpClient
The main client struct for interacting with Kafka REST Proxy.
Methods
new(base_uri: &str) -> Self- Create a new client instanceset_timeout_ms(&mut self, timeout_ms: u64)- Set the polling timeoutset_consumer_uri(&mut self, uri: &String)- Manually set consumer URIcreate_consumer(&mut self, group: &str, params: &CreateConsumerParams) -> Result<String, Error>- Create a new consumer (fails if exists)try_create_consumer(&mut self, group: &str, params: &CreateConsumerParams) -> Result<Option<String>, Error>- Create or reconnect to existing consumersubscribe(&self, group: &str, params: &SubscribeParams) -> Result<(), Error>- Subscribe to topicspoll(&self) -> Result<Vec<Record>, Error>- Poll for new recordsproduce(&self, topic: &str, params: &ProduceParams) -> Result<(), Error>- Produce messagescommit(&self, params: &PartitionOffsetCommitParams) -> Result<(), Error>- Commit offsets
Requirements
- Rust 1.90.0 or later
- Kafka REST Proxy running and accessible
Dependencies
reqwest- HTTP clientserde/serde_json- JSON serializationtracing- Logging and diagnostics
Error Handling
The library uses a custom Error type that provides detailed error information. All async methods return Result<T, Error>.
Logging
The library uses tracing for logging. To enable logs in your application: tracing
License
See the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Related Projects
Examples
For more examples, see the tests/ directory.
Contributing
Feel free to open an issue or submit a pull request.