简体   繁体   English

akka-streams 有状态的子流

[英]akka-streams stateful substream flow

There is a stateful flow:有一个有状态的流程:

val stream = Flow[Event].statefulMapConcat {
  () =>

    val state = ...

    {
      element =>
        // change the state
        element :: Nil
    }
}

and it is a part of the flow它是流程的一部分

Flow[Event]
  .groupBy(1000000, event => event.key2, allowClosedSubstreamRecreation = true)
  .via(stream)
  .mergeSubstreams

Is there any way to have a state in stream per substream (in this example per key after the groupBy)?有没有办法让每个子流在stream中有一个state (在这个例子中,每个键在 groupBy 之后)? I think it should be materialised per sub-stream, but don't know how to do that.我认为它应该按子流实现,但不知道该怎么做。

You do get a state per substream in that setup:在该设置中,您确实会为每个子流获得一个 state:

  val stream = Flow[Int].statefulMapConcat {
    () => {

      var state: List[Int] = Nil

      element => {
        state = element :: state
        List(state)
      }
    }
  }

  val groupByFlow =
  Flow[Int]
    .groupBy(1000000, identity, allowClosedSubstreamRecreation = true)
    .via(stream)
    .mergeSubstreams

  Source(List(1,1,2,3,3,3))
    .via(groupByFlow)
    .runForeach(i => println(i))

will print将打印

List(1)
List(1, 1)
List(3)
List(2)
List(3, 3)
List(3, 3, 3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM