如何在Akka Streams

时间:2017-10-27 09:55:51

标签: scala akka-stream

我们希望并行化一部分流保持排序,就像mapAsync一样,但没有Future。

目前我们有以下解决方案,但这需要一个物理化器,而普通的flatMapConcat不需要。

def flatMapConcatParallel[In, Out](parallelism: Int)(f: In => Source[Out, _])(implicit mat: Materializer): Flow[In, Out, NotUsed] = {
    // TODO there should be a better way to add parallelism that avoids a run (and the need for a materializer)
    Flow[In].mapAsync(parallelism){i =>
      f(i).runWith(Sink.head)
    }
  }

if (parallel){
  val parallelism = 4
  Flow[Batch].via(flatMapConcatParallel(parallelism)(singleRun))
} else{
  Flow[Batch].flatMapConcat(singleRun)
}

有关如何使用现有结构实现这一点的任何提示,而不必进入低级别(GraphStageLogic)?

                            ---> f() --->
--d-c-b-a--> OrderedBalance ---> f() ---> OrderedMerge --d'-c'-b'-a'--->
                            ---> f() --->

2 个答案:

答案 0 :(得分:3)

怎么样:

Flow[In].mapAsync(parallelism)(i => Future.successful(f(i))).flatMapConcat(identity)

答案 1 :(得分:0)

我遇到了类似的问题。我创建了一个流程来为我解决:

Source.range(1, 10)
  .via(parallelFlatMapConcat(3)(e => Source.range(1, 5).map(_ * e)))
  .runForeach(println(_))

然后可以将其用作流中的流。用法示例:

{
    "name": "testAutomation-1",
    "owner": "TestUSer",
    "description": "testAutomation-1",
    "subSet": [
        "test-audit"
    ],
    "labels": "{\"engagementType\":\"Sx\",\"type\":\"collect\"}",
    "createdTime": 1548508294790,
    "updatedTime": 1548654725381,
    "scheduleDateTime": null,
    "timeZone": null,
    "jobStatus": "Report-Requested",
    "loggedInUser": null,
    "Details": [
        {
            "Status": "Completed",
            "Id": "fe1f6b40-216b-11e9-a769-25be4c5889e7"
        }
    ]
}