-
-
Save pavan538/3f04129f0acf3390e2533c1f213e4383 to your computer and use it in GitHub Desktop.
Adding a new branch to an Avro union
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import org.apache.avro.Schema; | |
import org.apache.avro.generic.GenericData; | |
import org.apache.avro.generic.GenericDatumReader; | |
import org.apache.avro.generic.GenericDatumWriter; | |
import org.apache.avro.generic.GenericRecordBuilder; | |
import org.apache.avro.io.BinaryDecoder; | |
import org.apache.avro.io.BinaryEncoder; | |
import org.apache.avro.io.DatumReader; | |
import org.apache.avro.io.DatumWriter; | |
import org.apache.avro.io.DecoderFactory; | |
import org.apache.avro.io.EncoderFactory; | |
public class TestUnionEvolution { | |
public static final Schema WRITER_SCHEMA = new Schema.Parser().parse( | |
"{\"type\": \"record\", \"name\": \"Test\", \"fields\": [" + | |
"{\"name\": \"myField\", \"default\": null, \"type\": [\"null\", \"string\", \"long\"]}" + | |
"]}" | |
); | |
public static final Schema READER_SCHEMA = new Schema.Parser().parse( | |
"{\"type\": \"record\", \"name\": \"Test\", \"fields\": [" + | |
"{\"name\": \"myField\", \"default\": null, \"type\": [\"null\", \"string\"]}" + | |
"]}" | |
); | |
public static byte[] encode(Object datum) throws IOException { | |
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | |
try { | |
BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(stream, null); | |
DatumWriter<Object> writer = new GenericDatumWriter<Object>(WRITER_SCHEMA); | |
writer.write(datum, encoder); | |
encoder.flush(); | |
return stream.toByteArray(); | |
} finally { | |
stream.close(); | |
} | |
} | |
public static Object decode(byte[] data) throws IOException { | |
BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(data, null); | |
DatumReader<Object> reader = new GenericDatumReader<Object>(WRITER_SCHEMA, READER_SCHEMA); | |
return reader.read(null, decoder); | |
} | |
public static void main(String[] args) throws IOException { | |
GenericData.Record record = new GenericRecordBuilder(WRITER_SCHEMA) | |
.set("myField", 42L) | |
.build(); | |
System.out.println(decode(encode(record))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment