Search in sources :

Example 1 with AvroException

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

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

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

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

the class AvroSnapshotDeserializer method retrieveProtocolId.

protected byte retrieveProtocolId(InputStream inputStream) throws SerDesException {
    // first byte is protocol version/id.
    // protocol format:
    // 1 byte  : protocol version
    byte protocolId;
    try {
        protocolId = (byte) inputStream.read();
    } catch (IOException e) {
        throw new AvroRetryableException(e);
    }
    if (protocolId == -1) {
        throw new AvroException("End of stream reached while trying to read protocol id");
    }
    checkProtocolHandlerExists(protocolId);
    return protocolId;
}
Also used : AvroException(com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroException) AvroRetryableException(com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroRetryableException) IOException(java.io.IOException)

Example 5 with AvroException

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

the class ConfluentAvroSerDesHandler method getReaderSchema.

private Schema getReaderSchema(Schema writerSchema) {
    Schema readerSchema = this.readerSchemaCache.get(writerSchema.getFullName());
    if (readerSchema == null) {
        Class readerClass = SpecificData.get().getClass(writerSchema);
        if (readerClass == null) {
            throw new AvroException("Could not find class " + writerSchema.getFullName() + " specified in writer\'s schema whilst finding reader\'s schema for a SpecificRecord.");
        }
        try {
            readerSchema = ((SpecificRecord) readerClass.newInstance()).getSchema();
        } catch (InstantiationException e) {
            throw new AvroException(writerSchema.getFullName() + " specified by the " + "writers schema could not be instantiated to find the readers schema.");
        } catch (IllegalAccessException e) {
            throw new AvroException(writerSchema.getFullName() + " specified by the " + "writers schema is not allowed to be instantiated to find the readers schema.");
        }
        this.readerSchemaCache.put(writerSchema.getFullName(), readerSchema);
    }
    return readerSchema;
}
Also used : AvroException(com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroException) Schema(org.apache.avro.Schema)

Aggregations

AvroException (com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroException)9 IOException (java.io.IOException)6 AvroRetryableException (com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroRetryableException)5 Schema (org.apache.avro.Schema)5 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 SerDesProtocolHandler (com.hortonworks.registries.schemaregistry.serdes.SerDesProtocolHandler)1 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)1 DatumWriter (org.apache.avro.io.DatumWriter)1 SpecificDatumWriter (org.apache.avro.specific.SpecificDatumWriter)1