[英]Feeding the output of a Flow to a Broadcast in Akka Streams Graph
I am trying to write a Akka Stream graph. 我正在尝试编写Akka流图。 The code I have written is
我写的代码是
val graph = RunnableGraph.fromGraph(GraphDSL.create(sink1, sink2)((_, _)) { implicit builder =>
(sink1, sink2) =>
import GraphDSL.Implicits._
val bcast = builder.add(Broadcast[Row](2))
val flow = source ~> flow1 ~> flow2
flow.out ~> bcast.in
bcast.out(0) ~> sink1
bcast.out(1) ~> flow3 ~> flow4 ~> sink2
ClosedShape
})
val (f1, f2) = graph.run()
val consolidated = Future.sequence(List(f1, f2))
Await.result(consolidated, Duration.Inf)
This code does not compile because I cannot connect the out of flow to the in of bcast. 此代码无法编译,因为我无法将流出流连接到bcast的入流。
I can connect the out of the source to the in of the bcast, but I cannot do that because some portion is common between the two branches. 我可以将源的出站连接到bcast的入站,但是我不能这样做,因为两个分支之间的某些部分是相同的。 So I must create the branch in the graph only after flow2
所以我只能在flow2之后在图中创建分支
Also... I am not sure if I am writing the Graph correctly because it is returning two futures of Done and I need to combine them into a single future manually using Sequence. 另外...我不确定我是否正确编写了Graph,因为它返回了两个Future的Done,并且我需要使用Sequence将它们手动合并为一个Future。
You can't wire your graph in 2 steps, as the ~>
combinator does not give you back a flow. 您不能分两步对图形进行连线,因为
~>
组合器不会使您流回。 It is in fact a stateful, declarative operation. 实际上,这是有状态的声明式操作。
A better approach here would be to wire your graph in one go, eg 一种更好的方法是一次性连接图表,例如
source ~> flow1 ~> flow2 ~> bcast
bcast ~> sink1
bcast ~> flow3 ~> flow4 ~> sink2
or, alternatively you can split the declarations by adding a stage to the builder (and retrieving its shape), eg 或者,您也可以通过向构建器添加一个阶段(并获取其形状)来拆分声明,例如
val flow2s = builder.add(flow2)
source ~> flow1 ~> flow2s.in
flow2s.out ~> bcast
bcast ~> sink1
bcast ~> flow3 ~> flow4 ~> sink2
Regarding the materialized Future
s, you need to choose what is meaningful as a materialized value of your graph as a whole. 关于物化
Future
,您需要选择有意义的值作为整个图的物化值。 If you only need one of the 2 Sink
s materialized Future
s, you need to pass only that one to the GraphDSL.create
method. 如果您只需要2个
Sink
的实现的Future
的一个,则只需将那个传递给GraphDSL.create
方法。 If, otherwise, you are interested in both Future
s, it makes perfect sense to sequence
or zip
them together. 否则,如果您对两个
Future
都感兴趣,则将它们sequence
或zip
在一起是很有意义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.