Akka-Streams 子流程

你可以使用 groupBy 动态分叉多个子流中的流。继续阶段将应用于每个子流,直到你使用 mergeSubstreams 将它们合并回来。

val sumByKey: Flow[(String, Int), Int, NotUsed] = 
  Flow[(String, Int)].
    groupBy(Int.maxValue, _._1).  //forks the flow
    map(_._2).                    //this is applied to each subflow
    fold(0)(_ + _).
    mergeSubstreams               //the subflow outputs are merged back together