use of org.apache.avro.io.Encoder in project camel by apache.
the class AvroDataFormat method marshal.
public void marshal(Exchange exchange, Object graph, OutputStream outputStream) throws Exception {
// the schema should be from the graph class name
Schema useSchema = actualSchema != null ? actualSchema : loadSchema(graph.getClass().getName());
DatumWriter<Object> datum = new SpecificDatumWriter<Object>(useSchema);
Encoder encoder = EncoderFactory.get().binaryEncoder(outputStream, null);
datum.write(graph, encoder);
encoder.flush();
}
use of org.apache.avro.io.Encoder in project avro-kafka-storm by ransilberman.
the class MainTest method testGenericRecord.
@Test
public void testGenericRecord() throws IOException, InterruptedException {
Schema.Parser parser = new Schema.Parser();
Schema schema = parser.parse(getClass().getResourceAsStream("LPEvent.avsc"));
GenericRecord datum = new GenericData.Record(schema);
datum.put("revision", 1L);
datum.put("siteId", "28280110");
datum.put("eventType", "PLine");
datum.put("timeStamp", System.currentTimeMillis());
datum.put("sessionId", "123456II");
Map<String, Schema> unions = new HashMap<String, Schema>();
List<Schema> typeList = schema.getField("subrecord").schema().getTypes();
for (Schema sch : typeList) {
unions.put(sch.getName(), sch);
}
GenericRecord plineDatum = new GenericData.Record(unions.get("pline"));
plineDatum.put("text", "How can I help you?");
plineDatum.put("lineType", 1);
plineDatum.put("repId", "REPID12345");
datum.put("subrecord", plineDatum);
ByteArrayOutputStream out = new ByteArrayOutputStream();
DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(schema);
Encoder encoder = EncoderFactory.get().binaryEncoder(out, null);
writer.write(datum, encoder);
encoder.flush();
out.close();
Message message = new Message(out.toByteArray());
Properties props = new Properties();
props.put("zk.connect", zkConnection);
Producer<Message, Message> producer = new kafka.javaapi.producer.Producer<Message, Message>(new ProducerConfig(props));
producer.send(new ProducerData<Message, Message>(topic, message));
}
use of org.apache.avro.io.Encoder in project voldemort by voldemort.
the class AvroGenericSerializer method toBytes.
public byte[] toBytes(Object object) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
Encoder encoder = new BinaryEncoder(output);
GenericDatumWriter<Object> datumWriter = null;
try {
datumWriter = new GenericDatumWriter<Object>(typeDef);
datumWriter.write(object, encoder);
encoder.flush();
} catch (IOException e) {
throw new SerializationException(e);
} finally {
SerializationUtils.close(output);
}
return output.toByteArray();
}
use of org.apache.avro.io.Encoder in project voldemort by voldemort.
the class AvroSpecificSerializer method toBytes.
public byte[] toBytes(T object) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
Encoder encoder = new BinaryEncoder(output);
SpecificDatumWriter<T> datumWriter = null;
try {
datumWriter = new SpecificDatumWriter<T>(clazz);
datumWriter.write(object, encoder);
encoder.flush();
} catch (IOException e) {
throw new SerializationException(e);
} finally {
SerializationUtils.close(output);
}
return output.toByteArray();
}
use of org.apache.avro.io.Encoder in project cdap by caskdata.
the class ClientMessagingService method performWriteRequest.
/**
* Makes a request to the server for writing to the messaging system
*
* @param request contains information about what to write
* @param publish {@code true} to make publish call, {@code false} to make store call.
* @return the response from the server
* @throws IOException if failed to perform the write operation
* @throws TopicNotFoundException if the topic to write to does not exist
*/
private HttpResponse performWriteRequest(StoreRequest request, boolean publish) throws IOException, TopicNotFoundException {
GenericRecord record = new GenericData.Record(Schemas.V1.PublishRequest.SCHEMA);
if (request.isTransactional()) {
record.put("transactionWritePointer", request.getTransactionWritePointer());
}
record.put("messages", convertPayloads(request));
// Encode the request as avro
ExposedByteArrayOutputStream os = new ExposedByteArrayOutputStream();
Encoder encoder = EncoderFactory.get().directBinaryEncoder(os, null);
DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(Schemas.V1.PublishRequest.SCHEMA);
datumWriter.write(record, encoder);
// Make the publish request
String writeType = publish ? "publish" : "store";
TopicId topicId = request.getTopicId();
HttpRequest httpRequest = remoteClient.requestBuilder(HttpMethod.POST, createTopicPath(topicId) + "/" + writeType).addHeader(HttpHeaders.CONTENT_TYPE, "avro/binary").withBody(os.toByteBuffer()).build();
HttpResponse response = remoteClient.execute(httpRequest);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
}
handleError(response, "Failed to " + writeType + " message to topic " + topicId);
return response;
}
Aggregations