Search in sources :

Example 1 with SerDesException

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;
}
Also used : ArrayList(java.util.ArrayList) DEFAULT_CONNECTION_TIMEOUT(com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient.Configuration.DEFAULT_CONNECTION_TIMEOUT) DEFAULT_READ_TIMEOUT(com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient.Configuration.DEFAULT_READ_TIMEOUT) ClassLoaderAwareInvocationHandler(com.hortonworks.registries.common.util.ClassLoaderAwareInvocationHandler) SerDesException(com.hortonworks.registries.schemaregistry.serde.SerDesException) SerDesPair(com.hortonworks.registries.schemaregistry.SerDesPair)

Example 2 with SerDesException

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);
    }
}
Also used : KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) Schema(org.apache.avro.Schema) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) SerDesException(com.hortonworks.registries.schemaregistry.serde.SerDesException) ParseException(org.apache.commons.cli.ParseException) Callback(org.apache.kafka.clients.producer.Callback) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) SerDesException(com.hortonworks.registries.schemaregistry.serde.SerDesException) File(java.io.File)

Example 3 with SerDesException

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);
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) SerDesException(com.hortonworks.registries.schemaregistry.serde.SerDesException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

SerDesException (com.hortonworks.registries.schemaregistry.serde.SerDesException)3 ClassLoaderAwareInvocationHandler (com.hortonworks.registries.common.util.ClassLoaderAwareInvocationHandler)1 SerDesPair (com.hortonworks.registries.schemaregistry.SerDesPair)1 DEFAULT_CONNECTION_TIMEOUT (com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient.Configuration.DEFAULT_CONNECTION_TIMEOUT)1 DEFAULT_READ_TIMEOUT (com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient.Configuration.DEFAULT_READ_TIMEOUT)1 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 Schema (org.apache.avro.Schema)1 ParseException (org.apache.commons.cli.ParseException)1 Callback (org.apache.kafka.clients.producer.Callback)1 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)1