Skip to content

Instantly share code, notes, and snippets.

@ept
Created December 14, 2015 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ept/5fd7c625969248b31e73 to your computer and use it in GitHub Desktop.
Save ept/5fd7c625969248b31e73 to your computer and use it in GitHub Desktop.
Adding a new branch to an Avro union
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