This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Code to deserialize

By: a guest on Oct 15th, 2013  |  syntax: None  |  size: 1.98 KB  |  views: 67  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.cerner.avro.test;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.lang.reflect.InvocationTargetException;
  7.  
  8. import org.apache.avro.Schema;
  9. import org.apache.avro.generic.GenericData;
  10. import org.apache.avro.generic.GenericDatumReader;
  11. import org.apache.avro.generic.GenericDatumWriter;
  12. import org.apache.avro.generic.GenericRecord;
  13. import org.apache.avro.io.DatumReader;
  14. import org.apache.avro.io.Decoder;
  15. import org.apache.avro.io.DecoderFactory;
  16. import org.apache.avro.io.Encoder;
  17. import org.apache.avro.io.EncoderFactory;
  18.  
  19. public class TestAvroPassivity {
  20.  
  21.     @SuppressWarnings({ "deprecation", "rawtypes", "unchecked" })
  22.     public static void main(String[] args) {
  23.  
  24.         File schemaFile_10 = new File("schema_old");
  25.         Schema schema_10 = Schema.parse(schemaFile_10);
  26.  
  27.         File schemaFile_11 = new File("schema_new");
  28.         Schema schema_11 = Schema.parse(schemaFile_11);
  29.  
  30.         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  31.         Encoder e = EncoderFactory.get().binaryEncoder(outputStream, null);
  32.         GenericDatumWriter w = new GenericDatumWriter(schema_10);
  33.  
  34.         // Populate data with 1.0 data
  35.         GenericRecord r = new GenericData.Record(schema_10);
  36.         r.put("employeeId", "employeeId");
  37.         r.put("personId", "personId");
  38.  
  39.         // Encode
  40.         w.write(r, e);
  41.         e.flush();
  42.  
  43.         byte[] encodedByteArray = outputStream.toByteArray();
  44.         String encodedString = outputStream.toString();
  45.  
  46.         System.out.println("encodedString: " + encodedString);
  47.  
  48.         DatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>(schema_11);
  49.         Decoder decoder = DecoderFactory.get().binaryDecoder(encodedByteArray, null);
  50.         GenericRecord result = reader.read(null, decoder);
  51.         System.out.println(result.get("employeeId").toString());
  52.         System.out.println(result.get("personId").toString());
  53.     }
  54. }
clone this paste RAW Paste Data