I want to read an avro object with a 2nd schema. The below example is to write data into a file with schema 1 and read it from the file with schema 2. I am wondering how to read the data with schema 2 but without writing into a file.
// write it out to a file
final DatumWriter<CustomerV1> datumWriter = new SpecificDatumWriter<>(CustomerV1.class);
final DataFileWriter<CustomerV1> dataFileWriter = new DataFileWriter<>(datumWriter);
dataFileWriter.create(customerV1.getSchema(), new File("customerV1.avro"));
dataFileWriter.append(customerV1);
dataFileWriter.close();
System.out.println("successfully wrote customerV1.avro");
// read it using the v2 schema
System.out.println("Reading customerV1.avro with v2 schema");
final File file = new File("customerv1.avro");
final DatumReader<CustomerV2> datumReaderV2 = new SpecificDatumReader<>(CustomerV2.class);
final DataFileReader<CustomerV2> dataFileReaderV2 = new DataFileReader<>(file, datumReaderV2);
while (dataFileReaderV2.hasNext()) {
CustomerV2 customerV2read = dataFileReaderV2.next();
System.out.println("Customer V2 = " + customerV2read.toString());
}