I know Kafka Streams allow data to be distributed to multiple topics based on specified predicates, and the Kafka Streams binder supports this using both the @StreamListener
and the functional binding
approach.
...
// return type KStream<?, WordCount>[]
Predicate<Object, WordCount> isEnglish = (k, v) -> v.word.equals("english");
Predicate<Object, WordCount> isFrench = (k, v) -> v.word.equals("french");
Predicate<Object, WordCount> isSpanish = (k, v) -> v.word.equals("spanish");
return input.
...
branch(isEnglish, isFrench, isSpanish);
I'm wondering how to transform either the key or value of one of the branches before returning the data. Say I'd like one of the branches to have a different key type from the others.
Predicate<Key, Value> isOne = (k, v) -> v.important.equals("1");
Predicate<Key, Value> isTwo = (k, v) -> v.important.equals("2");
KStream<Key, Value>[] branches = input.branch(isOne, isTwo);
KStream<String, Value> one = branches[0].selectKey((k, v) -> v.importantValue);
I thought of creating a new KStream<?, Value>[]
array with both streams but couldn't because of the generic array creation error.
I know this is possible as could be seen from the documentation excerpt below, different key/value serdes could be specified for each branch's producer
.
spring.cloud.stream.kafka.streams.bindings.output1.producer.valueSerde=IntegerSerde
spring.cloud.stream.kafka.streams.bindings.output2.producer.valueSerde=StringSerde
spring.cloud.stream.kafka.streams.bindings.output3.producer.valueSerde=JsonSerde
Thanks for helping.