How to output multiple records from Transformer?

Dima Svider :

Given: DSL topology with KStream::transform. As part of Transformer::transform execution multiple messages are generated from the input one (KeyValue<String, Message>).

I probably can return KeyValue<String, List<Message>> object from the Transformer::transform and apply flatMapValues as the next processor in the topology to flat the list. However I'm wondering is it possible to use ProcessorContext::forward for the same goal, i.e

public KeyValue<String, Message> transform(String key, Message message) {
        Iterable<Message> messages = generateMultipleFromOne(message);
        messages.forEach(m->context.forward(key, m));

        return null;
}
Dima Svider :

From the javadoc of Transformer.transform(K key, V value):

If more than one output record should be forwarded downstream, ProcessorContext.forward(Object, Object) and ProcessorContext.forward(Object, Object, To) can be used.

Note that returning a new KeyValue is merely for convenience. The same can be achieved by using ProcessorContext.forward(Object, Object) and returning null.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=145243&siteId=1