apache kafka – KGroupedStream reduce use key in Reducer logic
You could use the aggregate method of the KGroupedStream interface:
KTable aggregate(Initializer initializer, Aggregator super K,? super V,VR> aggregator) Aggregate the values of records in this stream by the grouped key. Records with
nullkey or value are ignored. Aggregating is a generalization ofcombining via reduce(...)as it, for example, allows the result to have a different type than the input values.
If you want to output a result for a key only if the value matches, you create a Ktable, something like:
KTable resultTable = inputStream
.groupByKey()
.aggregate(
() -> true,
(key, value, agg) -> agg && key.equals(value),
Materialized.with(Serdes.String(), Serdes.Boolean())
);
You can then filter the values in the table, something like:
KStream finalResult = resultTable
.toStream()
.filter((key, allEqual) -> allEqual);
Read more here: Source link
