Lets say this is my sample stream like so:

SingleOutputStreamOperator<Tuple2<String, SampleClass>> sampleStream = previousStream
                                    .keyBy(value -> value.f1.getId())
                                    .window(TumblingProcessingTimeWindows.of(Time.seconds(1)))

SampleClass has two fields id and time. It also has two getter methods getId() and getTime that gets those respective fields.

Goal: Now I want to perform the maxBy operation on the above stream with the time as my condition for maximiziation. Basically, I want the latest element in the front of the window.

How do I achieve my goal?

maxBy works on position of parameter and not on nested field inside it. For example:

.maxBy(positionToMaxBy = 1, first = true)

Here positionToMaxBy would consider the second parameter from the stream parameters, which is the object. But how do I get it to use the getTime() method to be used instead of the whole object?

1

There are 1 best solutions below

1
David Anderson On

A few solutions:

  • use a map before applying the windowing that transforms Tuple2<String, SampleClass> to SampleClass
  • or if you need the String, add it to SampleClass
  • use reduce instead of maxBy
  • implement Comparable on SampleClass and have the compareTo method only check the time field