Last active
May 15, 2020 22:11
-
-
Save smitty-codes/372705d16940e73aa940b3403b6814e4 to your computer and use it in GitHub Desktop.
Apache.Avro schemaless files
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
// 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