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