Skip to content

Instantly share code, notes, and snippets.

@smitty-codes
Last active May 15, 2020 22:11
Show Gist options
  • Save smitty-codes/372705d16940e73aa940b3403b6814e4 to your computer and use it in GitHub Desktop.
Save smitty-codes/372705d16940e73aa940b3403b6814e4 to your computer and use it in GitHub Desktop.
Apache.Avro schemaless files
// Invoice class generated from a avro protocol file (.avpr) using the nuget package Apache.Avro.Tools
Invoice invoice = new Invoice();
invoice.invoiceno = 123;
// ...(more invoice properties set here)
// set destination
string filePath = String.Format(@"C:\AvroInvoices\{0}.avro", invoice.invoiceNo);
// write avro schemaless file - logic adapted from https://stackoverflow.com/q/36793657/194872
SpecificDatumWriter<Invoice> writer = new SpecificDatumWriter<Invoice>(Invoice._SCHEMA);
FileStream outStream = new FileStream(filePath, FileMode.Create, FileAccess.Write);
Avro.IO.BinaryEncoder binaryEncoder = new Avro.IO.BinaryEncoder(outStream);
writer.Write(invoice, binaryEncoder);
outStream.Close();
// read in our schemaless file to verify we can load into an invoice object
SpecificDatumReader<Invoice> reader = new SpecificDatumReader<Invoice>(Invoice._SCHEMA, Invoice._SCHEMA);
FileStream inStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
Avro.IO.BinaryDecoder binaryDecoder = new Avro.IO.BinaryDecoder(inStream);
Invoice inv = reader.Read(null, binaryDecoder);
inStream.Close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment