Search in sources :

Example 26 with BinaryEncoder

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

the class AvroCoder method encode.

@Override
public void encode(T value, OutputStream outStream) throws IOException {
    // Get a BinaryEncoder instance from the ThreadLocal cache and attempt to reuse it.
    BinaryEncoder encoderInstance = ENCODER_FACTORY.directBinaryEncoder(outStream, encoder.get());
    // Save the potentially-new instance for reuse later.
    encoder.set(encoderInstance);
    writer.get().write(value, encoderInstance);
// Direct binary encoder does not buffer any data and need not be flushed.
}
Also used : BinaryEncoder(org.apache.avro.io.BinaryEncoder)

Example 27 with BinaryEncoder

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

Example 28 with BinaryEncoder

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

the class IOUtils method serialize.

/**
   * Serializes the field object using the datumWriter.
   *
   * @param <T> class type of object to be serialized.
   * @param datumWriter AVRO datum writer for given schema.
   * @param object object to be serialized.
   * @param os output stream which serialized content is written.
   * @throws IOException occurred while serializing the object to bytes.
   */
public static <T extends SpecificRecord> void serialize(OutputStream os, SpecificDatumWriter<T> datumWriter, T object) throws IOException {
    BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(os, null);
    datumWriter.write(object, encoder);
    encoder.flush();
}
Also used : BinaryEncoder(org.apache.avro.io.BinaryEncoder)

Example 29 with BinaryEncoder

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

the class LoggingEventSerializer method toBytes.

/**
   * Encodes a {@link ILoggingEvent} to byte array.
   */
public byte[] toBytes(ILoggingEvent event) {
    event.prepareForDeferredProcessing();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder(out, null);
    GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<>(getAvroSchema());
    try {
        writer.write(toGenericRecord(event), encoder);
    } catch (IOException e) {
        // This shouldn't happen since we are writing to byte array output stream.
        throw Throwables.propagate(e);
    }
    return out.toByteArray();
}
Also used : BinaryEncoder(org.apache.avro.io.BinaryEncoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) 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