Search in sources :

Example 1 with SpecificRecord

use of org.apache.avro.specific.SpecificRecord in project spf4j by zolyfarkas.

the class SpecificRecordAppender method append.

@Override
public void append(final SpecificRecord object, final Appendable appendTo) throws IOException {
    StringBuilder sb = TMP.get();
    sb.setLength(0);
    try (AppendableOutputStream bos = new AppendableOutputStream(sb, Charsets.UTF_8)) {
        final Schema schema = object.getSchema();
        SpecificDatumWriter<SpecificRecord> writer = new SpecificDatumWriter<>(schema);
        JsonEncoder jsonEncoder = EF.jsonEncoder(schema, bos);
        writer.write(object, jsonEncoder);
        jsonEncoder.flush();
    } catch (IOException | RuntimeException ex) {
        writeSerializationError(object, sb, ex);
    }
    appendTo.append(sb);
}
Also used : JsonEncoder(org.apache.avro.io.JsonEncoder) SpecificRecord(org.apache.avro.specific.SpecificRecord) Schema(org.apache.avro.Schema) AppendableOutputStream(org.spf4j.io.AppendableOutputStream) IOException(java.io.IOException) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 2 with SpecificRecord

use of org.apache.avro.specific.SpecificRecord in project registry by hortonworks.

the class ConfluentAvroSerDesHandler method handlePayloadSerialization.

@Override
public void handlePayloadSerialization(OutputStream outputStream, Object input) {
    try {
        Schema schema = AvroUtils.computeSchema(input);
        if (input instanceof byte[]) {
            outputStream.write((byte[]) input);
        } else {
            BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(outputStream, null);
            DatumWriter<Object> writer;
            if (input instanceof SpecificRecord) {
                writer = new SpecificDatumWriter<>(schema);
            } else {
                writer = new GenericDatumWriter<>(schema);
            }
            writer.write(input, encoder);
            encoder.flush();
        }
    } catch (IOException e) {
        throw new AvroRetryableException("Error serializing Avro message", e);
    } catch (RuntimeException e) {
        // ClassCastException, etc
        throw new AvroException("Error serializing Avro message", e);
    }
}
Also used : BinaryEncoder(org.apache.avro.io.BinaryEncoder) AvroException(com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroException) SpecificRecord(org.apache.avro.specific.SpecificRecord) Schema(org.apache.avro.Schema) AvroRetryableException(com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroRetryableException) IOException(java.io.IOException)

Example 3 with SpecificRecord

use of org.apache.avro.specific.SpecificRecord in project samza by apache.

the class TestAzureBlobAvroWriter method encodeRecord.

private byte[] encodeRecord(IndexedRecord record) throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Schema schema = record.getSchema();
    EncoderFactory encoderfactory = new EncoderFactory();
    BinaryEncoder encoder = encoderfactory.binaryEncoder(out, null);
    DatumWriter<IndexedRecord> writer;
    if (record instanceof SpecificRecord) {
        writer = new SpecificDatumWriter<>(schema);
    } else {
        writer = new GenericDatumWriter<>(schema);
    }
    writer.write(record, encoder);
    // encoder may buffer
    encoder.flush();
    return out.toByteArray();
}
Also used : EncoderFactory(org.apache.avro.io.EncoderFactory) IndexedRecord(org.apache.avro.generic.IndexedRecord) BinaryEncoder(org.apache.avro.io.BinaryEncoder) SpecificRecord(org.apache.avro.specific.SpecificRecord) Schema(org.apache.avro.Schema) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with SpecificRecord

use of org.apache.avro.specific.SpecificRecord in project databus by linkedin.

the class DbusEventAvroDecoder method getTypedValue.

@Override
public <T extends SpecificRecord> T getTypedValue(DbusEvent e, T reuse, Class<T> targetClass) {
    if (null == reuse) {
        try {
            reuse = targetClass.newInstance();
        } catch (InstantiationException e1) {
            LOG.error("getTypedValue class instantiation error (" + e1.getMessage() + ") for event " + e, e1);
            return null;
        } catch (IllegalAccessException e1) {
            LOG.error("getTypedValue access error (" + e1.getMessage() + ") for event " + e, e1);
            return null;
        }
    }
    byte[] md5 = new byte[16];
    e.schemaId(md5);
    SchemaId schemaId = new SchemaId(md5);
    VersionedSchema writerSchema = _schemaSet.getById(schemaId);
    if (null == writerSchema) {
        LOG.error("Unable to find schema for id " + schemaId + "; event = " + e);
        throw new DatabusRuntimeException("No schema available to decode event " + e);
    }
    ByteBuffer valueBuffer = e.value();
    byte[] valueBytes = new byte[valueBuffer.remaining()];
    valueBuffer.get(valueBytes);
    try {
        // JsonDecoder jsonDec = new JsonDecoder(sourceSchema.getSchema(),new ByteArrayInputStream(valueBytes));
        binDecoder.set(DecoderFactory.defaultFactory().createBinaryDecoder(valueBytes, binDecoder.get()));
        SpecificDatumReader<SpecificRecord> reader = new SpecificDatumReader<SpecificRecord>(writerSchema.getSchema(), reuse.getSchema());
        return targetClass.cast(reader.read(reuse, binDecoder.get()));
    } catch (IOException e1) {
        LOG.error("getTypedValue IO error (" + e1.getMessage() + ") for event " + e, e1);
    }
    return reuse;
}
Also used : SpecificRecord(org.apache.avro.specific.SpecificRecord) SchemaId(com.linkedin.databus2.schemas.SchemaId) IOException(java.io.IOException) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema) ByteBuffer(java.nio.ByteBuffer) DatabusRuntimeException(com.linkedin.databus.core.DatabusRuntimeException)

Example 5 with SpecificRecord

use of org.apache.avro.specific.SpecificRecord in project flink by apache.

the class AvroRowDeSerializationSchemaTest method testSpecificDeserializeFromClassSeveralTimes.

@Test
public void testSpecificDeserializeFromClassSeveralTimes() throws IOException {
    final Tuple3<Class<? extends SpecificRecord>, SpecificRecord, Row> testData = AvroTestUtils.getSpecificTestData();
    final AvroRowSerializationSchema serializationSchema = new AvroRowSerializationSchema(testData.f0);
    final AvroRowDeserializationSchema deserializationSchema = new AvroRowDeserializationSchema(testData.f0);
    final byte[] bytes = serializationSchema.serialize(testData.f2);
    deserializationSchema.deserialize(bytes);
    deserializationSchema.deserialize(bytes);
    final Row actual = deserializationSchema.deserialize(bytes);
    assertEquals(testData.f2, actual);
}
Also used : SpecificRecord(org.apache.avro.specific.SpecificRecord) Row(org.apache.flink.types.Row) Test(org.junit.Test)

Aggregations

SpecificRecord (org.apache.avro.specific.SpecificRecord)16 IOException (java.io.IOException)7 Row (org.apache.flink.types.Row)7 Schema (org.apache.avro.Schema)6 Test (org.junit.Test)6 BinaryEncoder (org.apache.avro.io.BinaryEncoder)4 IndexedRecord (org.apache.avro.generic.IndexedRecord)3 SpecificDatumWriter (org.apache.avro.specific.SpecificDatumWriter)3 AvroException (com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroException)2 AvroRetryableException (com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroRetryableException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 EncoderFactory (org.apache.avro.io.EncoderFactory)2 JsonEncoder (org.apache.avro.io.JsonEncoder)2 SamzaException (org.apache.samza.SamzaException)2 AppendableOutputStream (org.spf4j.io.AppendableOutputStream)2 BlockBlobAsyncClient (com.azure.storage.blob.specialized.BlockBlobAsyncClient)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 DatabusRuntimeException (com.linkedin.databus.core.DatabusRuntimeException)1 SchemaId (com.linkedin.databus2.schemas.SchemaId)1 VersionedSchema (com.linkedin.databus2.schemas.VersionedSchema)1