Search in sources :

Example 1 with KsqlJsonSerializer

use of io.confluent.ksql.serde.json.KsqlJsonSerializer in project ksql by confluentinc.

the class TopicProducer method produceInputData.

/**
 * Topic topicName will be automatically created if it doesn't exist.
 * @param topicName
 * @param recordsToPublish
 * @param schema
 * @return
 * @throws InterruptedException
 * @throws TimeoutException
 * @throws ExecutionException
 */
public Map<String, RecordMetadata> produceInputData(String topicName, Map<String, GenericRow> recordsToPublish, Schema schema) throws InterruptedException, TimeoutException, ExecutionException {
    KafkaProducer<String, GenericRow> producer = new KafkaProducer<>(producerConfig, new StringSerializer(), new KsqlJsonSerializer(schema));
    Map<String, RecordMetadata> result = new HashMap<>();
    for (Map.Entry<String, GenericRow> recordEntry : recordsToPublish.entrySet()) {
        String key = recordEntry.getKey();
        ProducerRecord<String, GenericRow> producerRecord = new ProducerRecord<>(topicName, key, recordEntry.getValue());
        Future<RecordMetadata> recordMetadataFuture = producer.send(producerRecord);
        result.put(key, recordMetadataFuture.get(TEST_RECORD_FUTURE_TIMEOUT_MS, TimeUnit.MILLISECONDS));
    }
    producer.close();
    return result;
}
Also used : KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) HashMap(java.util.HashMap) KsqlJsonSerializer(io.confluent.ksql.serde.json.KsqlJsonSerializer) GenericRow(io.confluent.ksql.GenericRow) RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

GenericRow (io.confluent.ksql.GenericRow)1 KsqlJsonSerializer (io.confluent.ksql.serde.json.KsqlJsonSerializer)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)1 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)1 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)1 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)1