Search in sources :

Example 6 with AvroException

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

the class DefaultAvroSerDesHandler method handlePayloadSerialization.

@Override
public void handlePayloadSerialization(OutputStream outputStream, Object input) {
    try {
        Schema schema = AvroUtils.computeSchema(input);
        Schema.Type schemaType = schema.getType();
        if (Schema.Type.BYTES.equals(schemaType)) {
            // incase of byte arrays, no need to go through avro as there is not much to optimize and avro is expecting
            // the payload to be ByteBuffer instead of a byte array
            outputStream.write((byte[]) input);
        } else if (Schema.Type.STRING.equals(schemaType)) {
            // get UTF-8 bytes and directly send those over instead of using avro.
            outputStream.write(input.toString().getBytes("UTF-8"));
        } else {
            BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(outputStream, null);
            DatumWriter<Object> writer;
            boolean isSpecificRecord = input instanceof SpecificRecord;
            if (isSpecificRecord) {
                writer = new SpecificDatumWriter<>(schema);
            } else {
                writer = new GenericDatumWriter<>(schema);
            }
            writer.write(input, encoder);
            encoder.flush();
        }
    } catch (IOException e) {
        throw new AvroRetryableException(e);
    } catch (RuntimeException e) {
        throw new AvroException(e);
    }
}
Also used : GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) DatumWriter(org.apache.avro.io.DatumWriter) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter) 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) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) IOException(java.io.IOException) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 7 with AvroException

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

the class DefaultAvroSerDesHandler 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)

Example 8 with AvroException

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

the class SchemaVersionIdAsIntProtocolHandler method handleSchemaVersionSerialization.

@Override
public void handleSchemaVersionSerialization(OutputStream outputStream, SchemaIdVersion schemaIdVersion) {
    Long versionId = schemaIdVersion.getSchemaVersionId();
    if (versionId > Integer.MAX_VALUE) {
        // if it is more than int max, fallback to SchemaVersionIdAsLongProtocolHandler
        LOG.debug("Upgraded to " + delegate + " as versionId is more than max integer");
        delegate.handleSchemaVersionSerialization(outputStream, schemaIdVersion);
    } else {
        // 4 bytes
        try {
            outputStream.write(new byte[] { protocolId });
            outputStream.write(ByteBuffer.allocate(4).putInt(versionId.intValue()).array());
        } catch (IOException e) {
            throw new AvroException(e);
        }
    }
}
Also used : AvroException(com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroException) IOException(java.io.IOException)

Example 9 with AvroException

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

the class AbstractAvroSnapshotSerializer method doInit.

@Override
public void doInit(Map<String, ?> config) {
    Number number = (Number) ((Map<String, Object>) config).getOrDefault(SERDES_PROTOCOL_VERSION, SerDesProtocolHandlerRegistry.CURRENT_PROTOCOL);
    validateSerdesProtocolVersion(number);
    Byte protocolVersion = number.byteValue();
    SerDesProtocolHandler serDesProtocolHandler = SerDesProtocolHandlerRegistry.get().getSerDesProtocolHandler(protocolVersion);
    if (serDesProtocolHandler == null) {
        throw new AvroException("SerDesProtocolHandler with protocol version " + protocolVersion + " does not exist");
    }
    this.serDesProtocolHandler = serDesProtocolHandler;
}
Also used : AvroException(com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroException) SerDesProtocolHandler(com.hortonworks.registries.schemaregistry.serdes.SerDesProtocolHandler)

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