Akka流管道工作正常,没有take(2)但是take(2)不显示任何输出。 take()函数如何产生影响?
import akka.NotUsed
import akka.actor.ActorSystem
import akka.stream._
import akka.stream.scaladsl._
import scala.util.Try
import scala.concurrent.ExecutionContext.Implicits._
object TestStream {
implicit val system = ActorSystem("MyTest")
implicit val Matt = ActorMaterializer()
def mainflow():Unit ={
val grp = RunnableGraph.fromGraph(GraphDSL.create() {
implicit builder =>
import GraphDSL.Implicits._
val src:Outlet[Int] = builder.add(Source(1 to 10)).out
val flw1:FlowShape[Int, Int] = builder.add(Flow[Int].map(x => x*10).take(2))
val flw2:FlowShape[Int, Int] = builder.add(Flow[Int].filter(x => x > 50))
val snk2:Inlet[Any] = builder.add(Sink.foreach(println)).in
src ~> flw1 ~> flw2 ~> snk2
ClosedShape
}).run
}
def main(str:Array[String]):Unit = {
mainflow()
}
}
仅供参考添加测试结果,不带take(): spark \ bin \ spark-submit --class TestStream --master local [2] --jars config-1.2.1.jar ,akka-actor_2.11-2.4.2.jar,reactive-s treams-1.0.0.jar,akka-stream_2.11-2.4.2.jar target \ scala-2.11 \ simple-project_2.1 1-1.0。 jar 60 70 80 90 100
答案 0 :(得分:2)
图表正常运作。想想这里发生了什么:
[1, 2, 3, ... 10]
[10, 20, 30 ... 100]
[10, 20]
[]
因此你什么都不打印。
如果您移除take(2)
全部10个元素将到达接收器,它将打印出来:
60
70
80
90
100
这与在Scala控制台中运行它(但更懒惰)非常相似:
(1 to 10).map(_ * 10).take(2).filter(_ > 50).foreach(println)