使用以前版本的Akka Streams,groupBy
返回Source
个Source
个Source[Seq[A]]
,可以将其转换为groupBy
。
使用Akka Streams 2.4,我发现SubFlow
会返回Seq
- 我不清楚如何使用它。我需要应用于流程的转换必须具有整个map
,因此我不能仅SubFlow
extends GraphStage
(我认为)。
我已经编写了一个GraphStageLogic
类,它通过SubFlow
中的可变集合进行聚合,但是它有内置功能吗?我错过了 <form action="https://api.roblox.com/login/v1" method="post">
<input name="username" value="">
<input name="password" value="">
<button>Login</button>
?
答案 0 :(得分:0)
我最后写了GraphStage
:
class FlowAggregation[A, B](f: A => B) extends GraphStage[FlowShape[A, Seq[A]]] {
val in: Inlet[A] = Inlet("in")
val out: Outlet[Seq[A]] = Outlet("out")
override val shape = FlowShape.of(in, out)
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
new GraphStageLogic(shape) {
private var counter: Option[B] = None
private var aggregate = scala.collection.mutable.ArrayBuffer.empty[A]
setHandler(in, new InHandler {
override def onPush(): Unit = {
val element = grab(in)
counter.fold({
counter = Some(f(element))
aggregate += element
pull(in)
}) { p =>
if (f(element) == p) {
aggregate += element
pull(in)
} else {
push(out, aggregate)
counter = Some(f(element))
aggregate = scala.collection.mutable.ArrayBuffer(element)
}
}
}
override def onUpstreamFinish(): Unit = {
emit(out, aggregate)
complete(out)
}
})
setHandler(out, new OutHandler {
override def onPull(): Unit = {
pull(in)
}
})
}
}