use of org.apache.avro.specific.SpecificDatumWriter in project gora by apache.
the class AvroSerializerUtil method serializer.
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> byte[] serializer(T value, Schema schema) throws IOException {
SpecificDatumWriter writer = writerMap.get(schema.getFullName());
if (writer == null) {
// ignore dirty bits
writer = new SpecificDatumWriter(schema);
writerMap.put(schema.getFullName(), writer);
}
BinaryEncoder encoderFromCache = encoders.get();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
outputStream.set(bos);
BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder(bos, null);
if (encoderFromCache == null) {
encoders.set(encoder);
}
//reset the buffers
ByteArrayOutputStream os = outputStream.get();
os.reset();
writer.write(value, encoder);
encoder.flush();
byte[] byteValue = os.toByteArray();
return byteValue;
}
use of org.apache.avro.specific.SpecificDatumWriter in project sling by apache.
the class AvroContentSerializer method exportToStream.
@Override
public void exportToStream(ResourceResolver resourceResolver, DistributionExportOptions options, OutputStream outputStream) throws DistributionException {
DatumWriter<AvroShallowResource> datumWriter = new SpecificDatumWriter<AvroShallowResource>(AvroShallowResource.class);
DataFileWriter<AvroShallowResource> writer = new DataFileWriter<AvroShallowResource>(datumWriter);
try {
writer.create(schema, outputStream);
} catch (IOException e) {
throw new DistributionException(e);
}
try {
DistributionExportFilter filter = options.getFilter();
for (DistributionExportFilter.TreeFilter treeFilter : filter.getNodeFilters()) {
String path = treeFilter.getPath();
Resource resource = resourceResolver.getResource(path);
AvroShallowResource avroShallowResource = getAvroShallowResource(treeFilter, filter.getPropertyFilter(), resource);
writer.append(avroShallowResource);
}
outputStream.flush();
} catch (Exception e) {
throw new DistributionException(e);
} finally {
try {
writer.close();
} catch (IOException e) {
// do nothing
}
}
}
Aggregations