public static byte[] recordToByteArray(GenericRecord record) throws IOException {
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
      Encoder encoder = EncoderFactory.get().directBinaryEncoder(out, null);
      DatumWriter<GenericRecord> writer = new GenericDatumWriter<>(record.getSchema());
      writer.write(record, encoder);
      byte[] byteArray = out.toByteArray();
      return byteArray;
    }
  }
public static byte [ ] recordToByteArray ( GenericRecord record ) throws IOException { try ( ByteArrayOutputStream out = new ByteArrayOutputStream ( ) ) { Encoder encoder = EncoderFactory . get ( ) . directBinaryEncoder ( out , null ) ; DatumWriter < GenericRecord > writer = new GenericDatumWriter < > ( record . getSchema ( ) ) ; writer . write ( record , encoder ) ; byte [ ] byteArray = out . toByteArray ( ) ; return byteArray ; } }
Convert a GenericRecord to a byte array.