use of com.hortonworks.registries.schemaregistry.serde.SerDesException in project registry by hortonworks.
the class SchemaRegistryClient method createInstance.
private <T> T createInstance(SerDesInfo serDesInfo, boolean isSerializer) {
Set<Class<?>> interfaceClasses = isSerializer ? SERIALIZER_INTERFACE_CLASSES : DESERIALIZER_INTERFACE_CLASSES;
if (interfaceClasses == null || interfaceClasses.isEmpty()) {
throw new IllegalArgumentException("interfaceClasses array must be neither null nor empty.");
}
// loading serializer, create a class loader and and keep them in cache.
final SerDesPair serDesPair = serDesInfo.getSerDesPair();
String fileId = serDesPair.getFileId();
// get class loader for this file ID
ClassLoader classLoader = classLoaderCache.getClassLoader(fileId);
T t;
try {
String className = isSerializer ? serDesPair.getSerializerClassName() : serDesPair.getDeserializerClassName();
Class<T> clazz = (Class<T>) Class.forName(className, true, classLoader);
t = clazz.newInstance();
List<Class<?>> classes = new ArrayList<>();
for (Class<?> interfaceClass : interfaceClasses) {
if (interfaceClass.isAssignableFrom(clazz)) {
classes.add(interfaceClass);
}
}
if (classes.isEmpty()) {
throw new RuntimeException("Given Serialize/Deserializer " + className + " class does not implement any " + "one of the registered interfaces: " + interfaceClasses);
}
Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), classes.toArray(new Class[classes.size()]), new ClassLoaderAwareInvocationHandler(classLoader, t));
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
throw new SerDesException(e);
}
return t;
}
use of com.hortonworks.registries.schemaregistry.serde.SerDesException in project registry by hortonworks.
the class KafkaAvroSerDesApp method sendMessages.
public void sendMessages(String payloadJsonFile) throws Exception {
Properties props = new Properties();
try (FileInputStream fileInputStream = new FileInputStream(this.producerProps)) {
props.load(fileInputStream);
}
int limit = Integer.parseInt(props.getProperty(MSGS_LIMIT_OLD, props.getProperty(MSGS_LIMIT, DEFAULT_MSGS_LIMIT + "")));
boolean ignoreInvalidMsgs = Boolean.parseBoolean(props.getProperty(IGNORE_INVALID_MSGS, "false"));
int current = 0;
Schema schema = new Schema.Parser().parse(new File(this.schemaFile));
String topicName = props.getProperty(TOPIC);
// set protocol version to the earlier one.
props.put(SERDES_PROTOCOL_VERSION, METADATA_ID_VERSION_PROTOCOL);
final Producer<String, Object> producer = new KafkaProducer<>(props);
final Callback callback = new MyProducerCallback();
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(payloadJsonFile))) {
String line;
while (current++ < limit && (line = bufferedReader.readLine()) != null) {
// convert json to avro records
Object avroMsg = null;
try {
avroMsg = jsonToAvro(line, schema);
} catch (Exception ex) {
LOG.warn("Error encountered while converting json to avro of message [{}]", line, ex);
if (ignoreInvalidMsgs) {
continue;
} else {
throw ex;
}
}
// send avro messages to given topic using KafkaAvroSerializer which registers payload schema if it does not exist
// with schema name as "<topic-name>:v", type as "avro" and schemaGroup as "kafka".
// schema registry should be running so that KafkaAvroSerializer can register the schema.
LOG.info("Sending message: [{}] to topic: [{}]", avroMsg, topicName);
ProducerRecord<String, Object> producerRecord = new ProducerRecord<>(topicName, avroMsg);
try {
producer.send(producerRecord, callback);
} catch (SerDesException ex) {
LOG.warn("Error encountered while sending message [{}]", line, ex);
if (!ignoreInvalidMsgs) {
throw ex;
}
}
}
} finally {
producer.flush();
LOG.info("All message are successfully sent to topic: [{}]", topicName);
producer.close(5, TimeUnit.SECONDS);
}
}
use of com.hortonworks.registries.schemaregistry.serde.SerDesException in project registry by hortonworks.
the class MessageContextBasedAvroSerializer method doSerialize.
@Override
protected MessageContext doSerialize(Object input, SchemaIdVersion schemaIdVersion) throws SerDesException {
Map<String, Object> headers = new HashMap<>();
headers.put("protocol.id", getProtocolId());
headers.put("schema.metadata.id", schemaIdVersion.getSchemaMetadataId());
headers.put("schema.version", schemaIdVersion.getVersion());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(baos)) {
serializePayload(bufferedOutputStream, input);
} catch (IOException e) {
throw new SerDesException(e);
}
ByteArrayInputStream payload = new ByteArrayInputStream(baos.toByteArray());
return new MessageContext(headers, payload);
}
Aggregations