Keep组合的含义?
The meaning of the Keep combination?
我试图在 akka 流中的 Keep
组合下创建以下示例:
import java.nio.file.Paths
import akka.NotUsed
import akka.actor.ActorSystem
import akka.stream.{ActorMaterializer, IOResult}
import akka.stream.scaladsl.{FileIO, Flow, Keep, Sink, Source}
import akka.util.ByteString
import scala.concurrent.Future
import scala.util.{Failure, Success}
object FileConsumer extends App {
implicit val system = ActorSystem("reactive-tweets")
implicit val materializer = ActorMaterializer()
val source: Source[Int, NotUsed] = Source(1 to 100)
val factorials = source.scan(BigInt(1))((acc, next) => acc * next)
val result: Future[IOResult] =
factorials.map(_.toString).runWith(lineSink("factorial2.txt"))
implicit val ec = system.dispatcher
result.onComplete {
case Success(v) => println(s"Fileinfo ${ v.count }")
case Failure(e) => println(e)
}
def lineSink(filename: String): Sink[String, Future[IOResult]] =
Flow[String].map(s => ByteString(s + "\n")).toMat(FileIO.toPath(Paths.get(filename)))(Keep.right)
}
在 akka streams website 上面写着:
The resulting blueprint is a Sink[String, Future[IOResult]]
, which
means that it accepts strings as its input and when materialized it
will create auxiliary information of type Future[IOResult]
(when
chaining operations on a Source or Flow the type of the auxiliary
information—called the “materialized value”—is given by the leftmost
starting point; since we want to retain what the FileIO.toPath
sink
has to offer, we need to say Keep.right
).
但是,当我想将 ByteString
保留在左侧时,我试过:
def lineSink2(filename: String): Sink[String, Future[ByteString]] =
Flow[String].map(s => ByteString(s + "\n")).toMat(Sink.foreach(println))(Keep.left)
但它根本无法编译。
我也不懂:
is given by the leftmost starting point
最左边的起点是Flow
吗?
我想,我还不明白Keep
的想法。
Sink.foreach的定义如下:
def foreach[T](f: T ⇒ Unit): Sink[T, Future[Done]]
这意味着物化价值是未来[完成]
在流量的情况下你有:
val value: Flow[String, ByteString, NotUsed] = Flow[String].map(s => ByteString(s + "\n"))
其物化值为NotUsed
在这种情况下:
Keep.left - 未使用 - 源或流的物化值
Keep.right - 未来[完成] - 水槽的物化价值
Keep.both -(未使用,未来[完成])
重要的事实是,在许多情况下物化值不是流经流的元素的值,而是
- 诊断信息
- 流状态
- 其他关于流的信息
我试图在 akka 流中的 Keep
组合下创建以下示例:
import java.nio.file.Paths
import akka.NotUsed
import akka.actor.ActorSystem
import akka.stream.{ActorMaterializer, IOResult}
import akka.stream.scaladsl.{FileIO, Flow, Keep, Sink, Source}
import akka.util.ByteString
import scala.concurrent.Future
import scala.util.{Failure, Success}
object FileConsumer extends App {
implicit val system = ActorSystem("reactive-tweets")
implicit val materializer = ActorMaterializer()
val source: Source[Int, NotUsed] = Source(1 to 100)
val factorials = source.scan(BigInt(1))((acc, next) => acc * next)
val result: Future[IOResult] =
factorials.map(_.toString).runWith(lineSink("factorial2.txt"))
implicit val ec = system.dispatcher
result.onComplete {
case Success(v) => println(s"Fileinfo ${ v.count }")
case Failure(e) => println(e)
}
def lineSink(filename: String): Sink[String, Future[IOResult]] =
Flow[String].map(s => ByteString(s + "\n")).toMat(FileIO.toPath(Paths.get(filename)))(Keep.right)
}
在 akka streams website 上面写着:
The resulting blueprint is a
Sink[String, Future[IOResult]]
, which means that it accepts strings as its input and when materialized it will create auxiliary information of typeFuture[IOResult]
(when chaining operations on a Source or Flow the type of the auxiliary information—called the “materialized value”—is given by the leftmost starting point; since we want to retain what theFileIO.toPath
sink has to offer, we need to sayKeep.right
).
但是,当我想将 ByteString
保留在左侧时,我试过:
def lineSink2(filename: String): Sink[String, Future[ByteString]] =
Flow[String].map(s => ByteString(s + "\n")).toMat(Sink.foreach(println))(Keep.left)
但它根本无法编译。
我也不懂:
is given by the leftmost starting point
最左边的起点是Flow
吗?
我想,我还不明白Keep
的想法。
Sink.foreach的定义如下:
def foreach[T](f: T ⇒ Unit): Sink[T, Future[Done]]
这意味着物化价值是未来[完成]
在流量的情况下你有:
val value: Flow[String, ByteString, NotUsed] = Flow[String].map(s => ByteString(s + "\n"))
其物化值为NotUsed
在这种情况下:
Keep.left - 未使用 - 源或流的物化值
Keep.right - 未来[完成] - 水槽的物化价值
Keep.both -(未使用,未来[完成])
重要的事实是,在许多情况下物化值不是流经流的元素的值,而是
- 诊断信息
- 流状态
- 其他关于流的信息