Search in sources :

Example 16 with GenericDatumReader

use of org.apache.avro.generic.GenericDatumReader 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 17 with GenericDatumReader

use of org.apache.avro.generic.GenericDatumReader 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 18 with GenericDatumReader

use of org.apache.avro.generic.GenericDatumReader 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 19 with GenericDatumReader

use of org.apache.avro.generic.GenericDatumReader in project haivvreo by jghoman.

the class AvroGenericRecordWritable method readFields.

@Override
public void readFields(DataInput in) throws IOException {
    Schema schema = Schema.parse(in.readUTF());
    record = new GenericData.Record(schema);
    binaryDecoder = DecoderFactory.defaultFactory().createBinaryDecoder((InputStream) in, binaryDecoder);
    GenericDatumReader<GenericRecord> gdr = new GenericDatumReader<GenericRecord>(schema);
    record = gdr.read(record, binaryDecoder);
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord) GenericData(org.apache.avro.generic.GenericData)

Example 20 with GenericDatumReader

use of org.apache.avro.generic.GenericDatumReader in project haivvreo by jghoman.

the class TestThatEvolvedSchemasActAsWeWant method resolvedSchemasShouldReturnReaderSchema.

@Test
public void resolvedSchemasShouldReturnReaderSchema() throws IOException {
    // Need to verify that when reading a datum with an updated reader schema
    // that the datum then returns the reader schema as its own, since we
    // depend on this behavior in order to avoid re-encoding the datum
    // in the serde.
    String v0 = "{\n" + "    \"namespace\": \"com.linkedin.haivvreo\",\n" + "    \"name\": \"SomeStuff\",\n" + "    \"type\": \"record\",\n" + "    \"fields\": [\n" + "        {\n" + "            \"name\":\"v0\",\n" + "            \"type\":\"string\"\n" + "        }\n" + "    ]\n" + "}";
    String v1 = "{\n" + "    \"namespace\": \"com.linkedin.haivvreo\",\n" + "    \"name\": \"SomeStuff\",\n" + "    \"type\": \"record\",\n" + "    \"fields\": [\n" + "        {\n" + "            \"name\":\"v0\",\n" + "            \"type\":\"string\"\n" + "        },\n" + "        {\n" + "            \"name\":\"v1\",\n" + "            \"type\":\"string\",\n" + "            \"default\":\"v1_default\"" + "        }\n" + "    ]\n" + "}";
    Schema[] schemas = { Schema.parse(v0), Schema.parse(v1) };
    // Encode a schema with v0, write out.
    GenericRecord record = new GenericData.Record(schemas[0]);
    record.put("v0", "v0 value");
    assertTrue(GenericData.get().validate(schemas[0], record));
    // Write datum out to a stream
    GenericDatumWriter<GenericRecord> gdw = new GenericDatumWriter<GenericRecord>(schemas[0]);
    DataFileWriter<GenericRecord> dfw = new DataFileWriter<GenericRecord>(gdw);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    dfw.create(schemas[0], baos);
    dfw.append(record);
    dfw.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    GenericDatumReader<GenericRecord> gdr = new GenericDatumReader<GenericRecord>();
    gdr.setExpected(schemas[1]);
    DataFileStream<GenericRecord> dfs = new DataFileStream<GenericRecord>(bais, gdr);
    assertTrue(dfs.hasNext());
    GenericRecord next = dfs.next();
    assertEquals("v0 value", next.get("v0").toString());
    assertEquals("v1_default", next.get("v1").toString());
    // Now the most important check - when we query this record for its schema,
    // we should get back the latest, reader schema:
    assertEquals(schemas[1], next.getSchema());
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Schema(org.apache.avro.Schema) DataFileWriter(org.apache.avro.file.DataFileWriter) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataFileStream(org.apache.avro.file.DataFileStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GenericRecord(org.apache.avro.generic.GenericRecord) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.junit.Test)

Aggregations

GenericDatumReader (org.apache.avro.generic.GenericDatumReader)47 GenericRecord (org.apache.avro.generic.GenericRecord)32 Schema (org.apache.avro.Schema)20 IOException (java.io.IOException)15 File (java.io.File)10 DataFileStream (org.apache.avro.file.DataFileStream)10 Decoder (org.apache.avro.io.Decoder)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 GenericData (org.apache.avro.generic.GenericData)7 DataFileReader (org.apache.avro.file.DataFileReader)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 JsonDecoder (org.apache.avro.io.JsonDecoder)5 ParseException (io.druid.java.util.common.parsers.ParseException)4 FileInputStream (java.io.FileInputStream)4 DataFileWriter (org.apache.avro.file.DataFileWriter)4 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 Map (java.util.Map)3