Search in sources :

Example 6 with Decoder

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

the class AvroStreamBodyConsumerTest method generateFile.

@Override
protected ContentInfo generateFile(final int recordCount) throws IOException {
    return new FileContentInfo(generateAvroFile(TMP_FOLDER.newFile(), recordCount)) {

        @Override
        public boolean verify(Map<String, String> headers, InputSupplier<? extends InputStream> contentSupplier) throws IOException {
            // Deserialize and verify the records
            Decoder decoder = DecoderFactory.get().binaryDecoder(contentSupplier.getInput(), null);
            DatumReader<Record> reader = new ReflectDatumReader<>(Record.class);
            reader.setSchema(new Schema.Parser().parse(headers.get("schema")));
            for (int i = 0; i < recordCount; i++) {
                Record record = reader.read(null, decoder);
                if (i != record.id) {
                    return false;
                }
                if (!("Record number " + i).equals(record.name)) {
                    return false;
                }
            }
            return true;
        }
    };
}
Also used : InputStream(java.io.InputStream) Decoder(org.apache.avro.io.Decoder) ReflectDatumReader(org.apache.avro.reflect.ReflectDatumReader) Map(java.util.Map) InputSupplier(com.google.common.io.InputSupplier)

Example 7 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 8 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)

Example 9 with Decoder

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

the class StoreHandler method rollback.

@POST
@Path("/rollback")
public void rollback(HttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception {
    TopicId topicId = new NamespaceId(namespace).topic(topic);
    Decoder decoder = DecoderFactory.get().directBinaryDecoder(new ChannelBufferInputStream(request.getContent()), null);
    DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(Schemas.V1.PublishResponse.SCHEMA);
    messagingService.rollback(topicId, new GenericRecordRollbackDetail(datumReader.read(null, decoder)));
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) 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) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 10 with Decoder

use of org.apache.avro.io.Decoder in project rest.li by linkedin.

the class TestSchemaTranslator method genericRecordFromString.

public static GenericRecord genericRecordFromString(String jsonString, Schema writerSchema, Schema readerSchema) throws IOException {
    GenericDatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>(writerSchema, readerSchema);
    byte[] bytes = jsonString.getBytes(Data.UTF_8_CHARSET);
    Decoder binaryDecoder = DecoderFactory.defaultFactory().createBinaryDecoder(bytes, null);
    GenericRecord record = reader.read(null, binaryDecoder);
    return record;
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) GenericRecord(org.apache.avro.generic.GenericRecord) Decoder(org.apache.avro.io.Decoder)

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