...

/

Avro: Code Generation

Avro: Code Generation

This lesson demonstrates how to work with Avro while using generated code.

We'll cover the following...

Avro with Code Generation

The previous lesson demonstrated a program that used GenericRecord interface to serialize and deserialize data. This approach requires us to cast the individual fields of the record to the right data-type. For instance, if we want to print the model year of each car record, the program would look like as this:

Press + to interact
import org.apache.avro.file.DataFileReader;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumReader;
import org.apache.avro.Schema;
import java.io.File;
import java.io.IOException;
public class AvroReadExample {
public static void main(String[] args) throws IOException {
(new AvroReadExample()).deserializeAvroFile();
}
public void deserializeAvroFile() throws IOException {
DatumReader<GenericRecord> datumReader = new GenericDatumReader<>();
DataFileReader<GenericRecord> dataFileReader = new DataFileReader<>(new File("./fancyCars.avro"), datumReader);
System.out.println();
System.out.println("Car Years :");
GenericRecord record = null;
while (dataFileReader.hasNext()) {
record = dataFileReader.next(record);
int carYear = (int) record.get("year");
System.out.println(carYear);
}
}
}

In line 25, we explicitly cast the object returned from the get() method to an integer. The code will not compile otherwise. ...