Skip to content

QuickAvroWrite

Cinchoo edited this page Feb 28, 2021 · 1 revision

Writing Avro file

To generate Avro file, use the ChoAvroWriter component to generate it. Sample below shows how to create Avro file (Emp.avro)

(Avro is binary format, sample shows the payload in JSON format for illustration purpose)

[
 {
  "Id":"1",
  "Name":"Mark"
 },
 {
  "Id":"2",
  "Name":"Jason"
 }
]

Write using POCO object

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
}
List<Employee> objs = new List<Employee>();
objs.Add(new Employee() { Id = 1, Name = "Tom" });
objs.Add(new Employee() { Id = 2, Name = "Mark" });

using (var parser = new ChoAvroWriter<Employee>("Emp.avro"))
{
    parser.Write(objs);
}

Write each object

using (var parser = new ChoAvroWriter("Emp.avro"))
{
    Employee rec1 = new Employee();
    rec1.Id = 1;
    rec1.Name = "Mark";
    parser.Write(item);

    Employee rec1 = new Employee();
    rec1.Id = 2;
    rec1.Name = "Jason";
    parser.Write(item);
}