Processor is both Producer and Consumer. It consumes the data from 1 topic and produces data for another topic.
In our case, we have to do the following
consume the data from numbers topic
remove the odd numbers
squares the even number
write back into another topic.
Lets create the processor by using the corresponding Functional Interface in Java which is Function
We consume the data which is KStream
We do some processing
Then we return the KStream
xxxxxxxxxx
@Configuration
public class KafkaProcessor {
/*
* process the numbers received via kafka topic
* Function<T, R> makes this as kafka stream processor
* T is input type
* R is output type
*
* */
@Bean
public Function<KStream<String, Long>, KStream<String, Long>> evenNumberSquareProcessor(){
return kStream -> kStream
.filter((k, v) -> v % 2 == 0)
.peek((k, v) -> System.out.println("Squaring Even : " + v))
.mapValues(v -> v * v);
};
}