-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhello_world.rs
41 lines (34 loc) · 1.45 KB
/
hello_world.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// A hello world example of reading and writing avro data files
use anyhow::Error;
use avrow::from_value;
use avrow::Reader;
use avrow::Schema;
use avrow::Writer;
use std::str::FromStr;
use std::io::Cursor;
fn main() -> Result<(), Error> {
// Writing data
// Create a schema
let schema = Schema::from_str(r##""null""##)?;
// Create writer using schema and provide a buffer (implements Read) to write to
let mut writer = Writer::new(&schema, vec![])?;
// Write the data using write and creating a Value manually.
writer.write(())?;
// or the more convenient and intuitive serialize method that takes native Rust types.
writer.serialize(())?;
// retrieve the underlying buffer using the buffer method.
// TODO buffer is not intuive when using a file. into_inner is much better here.
let buf = writer.into_inner()?;
// Reading data
// Create Reader by providing a Read wrapped version of `buf`
let reader = Reader::new(Cursor::new(buf))?;
// Use iterator for reading data in an idiomatic manner.
for i in reader {
// reading values can fail due to decoding errors, so the return value of iterator is a Option<Result<Value>>
// it allows one to examine the failure reason on the underlying avro reader.
dbg!(&i);
// This value can be converted to a native Rust type using `from_value` method that uses serde underneath.
let _val: () = from_value(&i)?;
}
Ok(())
}