Akka流:Mat在Source [out,Mat]中代表什么 [英] Akka Streams: What does Mat represents in Source[out, Mat]

查看:179
本文介绍了Akka流:Mat在Source [out,Mat]中代表什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Akka流中,Source [Out,Mat]或Sink [In,Mat]中的Mat表示什么。

In Akka streams what does Mat in Source[Out, Mat] or Sink[In, Mat] represent. When will it actually be used?

推荐答案

Mat 类型参数表示该流的物化值的类型。

The Mat type parameter represents the type of the materialized value of this stream.

请记住,在Akka中, Source 接收器(好,所有图形)仅是蓝图,它们本身并不进行任何处理,仅描述应如何构造流。将这些蓝图转换为具有实时数据的工作流的过程称为 materialization

Remember that in Akka Source, Flow, Sink (well, all graphs) are just blueprints - they do not do any processing by themselves, they only describe how the stream should be constructed. The process of turning these blueprints into a working stream with live data is called materialization.

实现流的核心方法称为 run(),它是 RunnableGraph 类中的垫子 rel = noreferrer>已定义。运行流的所有其他方法(例如,在接收器 Source runWith c $ c>)最终委托给此方法。您可以看到此方法返回 Mat 。也就是说,实现一个流会产生一个实现的值。

The core method for materializing a stream is called run(), and it is defined in the RunnableGraph class. All other methods to run a stream (e.g. runWith on a Sink or Source) eventually delegate to this method. You can see that this method returns Mat. That is, materializing a stream yields a materialized value.

例如,有一个接收器将流中的所有值组合成一个值,它是用 Sink.fold 。但是,您如何获得这一价值?由于流异步运行,因此此值的自然类型将是 Future [T] ,其中 T 是倍数累加器的类型。事实证明, Sink.fold 返回 Sink [In,Future [T]] ,即此 Future [T] 是它的物化值,因此,当您实现它时,会得到一个 Future [T] 的实例然后可以在您自己的代码中使用以进行进一步处理:如果流正确完成,它将完成一个值;如果流因异常终止,它将完成一个失败。

For example, there is a sink which combines all the values in a stream into a single value, it is constructed with Sink.fold. But how do you get this value? Since the stream is running asynchronously, a natural type for this value would be Future[T], where T is the type of the fold accumulator. Turns out, Sink.fold returns Sink[In, Future[T]], that is, this Future[T] is its materialized value, therefore, when you materialize it, you get an instance of Future[T] which you can then use in your own code for further processing: it will complete with a value if the stream completes correctly and it will complete with a failure if the stream has terminated with an exception.

通过组合接收器,源和流(以及其他类型的图)而构建的图的每个部分都可能具有关联的物化值。例如, Source.queue 是一个队列,您可以使用它在元素实现后将其推送到流,而 Sink.actorSubscriber 的实现值是一个 ActorRef ,您可以使用它与actor交互(由实现器实现,该实现由实现器创建)。另一方面,存在 Flow.map ,它是没有有意义的物化值的流(当您仅将纯函数应用于流时,您无法从外部进行控制) ),因此其物化值为未使用,本质上是 Unit

Each part of the graph you construct by combining sinks, sources and flows (and other kinds of graphs) may potentially have an associated materialized value. For example, materialized value of Source.queue is a queue which you can use to push elements to the stream once it is materialized, and materialized value of Sink.actorSubscriber is an ActorRef which you can use to interact with the actor (which is created by the materializer when the stream is materialized). On the other hand, there is Flow.map which is a flow with no meaningful materialized value (there is nothing you can externally control when you only apply a pure function to a stream), therefore its materialized value is NotUsed, which is essentially Unit.

自然地,流的不同部分可能包含其自己的物化值。例如,没有什么可以阻止您组合 Source.queue Sink.fold 。但是 RunnableGraph.run()只能返回一个物化值。为了克服这个问题,通常在 Sink s, Flow s和其他图上有两种合并方法的变体称为方法 methodMat 之类的例子,例如 toMat 。第二个变体允许您选择如何组合要加入的流的物化值。例如,您可以将它们放入一个元组中,以同时获得它们:

Naturally, it is possible for different parts of stream contain their own materialized value. For example, nothing prevents you from combining Source.queue and Sink.fold. But RunnableGraph.run() can only return one materialized value. To overcome this, there are usually two variants of combining methods on Sinks, Flows, and other graphs, usually called like method and methodMat, e.g. to and toMat. The second variant allows you to choose how to combine materialized values of the streams you are joining. For example, you can put them into a tuple to get them both:

val (queue, future) = Source.queue[Int](10, OverflowStrategy.fail)
  .map(x => x + 10)
  .toMat(Sink.fold(0)(_ + _))(Keep.both)
  .run()

默认组合方法(没有 Mat 后缀)通常选择左侧或右侧的物化值,这取决于对这种特殊流进行最自然的处理。 保持 对象包含方便的方法,这些方法返回左,右或两个参数,特别是为了将它们用作 * Mat 方法,但是没有什么可以阻止您编写自己的合并函数。

Default combination methods (without the Mat suffix) usually choose either the left or the right materialized value, depending on what would be the most natural thing to do for this particular kind of stream. The Keep object contains convenience methods which return either left, right or both arguments, specifically for the purpose of using them as the last argument for *Mat methods, but nothing prevents you from writing your own combining function.

这篇关于Akka流:Mat在Source [out,Mat]中代表什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆