0

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());
        }
İsmail Y.
  • 3,579
  • 5
  • 21
  • 29
Shan
  • 177
  • 3
  • 12
  • Instead of a File, write it to an OutputStream https://avro.apache.org/docs/1.7.6/api/java/org/apache/avro/file/DataFileWriter.html https://stackoverflow.com/questions/17595091/how-to-create-a-new-java-io-file-in-memory – drum Feb 17 '21 at 20:17
  • Understood! Thanks a lot! – Shan Feb 17 '21 at 20:29

0 Answers0