How to sessionize / group the events in Akka Streams?

210 Views Asked by At

The requirement is that I want to write an Akka streaming application that listens to continuous events from Kafka, then sessionizes the event data in a time frame, based on some id value embedded inside each event.

For example, let's say that my time frame window is two minutes, and in the first two minutes I get the four events below:

Input:

{"message-domain":"1234","id":1,"aaa":"bbb"}
{"message-domain":"1234","id":2,"aaa":"bbb"}
{"message-domain":"5678","id":4,"aaa":"bbb"}
{"message-domain":"1234","id":3,"aaa":"bbb"}

Then in the output, after grouping/sessionizing these events, I will have only two events based on their message-domain value.

Output:

{"message-domain":"1234",messsages:[{"id":1,"aaa":"bbb"},{"id":2,"aaa":"bbb"},{"id":4,"aaa":"bbb"}]}
{"message-domain":"5678",messsages:[{"id":3,"aaa":"bbb"}]}

And I want this to happen in real time. Any suggestions on how to achieve this?

1

There are 1 best solutions below

2
On

To group the events within a time window you can use Flow.groupedWithin:

val maxCount : Int = Int.MaxValue

val timeWindow = FiniteDuration(2L, TimeUnit.MINUTES)

val timeWindowFlow : Flow[String, Seq[String]] =
  Flow[String] groupedWithin (maxCount, timeWindow)