This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
Added example reading Avro produced by Kafka #1151
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use arrow2::{ | ||
datatypes::{DataType, Field}, | ||
error::Error, | ||
io::avro, | ||
}; | ||
|
||
fn read_schema_id<R: std::io::Read>(reader: &mut R) -> Result<i32, Error> { | ||
let mut header = [0; 5]; | ||
reader.read_exact(&mut header)?; | ||
|
||
if header[0] != 0 { | ||
return Err(Error::ExternalFormat( | ||
"Avro requires the first byte to be a zero".to_string(), | ||
)); | ||
} | ||
Ok(i32::from_be_bytes(header[1..].try_into().unwrap())) | ||
} | ||
|
||
fn read_block<R: std::io::Read>(reader: &mut R, block: &mut avro::Block) -> Result<(), Error> { | ||
// (we could lump multiple records together by extending the block instead of clearing) | ||
block.data.clear(); | ||
reader.read_to_end(&mut block.data)?; | ||
block.number_of_rows = 1; | ||
Ok(()) | ||
} | ||
|
||
fn main() -> Result<(), Error> { | ||
// say we received the event from Kafka | ||
let data = &[0, 0, 0, 0, 1, 6, 115, 99, 105, 6, 109, 97, 115]; | ||
let mut stream = std::io::Cursor::new(data); | ||
|
||
// we can fetch its schema_id from the registry via: | ||
let _schema_id = read_schema_id(&mut stream)?; | ||
|
||
// say that from the registry we concluded that this schema has fields | ||
let avro_fields = vec![ | ||
avro_schema::Schema::String(None), | ||
avro_schema::Schema::String(None), | ||
]; | ||
// (which we map to arrow fields as) | ||
let fields = vec![ | ||
Field::new("first_name", DataType::Utf8, false), | ||
Field::new("last_name", DataType::Utf8, false), | ||
]; | ||
|
||
// the below allow us to read it to arrow (a chunk of a single element) | ||
let mut block = avro::Block::default(); | ||
read_block(&mut stream, &mut block)?; | ||
|
||
let chunk = avro::read::deserialize(&block, &fields, &avro_fields, &[true, true])?; | ||
|
||
println!("{chunk:?}"); | ||
Ok(()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that this is already merged, but I would suggest changing this to u32 instead of i32.
I don't know where the specification for the schema_id is, but the API documentation for Confluent's schema registry specifies that version (schema_id) should be in the range [1, 2^31 - 1].