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;
}
};
}
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;
}
}
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));
}
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);
}
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;
}
Aggregations