use of org.apache.avro.specific.SpecificDatumWriter in project java-common-libs by opencb.
the class AvroDataWriter method open.
@Override
public boolean open() {
/**
* Open output stream *
*/
try {
DatumWriter<T> datumWriter = new SpecificDatumWriter<>();
avroWriter = new DataFileWriter<>(datumWriter);
avroWriter.setCodec(gzip ? CodecFactory.deflateCodec(CodecFactory.DEFAULT_DEFLATE_LEVEL) : CodecFactory.nullCodec());
avroWriter.create(schema, outputPath.toFile());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return true;
}
use of org.apache.avro.specific.SpecificDatumWriter in project hudi by apache.
the class TimelineMetadataUtils method serializeAvroMetadata.
public static <T extends SpecificRecordBase> Option<byte[]> serializeAvroMetadata(T metadata, Class<T> clazz) throws IOException {
DatumWriter<T> datumWriter = new SpecificDatumWriter<>(clazz);
DataFileWriter<T> fileWriter = new DataFileWriter<>(datumWriter);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
fileWriter.create(metadata.getSchema(), baos);
fileWriter.append(metadata);
fileWriter.flush();
return Option.of(baos.toByteArray());
}
use of org.apache.avro.specific.SpecificDatumWriter in project hypertrace-ingester by hypertrace.
the class AvroToJsonLogger method convertToJsonString.
public static <T extends SpecificRecordBase> String convertToJsonString(T object, Schema schema) throws IOException {
JsonEncoder encoder;
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
DatumWriter<T> writer = new SpecificDatumWriter<>(schema);
encoder = EncoderFactory.get().jsonEncoder(schema, output, false);
writer.write(object, encoder);
encoder.flush();
output.flush();
return new String(output.toByteArray());
}
}
use of org.apache.avro.specific.SpecificDatumWriter in project brooklin by linkedin.
the class AvroUtils method encodeAvroSpecificRecord.
/**
* Encode an Avro record into byte array
*
* @param clazz The class type of the Avro record
* @param record the instance of the Avro record
* @param <T> The type of the Avro record.
* @return encoded bytes
*/
public static <T> byte[] encodeAvroSpecificRecord(Class<T> clazz, T record) throws IOException {
DatumWriter<T> msgDatumWriter = new SpecificDatumWriter<>(clazz);
ByteArrayOutputStream os = new ByteArrayOutputStream();
Encoder encoder = EncoderFactory.get().binaryEncoder(os, null);
msgDatumWriter.write(record, encoder);
encoder.flush();
return os.toByteArray();
}
use of org.apache.avro.specific.SpecificDatumWriter in project pulsar by yahoo.
the class JsonConverterTests method serialize.
public static byte[] serialize(GenericRecord record, Schema schema) throws IOException {
SpecificDatumWriter<GenericRecord> datumWriter = new SpecificDatumWriter<>(schema);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
BinaryEncoder binaryEncoder = new EncoderFactory().binaryEncoder(byteArrayOutputStream, null);
datumWriter.write(record, binaryEncoder);
binaryEncoder.flush();
return byteArrayOutputStream.toByteArray();
}
Aggregations