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