public static GenericRecord convertRecordSchema ( GenericRecord record , Schema newSchema ) throws IOException { if ( record . getSchema ( ) . equals ( newSchema ) ) { return record ; } try { BinaryDecoder decoder = new DecoderFactory ( ) . binaryDecoder ( recordToByteArray ( record ) , null ) ; DatumReader < GenericRecord > reader = new GenericDatumReader <> ( record . getSchema ( ) , newSchema ) ; return reader . read ( null , decoder ) ; } catch ( IOException e ) { throw new IOException ( String . format ( "Cannot convert avro record to new schema. Origianl schema = %s, new schema = %s" , record . getSchema ( ) , newSchema ) , e ) ; } }
Change the schema of an Avro record .