Search in sources :

Example 11 with Decoder

use of org.apache.avro.io.Decoder in project voldemort by voldemort.

the class AvroGenericSerializer method toObject.

public Object toObject(byte[] bytes) {
    Decoder decoder = DecoderFactory.defaultFactory().createBinaryDecoder(bytes, null);
    GenericDatumReader<Object> reader = null;
    try {
        reader = new GenericDatumReader<Object>(typeDef);
        return reader.read(null, decoder);
    } catch (IOException e) {
        throw new SerializationException(e);
    }
}
Also used : SerializationException(voldemort.serialization.SerializationException) IOException(java.io.IOException) Decoder(org.apache.avro.io.Decoder)

Example 12 with Decoder

use of org.apache.avro.io.Decoder in project voldemort by voldemort.

the class AvroReflectiveSerializer method toObject.

public T toObject(byte[] bytes) {
    Decoder decoder = DecoderFactory.defaultFactory().createBinaryDecoder(bytes, null);
    ReflectDatumReader<T> reader = null;
    try {
        reader = new ReflectDatumReader<T>(clazz);
        return reader.read(null, decoder);
    } catch (IOException e) {
        throw new SerializationException(e);
    }
}
Also used : SerializationException(voldemort.serialization.SerializationException) IOException(java.io.IOException) Decoder(org.apache.avro.io.Decoder)

Example 13 with Decoder

use of org.apache.avro.io.Decoder in project voldemort by voldemort.

the class AvroSpecificSerializer method toObject.

public T toObject(byte[] bytes) {
    Decoder decoder = DecoderFactory.defaultFactory().createBinaryDecoder(bytes, null);
    SpecificDatumReader<T> reader = null;
    try {
        reader = new SpecificDatumReader<T>(clazz);
        return reader.read(null, decoder);
    } catch (IOException e) {
        throw new SerializationException(e);
    }
}
Also used : SerializationException(voldemort.serialization.SerializationException) IOException(java.io.IOException) Decoder(org.apache.avro.io.Decoder)

Example 14 with Decoder

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

the class FetchHandler method poll.

@POST
@Path("poll")
public void poll(HttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception {
    TopicId topicId = new NamespaceId(namespace).topic(topic);
    // Currently only support avro
    if (!"avro/binary".equals(request.getHeader(HttpHeaders.Names.CONTENT_TYPE))) {
        throw new BadRequestException("Only avro/binary content type is supported.");
    }
    // Decode the poll request
    Decoder decoder = DecoderFactory.get().directBinaryDecoder(new ChannelBufferInputStream(request.getContent()), null);
    DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(Schemas.V1.ConsumeRequest.SCHEMA);
    // Fetch the messages
    CloseableIterator<RawMessage> iterator = fetchMessages(datumReader.read(null, decoder), topicId);
    try {
        responder.sendContent(HttpResponseStatus.OK, new MessagesBodyProducer(iterator, messageChunkSize), ImmutableMultimap.of(HttpHeaders.Names.CONTENT_TYPE, "avro/binary"));
    } catch (Throwable t) {
        iterator.close();
        throw t;
    }
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) BadRequestException(co.cask.cdap.common.BadRequestException) TopicId(co.cask.cdap.proto.id.TopicId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) Decoder(org.apache.avro.io.Decoder) GenericRecord(org.apache.avro.generic.GenericRecord) RawMessage(co.cask.cdap.messaging.data.RawMessage) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 15 with Decoder

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

the class StoreHandler method createStoreRequest.

/**
   * Creates a {@link StoreRequest} instance based on the given {@link HttpRequest}.
   */
private StoreRequest createStoreRequest(TopicId topicId, HttpRequest request) throws Exception {
    // Currently only support avro
    if (!"avro/binary".equals(request.getHeader(HttpHeaders.Names.CONTENT_TYPE))) {
        throw new BadRequestException("Only avro/binary content type is supported.");
    }
    Decoder decoder = DecoderFactory.get().directBinaryDecoder(new ChannelBufferInputStream(request.getContent()), null);
    DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(Schemas.V1.PublishRequest.SCHEMA);
    return new GenericRecordStoreRequest(topicId, datumReader.read(null, decoder));
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) BadRequestException(co.cask.cdap.common.BadRequestException) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) Decoder(org.apache.avro.io.Decoder) GenericRecord(org.apache.avro.generic.GenericRecord)

Aggregations

Decoder (org.apache.avro.io.Decoder)16 GenericDatumReader (org.apache.avro.generic.GenericDatumReader)8 GenericRecord (org.apache.avro.generic.GenericRecord)8 IOException (java.io.IOException)6 SerializationException (voldemort.serialization.SerializationException)4 Schema (org.apache.avro.Schema)3 ChannelBufferInputStream (org.jboss.netty.buffer.ChannelBufferInputStream)3 BadRequestException (co.cask.cdap.common.BadRequestException)2 NamespaceId (co.cask.cdap.proto.id.NamespaceId)2 TopicId (co.cask.cdap.proto.id.TopicId)2 AvroAdapter (com.linkedin.data.avro.AvroAdapter)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 RawMessage (co.cask.cdap.messaging.data.RawMessage)1 InputSupplier (com.google.common.io.InputSupplier)1 BufferedReader (java.io.BufferedReader)1 EOFException (java.io.EOFException)1 FileInputStream (java.io.FileInputStream)1