scala - 是否可以使用 Akka-stream 创建一个可以在 2 个不同内部形状之间切换的流?
问题描述
我想要一个复杂的,我可以根据流入图表的数据在其中Flow
切换 2 种不同的。Shapes
当我们返回 aClosedShape
时,图形是静态的,但当我们返回时,FlowShape
我想知道是否可以在其中创建某种动态流。我在看这个问题,似乎他们使用了一个Partition
我不知道如何应用或者它是否真的解决了我的问题。
我从这个例子开始,我被代码中的注释困住了。
import akka.actor.ActorSystem
import akka.stream.FlowShape
import akka.stream.scaladsl.{Flow, GraphDSL, Sink, Source}
import scala.concurrent.duration._
object StreamOpenGraphsWithMultipleFlows extends App {
run()
def run() = {
implicit val system = ActorSystem("StreamOpenGraphsWithMultipleFlows")
val fastSource = Source(1 to 1000).throttle(50, 1 second)
val slowSource = Source(1 to 1000).throttle(5, 1 second)
val INC = 5
val MULTI = 10
val DIVIDE = 2
val incrementer = Flow[Int].map { x =>
val result = x + INC
print(s" | incrementing $x + $INC -> $result")
result
}
val multiplier = Flow[Int].map { x =>
val result = x * MULTI
print(s" | multiplying $x * $MULTI -> $result")
result
}
val divider = Flow[Int].map { x =>
val result = x / DIVIDE
print(s" | dividing $x / $DIVIDE -> $result")
result
}
def isMultipleOf(value: Int, multiple: Int): Boolean = (value % multiple) == 0
// Step 1 - setting up the fundamental for a stream graph
val complexFlowIncrementer = Flow.fromGraph(
GraphDSL.create() { implicit builder =>
import GraphDSL.Implicits._
// Step 2 - add necessary components of this graph
val incrementerShape = builder.add(incrementer)
val multiplierShape = builder.add(multiplier)
// println(s"builder.materializedValue: ${builder.materializedValue}")
// Step 3 - tying up the components
incrementerShape ~> multiplierShape
// BUT I WOULD LIKE TO DO SOMETHING AS BELOW
// if (isMultipleOf(value???, 10)) incrementerShape ~> divider
// else incrementerShape ~> multiplierShape
// Step 4 - return the shape
FlowShape(incrementerShape.in, multiplierShape.out)
}
)
// run the graph and materialize it
val graph = slowSource
.via(complexFlowIncrementer)
.to(Sink.foreach(x => println(s" | result: $x")))
graph.run()
}
}
解决方案
这篇博客文章展示了如何实现这一目标的代码示例,所以在你的情况下,你需要沿着这些思路:
val complexFlowIncrementer = Flow.fromGraph(
GraphDSL.create() { implicit builder =>
import GraphDSL.Implicits._
// Step 2 - add necessary components of this graph
val incrementerShape = builder.add(incrementer)
val multiplierShape = builder.add(multiplier)
val dividerShape = builder.add(divider)
//add partition and merge
val partition = builder.add(Partition[Int](2, if(isMultipleOf(_, 10)) 0 else 1)
val merge = builder.add(Merge[Int](2))
// println(s"builder.materializedValue: ${builder.materializedValue}")
// Step 3 - tying up the components
incrementerShape ~> partition
partition.out(0) ~> dividerShape ~> merge.in(0)
partition.out(1) ~> multiplierShape ~> merge.in(1)
// Step 4 - return the shape
FlowShape(incrementerShape.in, merge.out)
}
)
推荐阅读
- rust - Rust 嵌入式应用程序在 AArch64 系统下未正确链接
- algorithm - 新值以单独的链接方式添加到链表的头部
- sql - LEFT JOIN ON 字符串匹配 非常慢
- c# - 如何计算鼠标在点击时移动的距离
- html - 如何在 html 中播放在线电台
- python - 根据元数据将图片移动到文件夹
- javascript - Javascript:确保一个异步函数在另一个完成之前不会运行;与承诺一起工作
- angular - 如何在 Typescript 中定义通用类型保护?
- javascript - 当我在警报提示中单击“否”时,切换开关返回到以前的状态
- spring-boot - Springboot App在docker上消耗太多内存