Implementation:Fede1024 Rust rdkafka ClientConfig Builder
| Knowledge Sources | |
|---|---|
| Domains | Messaging, Configuration |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Concrete builder for constructing Kafka client instances provided by the rust-rdkafka library.
Description
The ClientConfig struct provides a fluent builder API for Kafka client construction. It accumulates string key-value configuration parameters in a HashMap and then instantiates the desired client type through create() (default context) or create_with_context() (custom context). The builder delegates to librdkafka's native configuration system via rd_kafka_conf_new and rd_kafka_conf_set.
Usage
Import and use this struct whenever creating any Kafka client. Call new() to start, chain set() calls for parameters, then call create::<T>() with the desired client type.
Code Reference
Source Location
- Repository: rust-rdkafka
- File: src/config.rs
- Lines: L232-316
Signature
impl ClientConfig {
/// Creates a new empty configuration.
pub fn new() -> ClientConfig
/// Sets a parameter in the configuration.
pub fn set<K, V>(&mut self, key: K, value: V) -> &mut ClientConfig
where
K: Into<String>,
V: Into<String>,
/// Uses the current configuration to create a new Consumer or Producer.
pub fn create<T: FromClientConfig>(&self) -> KafkaResult<T>
/// Uses the current configuration and the provided context to create a new Consumer or Producer.
pub fn create_with_context<C, T>(&self, context: C) -> KafkaResult<T>
where
C: ClientContext,
T: FromClientConfigAndContext<C>,
}
Import
use rdkafka::config::ClientConfig;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| key | K: Into<String> | Yes | Kafka configuration parameter name (e.g., "bootstrap.servers") |
| value | V: Into<String> | Yes | Configuration parameter value |
| context | C: ClientContext | No | Custom context for callbacks (only for create_with_context) |
| T (type param) | FromClientConfig | Yes | Target client type (e.g., FutureProducer, StreamConsumer) |
Outputs
| Name | Type | Description |
|---|---|---|
| create() returns | KafkaResult<T> | Configured client instance or error |
| create_with_context() returns | KafkaResult<T> | Client instance with custom context or error |
Usage Examples
Create a FutureProducer
use rdkafka::config::ClientConfig;
use rdkafka::producer::FutureProducer;
let producer: FutureProducer = ClientConfig::new()
.set("bootstrap.servers", "localhost:9092")
.set("message.timeout.ms", "5000")
.create()
.expect("Producer creation failed");
Create a StreamConsumer
use rdkafka::config::ClientConfig;
use rdkafka::consumer::StreamConsumer;
let consumer: StreamConsumer = ClientConfig::new()
.set("bootstrap.servers", "localhost:9092")
.set("group.id", "my-group")
.set("auto.offset.reset", "earliest")
.create()
.expect("Consumer creation failed");
Create with Custom Context
use rdkafka::config::ClientConfig;
use rdkafka::consumer::StreamConsumer;
let consumer: StreamConsumer<CustomContext> = ClientConfig::new()
.set("bootstrap.servers", "localhost:9092")
.set("group.id", "my-group")
.create_with_context(CustomContext)
.expect("Consumer creation failed");