initial commit

This commit is contained in:
2025-08-20 17:34:20 +02:00
commit ca4aa96565
7 changed files with 2450 additions and 0 deletions

85
src/registry.rs Normal file
View File

@@ -0,0 +1,85 @@
use apache_avro::{AvroSchema, Schema};
use rdkafka::producer::{FutureProducer, FutureRecord};
use rdkafka::ClientConfig;
use schema_registry_converter::async_impl::avro::AvroEncoder;
use schema_registry_converter::async_impl::schema_registry::SrSettings;
use schema_registry_converter::avro_common::get_supplied_schema;
use schema_registry_converter::schema_registry_common::SubjectNameStrategy;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Duration;
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct ChatMessage {
pub user: String,
pub message: String,
}
impl AvroSchema for ChatMessage {
fn get_schema() -> Schema {
// Define the Avro schema for ChatMessage
Schema::parse_str(
r#"{
"type": "record",
"name": "ChatMessage",
"fields": [
{"name": "user", "type": "string"},
{"name": "message", "type": "string"}
]
}"#
).unwrap()
}
}
pub struct KafkaProducer<'a> {
producer: FutureProducer,
avro_encoder: Arc<AvroEncoder<'a>>,
topic: String,
}
impl KafkaProducer<'_> {
pub fn new(bootstrap_servers: String, schema_registry_url: String, topic: String) -> Self {
let producer: FutureProducer = ClientConfig::new()
.set("bootstrap.servers", bootstrap_servers)
.set("produce.offset.report", "true")
.set("message.timeout.ms", "5000")
.set("queue.buffering.max.messages", "10")
.create()
.expect("Producer creation error");
let sr_settings = SrSettings::new(schema_registry_url);
let avro_encoder = AvroEncoder::new(sr_settings);
Self {
producer,
topic,
avro_encoder: Arc::new(avro_encoder),
}
}
pub async fn produce<T: Serialize + apache_avro::AvroSchema>(&self, key: String, payload: T) -> bool {
let value_strategy = SubjectNameStrategy::TopicNameStrategyWithSchema(
self.topic.clone(),
true,
get_supplied_schema(&T::get_schema()),
);
let payload_bytes = match self
.avro_encoder
.encode_struct(payload, &value_strategy)
.await
{
Ok(bytes) => bytes,
Err(e) => panic!("Error encoding payload: {}", e),
};
let record = FutureRecord::to(&self.topic)
.key(&key)
.payload(&payload_bytes);
match self.producer.send(record, Duration::from_secs(10)).await {
Ok(_) => true,
Err(_) => false,
}
}
}