Search in sources :

Example 46 with SpecificDatumWriter

use of org.apache.avro.specific.SpecificDatumWriter in project gobblin by apache.

the class FsSpecProducer method writeAvroJobSpec.

private void writeAvroJobSpec(AvroJobSpec jobSpec) throws IOException {
    DatumWriter<AvroJobSpec> datumWriter = new SpecificDatumWriter<>(AvroJobSpec.SCHEMA$);
    DataFileWriter<AvroJobSpec> dataFileWriter = new DataFileWriter<>(datumWriter);
    Path jobSpecPath = new Path(this.specConsumerPath, annotateSpecFileName(jobSpec.getUri()));
    // Write the new JobSpec to a temporary path first.
    Path tmpDir = new Path(this.specConsumerPath, UUID.randomUUID().toString());
    if (!fs.exists(tmpDir)) {
        fs.mkdirs(tmpDir);
    }
    Path tmpJobSpecPath = new Path(tmpDir, jobSpec.getUri());
    OutputStream out = fs.create(tmpJobSpecPath);
    dataFileWriter.create(AvroJobSpec.SCHEMA$, out);
    dataFileWriter.append(jobSpec);
    dataFileWriter.close();
    // Rename the JobSpec from temporary to final location.
    HadoopUtils.renamePath(fs, tmpJobSpecPath, jobSpecPath, true);
    // Delete the temporary path once the jobspec has been moved to its final publish location.
    log.info("Deleting {}", tmpJobSpecPath.getParent().toString());
    fs.delete(tmpJobSpecPath.getParent(), true);
}
Also used : Path(org.apache.hadoop.fs.Path) DataFileWriter(org.apache.avro.file.DataFileWriter) OutputStream(java.io.OutputStream) AvroJobSpec(org.apache.gobblin.runtime.job_spec.AvroJobSpec) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 47 with SpecificDatumWriter

use of org.apache.avro.specific.SpecificDatumWriter in project spring-batch by spring-projects.

the class AvroTestUtils method createTestDataWithNoEmbeddedSchema.

static void createTestDataWithNoEmbeddedSchema() throws Exception {
    DatumWriter<User> userDatumWriter = new SpecificDatumWriter<>(User.class);
    FileOutputStream fileOutputStream = new FileOutputStream("user-data-no-schema.avro");
    Encoder encoder = EncoderFactory.get().binaryEncoder(fileOutputStream, null);
    userDatumWriter.write(new User("David", 20, "blue"), encoder);
    userDatumWriter.write(new User("Sue", 4, "red"), encoder);
    userDatumWriter.write(new User("Alana", 13, "yellow"), encoder);
    userDatumWriter.write(new User("Joe", 1, "pink"), encoder);
    encoder.flush();
    fileOutputStream.flush();
    fileOutputStream.close();
}
Also used : Encoder(org.apache.avro.io.Encoder) FileOutputStream(java.io.FileOutputStream) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 48 with SpecificDatumWriter

use of org.apache.avro.specific.SpecificDatumWriter in project kloadgen by corunet.

the class GenericAvroRecordBinarySerializer method serialize.

@Override
public byte[] serialize(String s, T data) {
    DatumWriter<T> writer = new SpecificDatumWriter<>(data.getSchema());
    try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        Encoder encoder = EncoderFactory.get().binaryEncoder(baos, null);
        writer.write(data, encoder);
        encoder.flush();
        return baos.toByteArray();
    } catch (IOException e) {
        log.error("Serialization error for date: {}", data, e);
        return new byte[] {};
    }
}
Also used : Encoder(org.apache.avro.io.Encoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 49 with SpecificDatumWriter

use of org.apache.avro.specific.SpecificDatumWriter in project kloadgen by corunet.

the class GenericAvroRecordSerializer method serialize.

@Override
public byte[] serialize(String topic, T record) {
    DatumWriter<T> writer = new SpecificDatumWriter<>(record.getSchema());
    byte[] data = new byte[0];
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Encoder jsonEncoder;
    try {
        jsonEncoder = EncoderFactory.get().jsonEncoder(record.getSchema(), stream);
        writer.write(record, jsonEncoder);
        jsonEncoder.flush();
        data = stream.toByteArray();
    } catch (IOException e) {
        log.error("Serialization error:" + e.getMessage());
    }
    return data;
}
Also used : Encoder(org.apache.avro.io.Encoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 50 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)

Aggregations

SpecificDatumWriter (org.apache.avro.specific.SpecificDatumWriter)115 ByteArrayOutputStream (java.io.ByteArrayOutputStream)53 Schema (org.apache.avro.Schema)36 BinaryEncoder (org.apache.avro.io.BinaryEncoder)30 DataFileWriter (org.apache.avro.file.DataFileWriter)27 Test (org.junit.Test)26 IOException (java.io.IOException)23 GenericRecord (org.apache.avro.generic.GenericRecord)20 Encoder (org.apache.avro.io.Encoder)18 File (java.io.File)12 ByteBuffer (java.nio.ByteBuffer)12 JsonEncoder (org.apache.avro.io.JsonEncoder)12 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)10 SpecificDatumReader (org.apache.avro.specific.SpecificDatumReader)9 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