use of org.apache.avro.generic.GenericDatumWriter in project core by s4.
the class AvroUtils method serialize.
public static byte[] serialize(Schema schema, GenericRecord content) throws Exception {
GenericDatumWriter<GenericRecord> serveWriter = new GenericDatumWriter<GenericRecord>(schema);
ByteArrayOutputStream out = new ByteArrayOutputStream();
serveWriter.write(content, new BinaryEncoder(out));
return out.toByteArray();
}
use of org.apache.avro.generic.GenericDatumWriter in project samza by apache.
the class TestAvroFileHdfsReader method writeTestEventsToFile.
public static void writeTestEventsToFile(String path, int numEvents) throws Exception {
Schema schema = Schema.parse(TestAvroFileHdfsReader.class.getResourceAsStream("/reader/TestEvent.avsc"));
File file = new File(path);
DatumWriter<GenericRecord> writer = new GenericDatumWriter<>(schema);
DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<>(writer);
dataFileWriter.create(schema, file);
for (int i = 0; i < numEvents; i++) {
GenericRecord datum = new GenericData.Record(schema);
datum.put(FIELD_1, i);
datum.put(FIELD_2, "string_" + i);
dataFileWriter.append(datum);
}
dataFileWriter.close();
}
use of org.apache.avro.generic.GenericDatumWriter 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;
}
use of org.apache.avro.generic.GenericDatumWriter in project cdap by caskdata.
the class LoggingEventSerializer method toBytes.
/**
* Encodes a {@link ILoggingEvent} to byte array.
*/
public byte[] toBytes(ILoggingEvent event) {
event.prepareForDeferredProcessing();
ByteArrayOutputStream out = new ByteArrayOutputStream();
BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder(out, null);
GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<>(getAvroSchema());
try {
writer.write(toGenericRecord(event), encoder);
} catch (IOException e) {
// This shouldn't happen since we are writing to byte array output stream.
throw Throwables.propagate(e);
}
return out.toByteArray();
}
Aggregations