Akka Stream写入多个文件

时间:2017-04-28 09:35:33

标签: akka akka-stream

Documentation中所述,可以将可重用的组件(如lineSink)写入文件中。

我想知道如何从文档中重写lineSink组件,以便能够实现以下场景:

val source = Source(1 to 10)
val flow = Flow[Int].map(_.toString)

/** Rewrite of the lineSink to enable new files with content */
val fileSink = Sink[String, Future[IOResult]] = 
  Flow[String]
    .map(s => ByteString(s + "\n"))
    .toMat(FileIO.toPath(Paths.get(filename)))(Keep.right)

/** After running the graph i want to have a new file for each item processed */
val graph = source.via(flow).to(sink)

1 个答案:

答案 0 :(得分:2)

您将需要为每个新传入元素实现一个新流。当Animation接收器具体化为FileIO.toPath时,您可以使用Future(具有合理的并行选择)来实现此目的。见下面的例子:

mapAsync

请注意,您需要为每个传入元素生成适当的 val source = Source(1 to 10) val flow: Flow[Int, String, NotUsed] = Flow[Int].map(_.toString) val fileFlow: Flow[String, IOResult, NotUsed] = Flow[String].mapAsync(parallelism = 4){ s ⇒ Source.single(ByteString(s)).runWith(FileIO.toPath(Paths.get(fileName))) } val fileSink: Sink[IOResult, Future[Done]] = Sink.foreach[IOResult]{println} val graph = source.via(flow).via(fileFlow).to(fileSink) 。你需要想出办法来做到这一点。