gpt4 book ai didi

scala - 阿卡流 : How do I get Materialized Sink output from GraphDSL API?

转载 作者:行者123 更新时间:2023-12-03 15:08:40 25 4
gpt4 key购买 nike

这是一个非常简单的新手问题,使用 GraphDSL API。我阅读了几个相关的 SO 线程,但没有看到答案:

val actorSystem = ActorSystem("QuickStart")
val executor = actorSystem.dispatcher
val materializer = ActorMaterializer()(actorSystem)

val source: Source[Int, NotUsed] = Source(1 to 5)
val throttledSource = source.throttle(1, 1.second, 1, ThrottleMode.shaping)
val intDoublerFlow = Flow.fromFunction[Int, Int](i => i * 2)
val sink = Sink.foreach(println)

val graphModel = GraphDSL.create() { implicit b =>
import GraphDSL.Implicits._

throttledSource ~> intDoublerFlow ~> sink

// I presume I want to change this shape to something else
// but I can't figure out what it is.
ClosedShape
}
// TODO: This is RunnableGraph[NotUsed], I want RunnableGraph[Future[Done]] that gives the
// materialized Future[Done] from the sink. I presume I need to use a GraphDSL SourceShape
// but I can't get that working.
val graph = RunnableGraph.fromGraph(graphModel)

// This works and gives me the materialized sink output using the simpler API.
// But I want to use the GraphDSL so that I can add branches or junctures.
val graphThatIWantFromDslAPI = throttledSource.toMat(sink)(Keep.right)

最佳答案

诀窍是将您想要的物化值(在您的情况下为 sink )传递给 GraphDSL.create .您作为第二个参数传递的函数也发生了变化,需要一个 Shape您可以在图表中使用的输入参数(下例中的 s)。

  val graphModel: Graph[ClosedShape, Future[Done]] = GraphDSL.create(sink) { implicit b => s =>
import GraphDSL.Implicits._

throttledSource ~> intDoublerFlow ~> s

// ClosedShape is just fine - it is always the shape of a RunnableGraph
ClosedShape
}
val graph: RunnableGraph[Future[Done]] = RunnableGraph.fromGraph(graphModel)

更多信息可以在 docs 中找到.

关于scala - 阿卡流 : How do I get Materialized Sink output from GraphDSL API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42426741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com