-
Hello, Thanks for this useful crate 👍 I'm a bit new to Rust and I must admit I'm confused on how to actually access some sort of Struct or Hashmap from your lib... I read Avro messages from Kafka, I'd like a way to access the data inside it (key, value). It looks to me (so far) that I need a way to deserialize a The The gist of my question is, using this crate, how one is supposed to get a workable data object to play with? PS: I'm using easy async. loop {
match consumer.recv().await {
Err(e) => warn!("Kafka error: {}", e),
Ok(m) => {
let decoded = decoder.decode(m.payload()).await;
match decoded {
Ok(v) => {
// this works tho I dunno how to access the actual data inside
println!("name {:?}", v.name.unwrap());
// what should I do to transform v.value into a struct deserialized by avro_rs?
},
Err(e) => println!("err {:?}", e),
}
}
};
} Thanks for your help, I'll sure propose a documentation update once I nail it down 🥇 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 12 replies
-
It depends a lot on whether you know the schema of the data up front. If you don't you get back a If you do know the schema you can do something similar to using the struct defined I fully agree this later option is kind of hidden, so I would very much appreciate a PR to make this use more clear. |
Beta Was this translation helpful? Give feedback.
-
As a follow-up in case someone looks for the answer before a new release of apache avro, here is a sample code that demonstrates how to map Avro data to a struct. #[derive(Default, Debug, Deserialize)]
struct Navigation {
string_field: String,
nullable_string_field: Option<String>,
}
// messages is rdkafka::message::OwnedMessage (or BorrowedMessage as you wish)
for msg in &messages {
let decode_result = avro_decoder.decode(msg.payload()).await;
match decode_result {
Ok(decoded) => {
let _navigation: Navigation = match &decoded.value {
Value::Record(_) => from_value::<Navigation>(&decoded.value).unwrap(),
_ => unimplemented!("from_value did not return a Value::Record"),
};
}
Err(err) => {
eprintln!("avro decode error {}", err);
break;
}
}
} |
Beta Was this translation helpful? Give feedback.
It depends a lot on whether you know the schema of the data up front. If you don't you get back a
Value::Record
which contains the values, but can be tedious to get the information from. Like inschema_registry_converter/src/async_impl/avro.rs
Line 593 in 95f48fb
If you do know the schema you can do something similar to
schema_registry_converter/src/async_impl/avro.rs
Line 613 in 95f48fb
using the struct defined
schema_regi…