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

查看:20
本文介绍了Akka Streams: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 SourceFlowSink(好吧,所有图)只是蓝图——它们不做任何处理他们自己,他们只描述了应该如何构造流.将这些蓝图转换为带有实时数据的工作流的过程称为物化.

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 类中.运行流的所有其他方法(例如 SinkSource 上的 runWith)最终委托给这个方法.你可以看到这个方法返回了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 它是一个没有有意义的物化值的流(当你只将一个纯函数应用于流时,你无法从外部控制任何东西),因此它的物化值是 NotUsed,本质上是 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.queueSink.fold.但是 RunnableGraph.run() 只能返回一个物化值.为了克服这个问题,在Sinks、Flows和其他图上通常有两种组合方法的变体,通常称为method和<代码>methodMat,例如totoMat.第二个变体允许您选择如何组合您正在加入的流的具体化值.例如,您可以将它们放入一个元组中以获取它们:

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 后缀)通常选择左侧或右侧物化值,这取决于对这种特定类型的流做什么最自然.Keep 对象包含返回左、右或两个参数的便捷方法,特别是为了将它们用作 *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 Streams:Mat 在 Source[out, Mat] 中代表什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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