Search in sources :

Example 1 with AvroRetryableException

use of com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroRetryableException in project registry by hortonworks.

the class AvroSnapshotSerializer method doSerialize.

protected byte[] doSerialize(Object input, SchemaIdVersion schemaIdVersion) throws SerDesException {
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        serializeSchemaVersion(baos, schemaIdVersion);
        serializePayload(baos, input);
        return baos.toByteArray();
    } catch (IOException e) {
        throw new AvroRetryableException(e);
    }
}
Also used : AvroRetryableException(com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroRetryableException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 2 with AvroRetryableException

use of com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroRetryableException 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 AvroRetryableException

use of com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroRetryableException in project registry by hortonworks.

the class ConfluentAvroSerDesHandler method handlePayloadDeserialization.

@Override
public Object handlePayloadDeserialization(InputStream payloadInputStream, Schema writerSchema, Schema readerSchema, boolean useSpecificAvroReader) {
    Object deserializedObj;
    try {
        if (Schema.Type.BYTES.equals(writerSchema.getType())) {
            // serializer writes byte array directly without going through avro encoder layers.
            deserializedObj = IOUtils.toByteArray(payloadInputStream);
        } else {
            DatumReader datumReader = getDatumReader(writerSchema, readerSchema, useSpecificAvroReader);
            deserializedObj = datumReader.read(null, DecoderFactory.get().binaryDecoder(payloadInputStream, null));
        }
    } catch (IOException e) {
        throw new AvroRetryableException("Error deserializing Avro message for id " + writerSchema, e);
    } catch (RuntimeException e) {
        // avro deserialization may throw AvroRuntimeException, NullPointerException, etc
        throw new AvroException("Error deserializing Avro message for id " + writerSchema, e);
    }
    return deserializedObj;
}
Also used : SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) DatumReader(org.apache.avro.io.DatumReader) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) AvroException(com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroException) AvroRetryableException(com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroRetryableException) IOException(java.io.IOException)

Example 4 with AvroRetryableException

use of com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroRetryableException in project registry by hortonworks.

the class DefaultAvroSerDesHandler method handlePayloadDeserialization.

@Override
public Object handlePayloadDeserialization(InputStream payloadInputStream, Schema writerSchema, Schema readerSchema, boolean useSpecificAvroReader) {
    Object deserializedObj;
    Schema.Type writerSchemaType = writerSchema.getType();
    try {
        if (Schema.Type.BYTES.equals(writerSchemaType)) {
            // serializer writes byte array directly without going through avro encoder layers.
            deserializedObj = IOUtils.toByteArray(payloadInputStream);
        } else if (Schema.Type.STRING.equals(writerSchemaType)) {
            // generate UTF-8 string object from the received bytes.
            deserializedObj = new String(IOUtils.toByteArray(payloadInputStream), AvroUtils.UTF_8);
        } else {
            DatumReader datumReader = getDatumReader(writerSchema, readerSchema, useSpecificAvroReader);
            deserializedObj = datumReader.read(null, DecoderFactory.get().binaryDecoder(payloadInputStream, null));
        }
    } catch (IOException e) {
        throw new AvroRetryableException(e);
    } catch (Exception e) {
        throw new AvroException(e);
    }
    return deserializedObj;
}
Also used : SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) DatumReader(org.apache.avro.io.DatumReader) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) AvroException(com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroException) Schema(org.apache.avro.Schema) AvroRetryableException(com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroRetryableException) IOException(java.io.IOException) IOException(java.io.IOException) AvroException(com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroException) AvroRetryableException(com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroRetryableException)

Example 5 with AvroRetryableException

use of com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroRetryableException in project registry by hortonworks.

the class SchemaVersionIdAsIntProtocolHandler method handleSchemaVersionDeserialization.

@Override
public SchemaIdVersion handleSchemaVersionDeserialization(InputStream inputStream) {
    ByteBuffer byteBuffer = ByteBuffer.allocate(4);
    try {
        inputStream.read(byteBuffer.array());
    } catch (IOException e) {
        throw new AvroRetryableException(e);
    }
    int schemaVersionId = byteBuffer.getInt();
    return new SchemaIdVersion((long) schemaVersionId);
}
Also used : SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) AvroRetryableException(com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroRetryableException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

AvroRetryableException (com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroRetryableException)11 IOException (java.io.IOException)11 AvroException (com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroException)5 SchemaIdVersion (com.hortonworks.registries.schemaregistry.SchemaIdVersion)3 ByteBuffer (java.nio.ByteBuffer)3 Schema (org.apache.avro.Schema)3 GenericDatumReader (org.apache.avro.generic.GenericDatumReader)2 BinaryEncoder (org.apache.avro.io.BinaryEncoder)2 DatumReader (org.apache.avro.io.DatumReader)2 SpecificDatumReader (org.apache.avro.specific.SpecificDatumReader)2 SpecificRecord (org.apache.avro.specific.SpecificRecord)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)1 DatumWriter (org.apache.avro.io.DatumWriter)1 SpecificDatumWriter (org.apache.avro.specific.SpecificDatumWriter)1