Akka流写入多个文件
如中所述,可以将可重用组件(如lineSink)写入文件 我想知道如何从文档中重写Akka流写入多个文件,akka,akka-stream,Akka,Akka Stream,如中所述,可以将可重用组件(如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[St
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)
您将需要为每个新的传入元素具体化一个新流。随着
FileIO.toPath
接收器具体化为未来
,您可以使用mapsync
(使用合理的并行选择)实现这一点。见下例:
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)
请注意,您需要为每个传入元素生成一个适当的文件名
。你需要想出一个方法来做到这一点