Search in sources :

Example 6 with Encoder

use of org.apache.avro.io.Encoder 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 7 with Encoder

use of org.apache.avro.io.Encoder 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 8 with Encoder

use of org.apache.avro.io.Encoder in project cdap by caskdata.

the class ClientMessagingService method encodeRollbackDetail.

/**
   * Encodes the given {@link RollbackDetail} as expected by the rollback call. This method is rarely used
   * as the call to {@link #rollback(TopicId, RollbackDetail)} expects a {@link ClientRollbackDetail} which
   * already contains the encoded bytes.
   *
   * This method looks very similar to the {@code StoreHandler.encodeRollbackDetail} method, but is intended to have
   * them separated. This is to allow client side classes be moved to separate module without any dependency
   * on the server side (this can also be done with a util method in a common module, but it is kind of overkill
   * for a simple method like this for now).
   */
private ByteBuffer encodeRollbackDetail(RollbackDetail rollbackDetail) throws IOException {
    // Constructs the response object as GenericRecord
    Schema schema = Schemas.V1.PublishResponse.SCHEMA;
    GenericRecord record = new GenericData.Record(schema);
    record.put("transactionWritePointer", rollbackDetail.getTransactionWritePointer());
    GenericRecord rollbackRange = new GenericData.Record(schema.getField("rollbackRange").schema());
    rollbackRange.put("startTimestamp", rollbackDetail.getStartTimestamp());
    rollbackRange.put("startSequenceId", rollbackDetail.getStartSequenceId());
    rollbackRange.put("endTimestamp", rollbackDetail.getEndTimestamp());
    rollbackRange.put("endSequenceId", rollbackDetail.getEndSequenceId());
    record.put("rollbackRange", rollbackRange);
    ExposedByteArrayOutputStream os = new ExposedByteArrayOutputStream();
    Encoder encoder = EncoderFactory.get().directBinaryEncoder(os, null);
    DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(Schemas.V1.PublishRequest.SCHEMA);
    datumWriter.write(record, encoder);
    return os.toByteBuffer();
}
Also used : Encoder(org.apache.avro.io.Encoder) Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 9 with Encoder

use of org.apache.avro.io.Encoder in project cdap by caskdata.

the class StoreHandler method encodeRollbackDetail.

/**
   * Encodes the {@link RollbackDetail} object as avro record based on the {@link Schemas.V1.PublishResponse#SCHEMA}.
   */
private ChannelBuffer encodeRollbackDetail(RollbackDetail rollbackDetail) throws IOException {
    Schema schema = Schemas.V1.PublishResponse.SCHEMA;
    // Constructs the response object as GenericRecord
    GenericRecord response = new GenericData.Record(schema);
    response.put("transactionWritePointer", rollbackDetail.getTransactionWritePointer());
    GenericRecord rollbackRange = new GenericData.Record(schema.getField("rollbackRange").schema());
    rollbackRange.put("startTimestamp", rollbackDetail.getStartTimestamp());
    rollbackRange.put("startSequenceId", rollbackDetail.getStartSequenceId());
    rollbackRange.put("endTimestamp", rollbackDetail.getEndTimestamp());
    rollbackRange.put("endSequenceId", rollbackDetail.getEndSequenceId());
    response.put("rollbackRange", rollbackRange);
    // For V1 PublishResponse, it contains an union(long, null) and then 2 longs and 2 integers,
    // hence the max size is 38
    // (union use 1 byte, long max size is 9 bytes, integer max size is 5 bytes in avro binary encoding)
    ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(38);
    Encoder encoder = EncoderFactory.get().directBinaryEncoder(new ChannelBufferOutputStream(buffer), null);
    DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema);
    datumWriter.write(response, encoder);
    return buffer;
}
Also used : ChannelBufferOutputStream(org.jboss.netty.buffer.ChannelBufferOutputStream) Encoder(org.apache.avro.io.Encoder) Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) GenericRecord(org.apache.avro.generic.GenericRecord) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 10 with Encoder

use of org.apache.avro.io.Encoder in project storm by apache.

the class AvroSerializer method write.

@Override
public ByteBuffer write(List<Object> data, ByteBuffer buffer) {
    Preconditions.checkArgument(data != null && data.size() == fieldNames.size(), "Invalid schemas");
    try {
        Schema schema = schemas.getSchema(schemaString);
        GenericRecord record = new GenericData.Record(schema);
        for (int i = 0; i < fieldNames.size(); i++) {
            record.put(fieldNames.get(i), data.get(i));
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DatumWriter<GenericRecord> writer = new GenericDatumWriter<>(record.getSchema());
        Encoder encoder = EncoderFactory.get().directBinaryEncoder(out, null);
        writer.write(record, encoder);
        encoder.flush();
        byte[] bytes = out.toByteArray();
        out.close();
        return ByteBuffer.wrap(bytes);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Encoder(org.apache.avro.io.Encoder) Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) IOException(java.io.IOException) GenericRecord(org.apache.avro.generic.GenericRecord)

Aggregations

Encoder (org.apache.avro.io.Encoder)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)12 GenericRecord (org.apache.avro.generic.GenericRecord)12 IOException (java.io.IOException)10 BinaryEncoder (org.apache.avro.io.BinaryEncoder)10 Schema (org.apache.avro.Schema)7 SerializationException (voldemort.serialization.SerializationException)5 SpecificDatumWriter (org.apache.avro.specific.SpecificDatumWriter)3 AvroAdapter (com.linkedin.data.avro.AvroAdapter)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 Producer (kafka.javaapi.producer.Producer)2 Message (kafka.message.Message)2 ProducerConfig (kafka.producer.ProducerConfig)2 Test (org.junit.Test)2 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)1 TopicId (co.cask.cdap.proto.id.TopicId)1 HttpRequest (co.cask.common.http.HttpRequest)1