code: 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;
      }
    }
docs: Convert a GenericRecord to a byte array.