Search in sources :

Example 26 with SpecificDatumWriter

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;
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 27 with SpecificDatumWriter

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());
}
Also used : DataFileWriter(org.apache.avro.file.DataFileWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 28 with SpecificDatumWriter

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());
    }
}
Also used : JsonEncoder(org.apache.avro.io.JsonEncoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 29 with SpecificDatumWriter

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();
}
Also used : Encoder(org.apache.avro.io.Encoder) BinaryEncoder(org.apache.avro.io.BinaryEncoder) JsonEncoder(org.apache.avro.io.JsonEncoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 30 with SpecificDatumWriter

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();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericRecord(org.apache.avro.generic.GenericRecord) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Aggregations

SpecificDatumWriter (org.apache.avro.specific.SpecificDatumWriter)119 ByteArrayOutputStream (java.io.ByteArrayOutputStream)57 Schema (org.apache.avro.Schema)35 BinaryEncoder (org.apache.avro.io.BinaryEncoder)32 DataFileWriter (org.apache.avro.file.DataFileWriter)26 Test (org.junit.Test)26 IOException (java.io.IOException)23 GenericRecord (org.apache.avro.generic.GenericRecord)23 Encoder (org.apache.avro.io.Encoder)19 JsonEncoder (org.apache.avro.io.JsonEncoder)13 File (java.io.File)12 ByteBuffer (java.nio.ByteBuffer)12 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)10 SpecificDatumReader (org.apache.avro.specific.SpecificDatumReader)10 Path (java.nio.file.Path)8 Avro1124SubjectAndIdConverter (org.apache.druid.data.input.schemarepo.Avro1124SubjectAndIdConverter)8 InMemoryRepository (org.schemarepo.InMemoryRepository)8 Repository (org.schemarepo.Repository)8 TypedSchemaRepository (org.schemarepo.api.TypedSchemaRepository)8