Search in sources :

Example 11 with BinaryEncoder

use of org.apache.avro.io.BinaryEncoder in project cdap by caskdata.

the class AvroRecordFormatTest method toStreamEvent.

private StreamEvent toStreamEvent(GenericRecord record, boolean writeSchema) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null);
    DatumWriter<GenericRecord> writer = new GenericDatumWriter<>(record.getSchema());
    writer.write(record, encoder);
    encoder.flush();
    out.close();
    byte[] serializedRecord = out.toByteArray();
    String schemaString = record.getSchema().toString();
    Map<String, String> headers = Maps.newHashMap();
    if (writeSchema) {
        headers.put(AvroRecordFormat.SCHEMA, schemaString);
        headers.put(AvroRecordFormat.SCHEMA_HASH, Hashing.md5().hashString(schemaString, Charsets.UTF_8).toString());
    }
    return new StreamEvent(headers, ByteBuffer.wrap(serializedRecord));
}
Also used : BinaryEncoder(org.apache.avro.io.BinaryEncoder) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 12 with BinaryEncoder

use of org.apache.avro.io.BinaryEncoder in project cdap by caskdata.

the class MapReduceStreamInputTestRun method createEvent.

private byte[] createEvent(Schema schema, String ticker, int count, float price) throws IOException {
    GenericRecord record = new GenericRecordBuilder(schema).set("ticker", ticker).set("num_traded", count).set("price", price).build();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null);
    DatumWriter<GenericRecord> writer = new GenericDatumWriter<>(schema);
    writer.write(record, encoder);
    encoder.flush();
    out.close();
    return out.toByteArray();
}
Also used : BinaryEncoder(org.apache.avro.io.BinaryEncoder) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 13 with BinaryEncoder

use of org.apache.avro.io.BinaryEncoder in project hive by apache.

the class AvroGenericRecordWritable method write.

@Override
public void write(DataOutput out) throws IOException {
    // Write schema since we need it to pull the data out. (see point #1 above)
    String schemaString = record.getSchema().toString(false);
    out.writeUTF(schemaString);
    schemaString = fileSchema.toString(false);
    out.writeUTF(schemaString);
    recordReaderID.write(out);
    // Write record to byte buffer
    GenericDatumWriter<GenericRecord> gdw = new GenericDatumWriter<GenericRecord>();
    BinaryEncoder be = EncoderFactory.get().directBinaryEncoder((DataOutputStream) out, null);
    gdw.setSchema(record.getSchema());
    gdw.write(record, be);
}
Also used : BinaryEncoder(org.apache.avro.io.BinaryEncoder) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 14 with BinaryEncoder

use of org.apache.avro.io.BinaryEncoder in project haivvreo by jghoman.

the class AvroGenericRecordWritable method write.

@Override
public void write(DataOutput out) throws IOException {
    // Write schema since we need it to pull the data out. (see point #1 above)
    String schemaString = record.getSchema().toString(false);
    out.writeUTF(schemaString);
    // Write record to byte buffer
    GenericDatumWriter<GenericRecord> gdw = new GenericDatumWriter<GenericRecord>();
    BinaryEncoder be = EncoderFactory.get().directBinaryEncoder((DataOutputStream) out, null);
    gdw.setSchema(record.getSchema());
    gdw.write(record, be);
}
Also used : BinaryEncoder(org.apache.avro.io.BinaryEncoder) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 15 with BinaryEncoder

use of org.apache.avro.io.BinaryEncoder in project databus by linkedin.

the class AvroBinaryDtailPrinter method printGenericRecord.

/**
   * @see com.linkedin.databus2.tools.dtail.GenericRecordDtailPrinter#printGenericRecord(org.apache.avro.generic.GenericRecord)
   */
@Override
public ConsumerCallbackResult printGenericRecord(GenericRecord r) {
    ConsumerCallbackResult result = ConsumerCallbackResult.SUCCESS;
    try {
        BinaryEncoder binEnc = _binEncoders.get(r.getSchema());
        if (null == binEnc) {
            binEnc = new BinaryEncoder(_out);
            _binEncoders.put(r.getSchema(), binEnc);
        }
        GenericDatumWriter<GenericRecord> datumWriter = binWriters.get(r.getSchema());
        if (null == datumWriter) {
            datumWriter = new GenericDatumWriter<GenericRecord>(r.getSchema());
            binWriters.put(r.getSchema(), datumWriter);
        }
        datumWriter.write(r, binEnc);
        binEnc.flush();
        _out.write('\n');
    } catch (RuntimeException re) {
        LOG.error("event dump error: " + re.getMessage(), re);
        result = ConsumerCallbackResult.ERROR;
    } catch (IOException ioe) {
        LOG.error("event dump error: " + ioe.getMessage(), ioe);
        result = ConsumerCallbackResult.ERROR;
    }
    return result;
}
Also used : BinaryEncoder(org.apache.avro.io.BinaryEncoder) ConsumerCallbackResult(com.linkedin.databus.client.pub.ConsumerCallbackResult) IOException(java.io.IOException) GenericRecord(org.apache.avro.generic.GenericRecord)

Aggregations

BinaryEncoder (org.apache.avro.io.BinaryEncoder)29 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)17 GenericRecord (org.apache.avro.generic.GenericRecord)17 IOException (java.io.IOException)14 Encoder (org.apache.avro.io.Encoder)10 SerializationException (voldemort.serialization.SerializationException)5 SpecificDatumWriter (org.apache.avro.specific.SpecificDatumWriter)4 ArrayList (java.util.ArrayList)3 DbusEventInfo (com.linkedin.databus.core.DbusEventInfo)2 SQLException (java.sql.SQLException)2 Properties (java.util.Properties)2 Producer (kafka.javaapi.producer.Producer)2 KeyedMessage (kafka.producer.KeyedMessage)2 ProducerConfig (kafka.producer.ProducerConfig)2 Schema (org.apache.avro.Schema)2 GenericRecordBuilder (org.apache.avro.generic.GenericRecordBuilder)2 EncoderFactory (org.apache.avro.io.EncoderFactory)2 OutputBuffer (org.apache.cassandra.io.util.OutputBuffer)2 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)2