### Kafka Client Low-Level Operations in Rust Source: https://context7.com/oneslash/rs-kafka/llms.txt Demonstrates direct Kafka protocol operations using KafkaClient. This includes loading cluster metadata, fetching topic offsets, and retrieving messages from specific partitions. It requires the kafkang crate and provides an example of basic Kafka interaction without automatic offset management. ```rust use kafkang::client::{KafkaClient, FetchOffset, FetchPartition}; fn main() -> Result<(), Box> { // Create client and load metadata let mut client = KafkaClient::new(vec!["localhost:9092".to_owned()]); client.set_client_id("my-application".into()); client.load_metadata_all()?; // Check available topics let topics: Vec<&str> = client.topics().names().collect(); println!("Available topics: {:?}", topics); // Fetch partition offsets let topic_offsets = client.fetch_offsets(&["my-topic"], FetchOffset::Latest)?; for (topic, offsets) in topic_offsets { println!("Topic: {}", topic); for offset in offsets { println!(" Partition {}: offset {:?}", offset.partition, offset.offset); } } // Fetch messages from specific partition let fetch_partition = FetchPartition::new("my-topic", 0, 0); let responses = client.fetch_messages(&[fetch_partition])?; for response in responses { for topic in response.topics() { for partition in topic.partitions() { match partition.data() { Ok(data) => { println!("Highwater mark: {}", data.highwatermark_offset()); for msg in data.messages() { println!("Offset: {}, Length: {}", msg.offset, msg.value.len()); } } Err(e) => eprintln!("Error: {}", e), } } } } Ok(()) } ``` -------------------------------- ### Configure SASL Plain Authentication for Kafka Client Source: https://github.com/oneslash/rs-kafka/blob/master/README.md This example demonstrates how to configure SASL PLAIN authentication for a Kafka client. It initializes a KafkaClient and sets the SASL configuration with a username and password. Note that SASL/PLAIN does not encrypt credentials and should be combined with TLS in production. ```rust use kafkang::client::{KafkaClient, SaslConfig}; let mut client = KafkaClient::new(vec!["localhost:9096".to_owned()]); client.set_sasl_config(Some(SaslConfig::plain("kafkang", "kafkang-secret"))); client.load_metadata_all().unwrap(); ``` -------------------------------- ### Rust Kafka Consumer for Multiple Topics and Partitions Source: https://context7.com/oneslash/rs-kafka/llms.txt This Rust code demonstrates how to configure a Kafka consumer to subscribe to multiple topics and specific partitions. It utilizes custom offset management, starting from the earliest available offset, and configures fetch parameters like maximum wait time and minimum bytes. Error handling is basic, relying on the Result type for propagation. ```rust use kafkang::consumer::{Consumer, FetchOffset, GroupOffsetStorage}; use std::time::Duration; use std::collections::HashMap; fn multi_topic_consumer() -> Result<(), Box> { // Create consumer for multiple topics let mut consumer = Consumer::from_hosts(vec!["localhost:9092".to_owned()]) .with_topic("topic-1".to_owned()) .with_topic_partitions("topic-2".to_owned(), &[0, 1, 2]) .with_group("multi-topic-group".to_owned()) .with_fallback_offset(FetchOffset::Earliest) .with_offset_storage(Some(GroupOffsetStorage::Kafka)) .with_fetch_max_wait_time(Duration::from_secs(5)) .with_fetch_min_bytes(10_000) .with_fetch_max_bytes_per_partition(100_000) .create()?; // Check subscriptions let subscriptions = consumer.subscriptions(); println!("Subscribed to: {:?}", subscriptions); let mut message_count: HashMap = HashMap::new(); loop { let messagesets = consumer.poll()?; if messagesets.is_empty() { println!("No new messages"); break; } for ms in messagesets.iter() { let topic = ms.topic().to_string(); let count = message_count.entry(topic.clone()).or_insert(0); for msg in ms.messages() { *count += 1; println!("[{}:{}@{}] Size: {} bytes", topic, ms.partition(), msg.offset, msg.value.len()); } consumer.consume_messageset(&ms)?; } // Commit periodically consumer.commit_consumed()?; println!("Messages received per topic: {:?}", message_count); } Ok(()) } ``` -------------------------------- ### TLS/SSL Security Configuration for Kafka Client in Rust Source: https://context7.com/oneslash/rs-kafka/llms.txt Configures secure Kafka connections using TLS/SSL with the rustls library. This example shows how to set up a TlsConnector, add custom CA certificates, and configure client authentication for mutual TLS (mTLS) using PEM-encoded certificate and key files. It requires the kafkang crate. ```rust use kafkang::client::{KafkaClient, SecurityConfig, TlsConnector}; fn main() -> Result<(), Box> { // Build TLS connector with custom certificates let mut builder = TlsConnector::builder(); // Add custom CA certificate builder = builder.add_ca_certs_pem_file("/path/to/ca-cert.pem")?; // Configure client certificate for mTLS builder = builder.with_client_auth_pem_files( "/path/to/client-cert.pem", "/path/to/client-key.pem" )?; let connector = builder.build()?; // Create secure client let mut client = KafkaClient::new_secure( vec!["broker:9093".to_owned()], SecurityConfig::new(connector).with_hostname_verification(true) ); // Use client normally client.load_metadata_all()?; println!("Secure connection established"); Ok(()) } ``` -------------------------------- ### Configure Mutual TLS with rustls TlsConnector in Rust Source: https://github.com/oneslash/rs-kafka/blob/master/docs/migration-openssl-to-rustls.md Provides an example of configuring the rustls TlsConnector for mutual TLS (mTLS) by specifying client certificate and private key files. ```Rust use kafkang::client::{KafkaClient, SecurityConfig, TlsConnector}; let connector = TlsConnector::builder() .add_ca_certs_pem_file("ca.crt.pem") .unwrap() .with_client_auth_pem_files("client.crt.pem", "client.key.pem") .unwrap() .build() .unwrap(); let mut client = KafkaClient::new_secure( vec!["localhost:9094".to_owned()], SecurityConfig::new(connector), ); ``` -------------------------------- ### Configure SASL/PLAIN Authentication in Rust Source: https://github.com/oneslash/rs-kafka/blob/master/docs/sasl.md Demonstrates how to configure SASL/PLAIN authentication for a Kafka client using the kafkang library. This involves creating a KafkaClient instance and setting the SASL configuration with username and password. ```rust use kafkang::client::{KafkaClient, SaslConfig}; let mut client = KafkaClient::new(vec!["localhost:9096".to_owned()]); client.set_sasl_config(Some(SaslConfig::plain("kafkang", "kafkang-secret"))); client.load_metadata_all().unwrap(); ``` -------------------------------- ### Rust Kafka Producer with Compression and Batching Source: https://context7.com/oneslash/rs-kafka/llms.txt This Rust code snippet shows how to configure and use a Kafka producer to send messages with SNAPPY compression and batching. It includes error handling for sending messages and verifies acknowledgments, with a retry mechanism implicitly handled by the library's configuration. The producer reads lines from a file and sends them in batches. ```rust use std::time::Duration; use kafkang::client::{Compression, RequiredAcks}; use kafkang::producer::{Producer, Record}; use std::io::{BufRead, BufReader}; use std::fs::File; fn produce_from_file(file_path: &str) -> Result<(), Box> { // Configure producer for batching let mut producer = Producer::from_hosts(vec!["localhost:9092".to_owned()]) .with_ack_timeout(Duration::from_secs(30)) .with_required_acks(RequiredAcks::All) .with_compression(Compression::SNAPPY) .with_connection_idle_timeout(Duration::from_secs(540)) .create()?; let file = File::open(file_path)?; let reader = BufReader::new(file); let batch_size = 100; let mut batch: Vec> = Vec::with_capacity(batch_size); for line in reader.lines() { let line = line?; if line.trim().is_empty() { continue; } batch.push(Record::from_value("my-topic", line)); // Send when batch is full if batch.len() >= batch_size { let results = producer.send_all(&batch)?; // Verify all messages were acknowledged for result in results { for confirm in result.partition_confirms { if let Err(code) = confirm.offset { eprintln!("Send failed with code: {:?}", code); return Err(format!("Kafka error: {:?}", code).into()); } } } batch.clear(); println!("Batch sent successfully"); } } // Send remaining messages if !batch.is_empty() { producer.send_all(&batch)?; println!("Final batch sent: {} messages", batch.len()); } Ok(()) } ``` -------------------------------- ### Send Messages to Kafka using Producer API in Rust Source: https://context7.com/oneslash/rs-kafka/llms.txt Demonstrates how to send single and batched messages to Kafka using the high-level Producer API. It configures the producer with hosts, acknowledgment timeout, required acknowledgments, and compression. The code sends a single record and then a batch of records, printing confirmation offsets or errors. ```rust use std::time::Duration; use kafkang::producer::{Producer, Record, RequiredAcks}; use kafkang::client::Compression; fn main() -> Result<(), Box> { // Create a producer with configuration let mut producer = Producer::from_hosts(vec!["localhost:9092".to_owned()]) .with_ack_timeout(Duration::from_secs(1)) .with_required_acks(RequiredAcks::One) .with_compression(Compression::GZIP) .create()?; // Send single message let data = "hello, kafka".as_bytes(); producer.send(&Record::from_value("my-topic", data))?; // Send batch of messages for better performance let messages = vec![ Record::from_value("my-topic", "message 1".as_bytes()), Record::from_value("my-topic", "message 2".as_bytes()), Record::from_value("my-topic", "message 3".as_bytes()), ]; let results = producer.send_all(&messages)?; for result in results { for partition_confirm in result.partition_confirms { match partition_confirm.offset { Ok(offset) => println!("Message sent to offset: {}", offset), Err(code) => eprintln!("Error: {:?}", code), } } } Ok(()) } ``` -------------------------------- ### Construct OpenSSL Connector (Pre-migration) in Rust Source: https://github.com/oneslash/rs-kafka/blob/master/docs/migration-openssl-to-rustls.md Illustrates the older method of constructing a secure Kafka client using OpenSSL's SslConnector before migrating to rustls. ```Rust use kafkang::client::{KafkaClient, SecurityConfig}; use openssl::ssl::{SslConnector, SslMethod}; let openssl = SslConnector::builder(SslMethod::tls()).unwrap().build(); let security = SecurityConfig::new(openssl); let mut client = KafkaClient::new_secure(vec!["localhost:9094".to_owned()], security); ``` -------------------------------- ### Poll Messages from Kafka using Consumer API in Rust Source: https://context7.com/oneslash/rs-kafka/llms.txt Illustrates how to consume messages from a Kafka topic using the high-level Consumer API. It configures the consumer with hosts, topic, group ID, offset storage, fetch parameters, and polls for messages in a loop. Processed messages are printed, and offsets are committed back to Kafka. ```rust use kafkang::consumer::{Consumer, FetchOffset, GroupOffsetStorage}; use std::time::Duration; fn main() -> Result<(), Box> { // Create consumer with group and offset management let mut consumer = Consumer::from_hosts(vec!["localhost:9092".to_owned()]) .with_topic("my-topic".to_owned()) .with_group("my-group".to_owned()) .with_fallback_offset(FetchOffset::Earliest) .with_offset_storage(Some(GroupOffsetStorage::Kafka)) .with_fetch_max_wait_time(Duration::from_secs(1)) .with_fetch_min_bytes(1_000) .create()?; // Poll and process messages loop { let messagesets = consumer.poll()?; if messagesets.is_empty() { println!("No messages available"); break; } for messageset in messagesets.into_iter() { println!("Topic: {}, Partition: {}", messageset.topic(), messageset.partition()); for message in messageset.messages() { println!("Offset: {}, Value: {:?}", message.offset, message.value); // Process message data let data = String::from_utf8_lossy(message.value); println!("Message content: {}", data); } // Mark messageset as consumed consumer.consume_messageset(&messageset)?; } // Commit consumed offsets to Kafka consumer.commit_consumed()?; } Ok(()) } ``` -------------------------------- ### Replace OpenSSL Connector with rustls TlsConnector in Rust Source: https://github.com/oneslash/rs-kafka/blob/master/docs/migration-openssl-to-rustls.md Demonstrates how to replace the OpenSSL SslConnector with rustls' TlsConnector for establishing secure Kafka client connections. TlsConnector::default() uses native OS root CAs or falls back to Mozilla's set. ```Rust use kafkang::client::{KafkaClient, SecurityConfig, TlsConnector}; let connector = TlsConnector::default(); let security = SecurityConfig::new(connector); let mut client = KafkaClient::new_secure(vec!["localhost:9094".to_owned()], security); ``` -------------------------------- ### Add Kafkang Dependency to Cargo.toml Source: https://github.com/oneslash/rs-kafka/blob/master/README.md This snippet shows how to add the kafkang crate as a dependency in your Cargo.toml file. It specifies the version to be used in your Rust project. ```toml [dependencies] kafkang = "0.1.0" ``` -------------------------------- ### Disable Hostname Verification with rustls in Rust Source: https://github.com/oneslash/rs-kafka/blob/master/docs/migration-openssl-to-rustls.md Demonstrates how to explicitly disable hostname verification for TLS connections using SecurityConfig. This is insecure and not recommended. ```Rust let security = SecurityConfig::new(TlsConnector::default()) .with_hostname_verification(false); ``` -------------------------------- ### Override SNI Server Name with rustls in Rust Source: https://github.com/oneslash/rs-kafka/blob/master/docs/migration-openssl-to-rustls.md Shows how to override the server name used for SNI and verification when connecting to an IP address but the broker certificate is issued for a different DNS name. ```Rust use kafkang::client::{KafkaClient, SecurityConfig, TlsConnector}; let connector = TlsConnector::builder() .add_ca_certs_pem_file("ca.crt.pem") .unwrap() .build() .unwrap(); let mut client = KafkaClient::new_secure( vec!["127.0.0.1:9094".to_owned()], SecurityConfig::new(connector).with_server_name("localhost"), ); ``` -------------------------------- ### Update TLS Error Handling in rs-kafka Source: https://github.com/oneslash/rs-kafka/blob/master/docs/migration-openssl-to-rustls.md Update your code to match `kafkang::Error::Tls(..)` instead of `kafkang::Error::Ssl(..)` for handling TLS-related errors. This ensures compatibility with recent changes in the rs-kafka library. ```rust match client.load_metadata_all() { Ok(_) => {} Err(kafkang::Error::Tls(e)) => { eprintln!("TLS error: {e}"); } Err(e) => { eprintln!("Kafka error: {e}"); } } ``` -------------------------------- ### Trust Private CA with rustls TlsConnector in Rust Source: https://github.com/oneslash/rs-kafka/blob/master/docs/migration-openssl-to-rustls.md Shows how to configure the rustls TlsConnector to trust certificates signed by a private or self-signed CA by appending the CA certificate in PEM format. ```Rust use kafkang::client::{KafkaClient, SecurityConfig, TlsConnector}; let connector = TlsConnector::builder() .add_ca_certs_pem_file("ca.crt.pem") .unwrap() .build() .unwrap(); let mut client = KafkaClient::new_secure( vec!["localhost:9094".to_owned()], SecurityConfig::new(connector), ); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.