Search in sources :

Example 6 with BinaryEncoder

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();
}
Also used : SerializationException(voldemort.serialization.SerializationException) BinaryEncoder(org.apache.avro.io.BinaryEncoder) Encoder(org.apache.avro.io.Encoder) BinaryEncoder(org.apache.avro.io.BinaryEncoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 7 with BinaryEncoder

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();
}
Also used : SerializationException(voldemort.serialization.SerializationException) BinaryEncoder(org.apache.avro.io.BinaryEncoder) Encoder(org.apache.avro.io.Encoder) BinaryEncoder(org.apache.avro.io.BinaryEncoder) Schema(org.apache.avro.Schema) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IOException(java.io.IOException) SerializationException(voldemort.serialization.SerializationException)

Example 8 with BinaryEncoder

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();
}
Also used : SerializationException(voldemort.serialization.SerializationException) BinaryEncoder(org.apache.avro.io.BinaryEncoder) BinaryEncoder(org.apache.avro.io.BinaryEncoder) Encoder(org.apache.avro.io.Encoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 9 with BinaryEncoder

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());
}
Also used : BinaryEncoder(org.apache.avro.io.BinaryEncoder) Utf8(org.apache.avro.util.Utf8) OutputBuffer(org.apache.cassandra.io.util.OutputBuffer) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 10 with BinaryEncoder

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();
}
Also used : BinaryEncoder(org.apache.avro.io.BinaryEncoder) Schema(co.cask.cdap.api.data.schema.Schema) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) GenericRecord(org.apache.avro.generic.GenericRecord)

Aggregations

BinaryEncoder (org.apache.avro.io.BinaryEncoder)29 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)17 GenericRecord (org.apache.avro.generic.GenericRecord)17 IOException (java.io.IOException)14 Encoder (org.apache.avro.io.Encoder)10 SerializationException (voldemort.serialization.SerializationException)5 SpecificDatumWriter (org.apache.avro.specific.SpecificDatumWriter)4 ArrayList (java.util.ArrayList)3 DbusEventInfo (com.linkedin.databus.core.DbusEventInfo)2 SQLException (java.sql.SQLException)2 Properties (java.util.Properties)2 Producer (kafka.javaapi.producer.Producer)2 KeyedMessage (kafka.producer.KeyedMessage)2 ProducerConfig (kafka.producer.ProducerConfig)2 Schema (org.apache.avro.Schema)2 GenericRecordBuilder (org.apache.avro.generic.GenericRecordBuilder)2 EncoderFactory (org.apache.avro.io.EncoderFactory)2 OutputBuffer (org.apache.cassandra.io.util.OutputBuffer)2 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)2