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);
}
}
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);
}
}
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;
}
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;
}
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);
}
Aggregations