I am using Kafka stream for windowed aggregation. Here is the logic:
KStream<String, String> stream = builder.stream(topics, Consumed.with(stringSerde, stringSerde));
Stream.groupByKey()
.windowedBy(TimeWindows.of(Duration.ofMinutes(2)).grace(Duration.ZERO))
.aggregate(()-> "",
(key, word, aggregate) -> {
logger.info("(key, word, aggregate): ({}, {}, {})" , key, word, aggregate);
aggregate = aggregate+ "-" + word;
return aggregate;
}, Materialized.with(stringSerde, stringSerde))
.toStream((k, v) -> k.key())
.foreach((k, v) -> logger.info("Aggregated to Stream. (k,v): ({}, {})" , k, v));
Though this works for most of the times, I observed these issues:
- Aggregation is prematurely flushed
- New aggregation bucket gets created even before the window is closed
These issues are evident by these logs (marked lines):
[2019-08-14 14:10:38,855] [ INFO] [prc-client-StreamThread-1] Aggregation (118) - (key, word, aggregate): (1, a, )
(1)[2019-08-14 14:11:24,503] [ INFO] [prc-client-StreamThread-1] Aggregation (124) - Aggregated to Stream. (k,v): (1, -a)
[2019-08-14 14:11:27,735] [ INFO] [prc-client-StreamThread-1] Aggregation (118) - (key, word, aggregate): (1, b, -a)
[2019-08-14 14:11:43,298] [ INFO] [prc-client-StreamThread-1] Aggregation (118) - (key, word, aggregate): (1, f, -a-b)
[2019-08-14 14:11:59,373] [ INFO] [prc-client-StreamThread-1] Aggregation (118) - (key, word, aggregate): (1, b, -a-b-f)
(2)[2019-08-14 14:12:14,196] [ INFO] [prc-client-StreamThread-1] Aggregation (118) - (key, word, aggregate): (1, r, )
[2019-08-14 14:13:24,808] [ INFO] [prc-client-StreamThread-1] Aggregation (124) - Aggregated to Stream. (k,v): (1, -a-b-f-b)
[2019-08-14 14:13:24,808] [ INFO] [prc-client-StreamThread-1] Aggregation (124) - Aggregated to Stream. (k,v): (1, -r)
Is there is anyway to address these issues?