gpt4 book ai didi

scala - Akka Streams 入口和导出匹配

转载 作者:行者123 更新时间:2023-12-02 03:01:22 24 4
gpt4 key购买 nike

这是我能想出的使用 PartitionMerge 的最简单的图表,但运行时出现以下错误:要求失败:入口[]和导出[]必须对应于入口[Merge.in0,Merge.in1]和导出[Partition.out0,Partition.out1]

我知道该消息表明我的输出多于输入或未连接的流,但我似乎无法在这个简单的示例中看到不匹配的地方。

感谢任何帮助。

图表:

    def createGraph()(implicit actorSystem: ActorSystem): Graph[ClosedShape, Future[Done]] = {
GraphDSL.create(Sink.ignore) { implicit builder: GraphDSL.Builder[Future[Done]] => s =>
import GraphDSL.Implicits._
val inputs: List[Int] = List(1, 2, 3, 4)
val source: Source[Int, NotUsed] = Source(inputs)

val messageSplit: UniformFanOutShape[Int, Int] = builder.add(Partition[Int](2, i => i%2))

val messageMerge: UniformFanInShape[Int, Int] = builder.add(Merge[Int](2))

val processEven: Flow[Int, Int, NotUsed] = Flow[Int].map(rc => {
actorSystem.log.debug(s"even: $rc")
rc
})

val processOdd: Flow[Int, Int, NotUsed] = Flow[Int].map(rc => {
actorSystem.log.debug(s"odd: $rc")
rc
})

source ~> messageSplit.in
messageSplit.out(0) -> processEven -> messageMerge.in(0)
messageSplit.out(1) -> processOdd -> messageMerge.in(1)
messageMerge.out ~> s
ClosedShape
}
}

测试:

import akka.actor.ActorSystem
import akka.stream._
import akka.stream.scaladsl.{Flow, GraphDSL, Merge, Partition, RunnableGraph, Sink, Source}
import akka.{Done, NotUsed}
import org.scalatest.FunSpec

import scala.concurrent.Future
class RoomITSpec extends FunSpec {

implicit val actorSystem: ActorSystem = ActorSystem("RoomITSpec")
implicit val actorCreator: ActorMaterializer = ActorMaterializer()
describe("graph") {
it("should run") {
val graph = createGraph()
RunnableGraph.fromGraph(graph).run
}
}
}

最佳答案

小句法错误。

// Notice the curly arrows
messageSplit.out(0) ~> processEven ~> messageMerge.in(0)
messageSplit.out(1) ~> processOdd ~> messageMerge.in(1)

而不是你写的:

// Straight arrows
messageSplit.out(0) -> processEven -> messageMerge.in(0)
messageSplit.out(1) -> processOdd -> messageMerge.in(1)

您最终生成(并丢弃)元组而不是添加到图中。

关于scala - Akka Streams 入口和导出匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45992244/

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