Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement aggregate on windows. #23

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/main/scala/com.ariskk.flink4s/WindowedStream.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.ariskk.flink4s

import cats.Semigroup
import cats.kernel.Monoid
import org.apache.flink.streaming.api.datastream.{WindowedStream => JavaWStream}
import org.apache.flink.streaming.api.windowing.windows.Window
import org.apache.flink.api.common.functions.ReduceFunction
import org.apache.flink.api.common.functions.{AggregateFunction, ReduceFunction}
import org.apache.flink.api.common.typeinfo.TypeInformation

final case class WindowedStream[T, K, W <: Window](stream: JavaWStream[T, K, W])(implicit
Expand All @@ -16,4 +18,21 @@ final case class WindowedStream[T, K, W <: Window](stream: JavaWStream[T, K, W])
DataStream(stream.reduce(reducer))
}

def aggregate[A, O](aggregateF: (A, T) => A)(outputF: A => O)(implicit monoid: Monoid[A],
aggTypeInformation: TypeInformation[A],
typeInformation: TypeInformation[O]): DataStream[O] = {
val reducer = new AggregateFunction[T, A, O] {

override def createAccumulator(): A = Monoid.empty[A]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though this sounds like a good idea, I have never used a Monoid in practice because Monoid.empty[A] lacks the context of a key (that's needed to do anything meaningly aggregation at scale).
If you find it useful though, happy to have it.

Copy link
Contributor Author

@Wosin Wosin Feb 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gonna remove that as it seems it doesn't make that much sense.


override def add(value: T, accumulator: A): A = aggregateF(accumulator, value)

override def getResult(accumulator: A): O = outputF(accumulator)

override def merge(a: A, b: A): A = Monoid
.combine(a, b)
}
DataStream(stream.aggregate[A,O](reducer, aggTypeInformation, typeInformation))
}

}
52 changes: 50 additions & 2 deletions src/test/scala/com.ariskk.flink4s/WindowedStreamSpec.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.ariskk.flink4s

import scala.collection.mutable.{Buffer => MutableBuffer}
import cats.Monoid
import cats.kernel.Semigroup

import scala.collection.mutable.{Buffer => MutableBuffer}
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.should.Matchers
import org.apache.flink.streaming.api.functions.sink.SinkFunction

import com.ariskk.flink4s.TypeInfo.{intTypeInfo, stringTypeInfo}

final class WindowedStreamSpec extends AnyFunSpec with Matchers {
Expand Down Expand Up @@ -41,6 +42,53 @@ final class WindowedStreamSpec extends AnyFunSpec with Matchers {
results.size should equal(4)
results shouldBe List(50, 100, 100, 100)
}

it("should apply aggregation to count window with slide") {
val env = FlinkExecutor.newEnv(parallelism = 1)
val stream = env.fromCollection((1 to 200).toList.map(_ => 1))
val results = stream
.keyBy(identity)
.countWindow(100, 50)
.aggregate[Int, Int]((agg, i) => agg + i)(identity(_))
.runAndCollect

results.size should equal(4)
results shouldBe List(50, 100, 100, 100)
}


it("should apply aggregation if aggregator and out are different types") {
val env = FlinkExecutor.newEnv(parallelism = 1)
val stream = env.fromCollection((1 to 200).toList.map(_ => 1))
val results = stream
.keyBy(identity)
.countWindow(100, 50)
.aggregate[Int, String]((agg, i) => agg + i)(_.toString)
.runAndCollect

results.size should equal(4)
results shouldBe List("50", "100", "100", "100")
}

it("should apply aggregation based on Monoid") {
val env = FlinkExecutor.newEnv(parallelism = 1)
val stream = env.fromCollection((1 to 200).toList.map(_ => 1))
implicit val semigroup = new Monoid[Int] {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implicit val monoid


override def empty: Int = 5

override def combine(x: Int, y: Int): Int = x + y
}

val results = stream
.keyBy(identity)
.countWindow(100, 50)
.aggregate[Int, Int]((agg, i) => agg + i)(identity(_))
.runAndCollect

results.size should equal(4)
results shouldBe List(55, 105, 105, 105)
}
}

}