use of org.apache.avro.io.BinaryEncoder in project voldemort by voldemort.
the class AvroVersionedGenericSerializer method toBytes.
/*
* Serialize a given object using a non latest schema With auto rebootstrap
* the client gets the latest schema updated on the server However an
* application may still create objects using an old schema this lets us
* serialize those objects without an exception
*/
private byte[] toBytes(Object object, Schema writer, Integer writerVersion) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
Encoder encoder = new BinaryEncoder(output);
GenericDatumWriter<Object> datumWriter = null;
output.write(writerVersion.byteValue());
try {
datumWriter = new GenericDatumWriter<Object>(writer);
datumWriter.write(object, encoder);
encoder.flush();
} catch (IOException e) {
throw new SerializationException(e);
} catch (SerializationException sE) {
throw sE;
} finally {
SerializationUtils.close(output);
}
return output.toByteArray();
}
use of org.apache.avro.io.BinaryEncoder in project voldemort by voldemort.
the class AvroVersionedGenericSerializer method toBytes.
public byte[] toBytes(Object object) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
Encoder encoder = new BinaryEncoder(output);
GenericDatumWriter<Object> datumWriter = null;
output.write(newestVersion.byteValue());
try {
datumWriter = new GenericDatumWriter<Object>(typeDef);
datumWriter.write(object, encoder);
encoder.flush();
} catch (SerializationException sE) {
throw sE;
} catch (IOException e) {
throw new SerializationException(e);
} catch (Exception aIOBE) {
// probably the object sent to us was not created using the latest
// schema
// We simply check the old version number and serialize it using the
// old schema version
Schema writer = ((GenericContainer) object).getSchema();
Integer writerVersion = getSchemaVersion(writer);
return toBytes(object, writer, writerVersion);
} finally {
SerializationUtils.close(output);
}
return output.toByteArray();
}
use of org.apache.avro.io.BinaryEncoder in project voldemort by voldemort.
the class AvroReflectiveSerializer method toBytes.
public byte[] toBytes(T object) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
Encoder encoder = new BinaryEncoder(output);
ReflectDatumWriter<T> datumWriter = null;
try {
datumWriter = new ReflectDatumWriter<T>(clazz);
datumWriter.write(object, encoder);
encoder.flush();
} catch (IOException e) {
throw new SerializationException(e);
} finally {
SerializationUtils.close(output);
}
return output.toByteArray();
}
use of org.apache.avro.io.BinaryEncoder in project eiger by wlloyd.
the class SerDeUtils method serializeWithSchema.
/**
* Serializes a single object along with its Schema. NB: For performance critical areas, it is <b>much</b>
* more efficient to store the Schema independently.
* @param o Object to serialize
*/
public static <T extends SpecificRecord> ByteBuffer serializeWithSchema(T o) throws IOException {
OutputBuffer buff = new OutputBuffer();
BinaryEncoder enc = new BinaryEncoder(buff);
enc.writeString(new Utf8(o.getSchema().toString()));
SpecificDatumWriter<T> writer = new SpecificDatumWriter<T>(o.getSchema());
writer.write(o, enc);
enc.flush();
return ByteBuffer.wrap(buff.asByteArray());
}
use of org.apache.avro.io.BinaryEncoder in project cdap by caskdata.
the class HiveExploreServiceStreamTest method createAvroEvent.
private byte[] createAvroEvent(org.apache.avro.Schema schema, Object... values) throws IOException {
GenericRecordBuilder builder = new GenericRecordBuilder(schema);
int i = 0;
for (org.apache.avro.Schema.Field field : schema.getFields()) {
builder.set(field.name(), values[i]);
i++;
}
GenericRecord record = builder.build();
ByteArrayOutputStream out = new ByteArrayOutputStream();
BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null);
DatumWriter<GenericRecord> writer = new GenericDatumWriter<>(schema);
writer.write(record, encoder);
encoder.flush();
out.close();
return out.toByteArray();
}
Aggregations