Search in sources :

Example 6 with AvroRetryableException

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

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

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

Example 8 with AvroRetryableException

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

the class SchemaMetadataIdProtocolHandler method handleSchemaVersionDeserialization.

@Override
public SchemaIdVersion handleSchemaVersionDeserialization(InputStream inputStream) {
    // 8 bytes : schema metadata Id
    // 4 bytes : schema version
    ByteBuffer byteBuffer = ByteBuffer.allocate(12);
    try {
        inputStream.read(byteBuffer.array());
    } catch (IOException e) {
        throw new AvroRetryableException(e);
    }
    long schemaMetadataId = byteBuffer.getLong();
    int schemaVersion = byteBuffer.getInt();
    return new SchemaIdVersion(schemaMetadataId, schemaVersion);
}
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)

Example 9 with AvroRetryableException

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

the class SchemaVersionIdAsLongProtocolHandler method doHandleSchemaVersionSerialization.

@Override
public void doHandleSchemaVersionSerialization(OutputStream outputStream, SchemaIdVersion schemaIdVersion) throws SerDesException {
    try {
        Long versionId = schemaIdVersion.getSchemaVersionId();
        outputStream.write(ByteBuffer.allocate(8).putLong(versionId).array());
    } catch (IOException e) {
        throw new AvroRetryableException(e);
    }
}
Also used : AvroRetryableException(com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroRetryableException) IOException(java.io.IOException)

Example 10 with AvroRetryableException

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

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