问题描述
我正在学习akka流,并在代码中遇到Keep.left和Keep.right:
I am learning akka stream and encounter Keep.left and Keep.right in the code:
implicit val system = ActorSystem("KafkaProducer")
implicit val materializer = ActorMaterializer()
val source = Source(List("a", "b", "c"))
val sink = Sink.fold[String, String]("")(_ + _)
val runnable: RunnableGraph[Future[String]] = source.toMat(sink)(Keep.right)
val result: Future[String] = runnable.run()
这里的Keep.right是什么意思?
What does here Keep.right mean?
推荐答案
每个流处理阶段都可以产生一个物化值,可以使用viaMat
或toMat
(与via()
或to()
相对,分别).在代码段中,使用source.toMat(sink)
表示您有兴趣捕获源和接收器的物化值,而Keep.right
保留物化值的右侧(即接收器). Keep.left
将物化值保留在左侧(即源),而Keep.both
将允许您将两者都保留.
Every stream processing stage can produce a materialized value which can be captured using viaMat
or toMat
(as opposed to via()
or to()
, respectively). In your code snippet, the using of source.toMat(sink)
indicates that you're interested in capturing the materialized value of the source and sink and Keep.right
keeps the right side (i.e. sink) of the materialized value. Keep.left
would keep the materialized value on the left side (i.e. source), and Keep.both
would allow you to keep both.
这篇关于Keep in akka stream是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!