简体   繁体   English

Akka Streams:Mat代表什么来源[out,Mat]

[英]Akka Streams: What does Mat represents in Source[out, Mat]

In Akka streams what does Mat in Source[Out, Mat] or Sink[In, Mat] represent. 在Akka流中,Mat [Out,Mat]或Sink [In,Mat]中的Mat代表什么。 When will it actually be used? 它什么时候才会被使用?

The Mat type parameter represents the type of the materialized value of this stream. Mat类型参数表示此流的具体化值的类型。

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. 请记住,在Akka SourceFlowSink (以及所有图形)只是蓝图 - 它们本身不进行任何处理,它们只描述了如何构造流。 The process of turning these blueprints into a working stream with live data is called materialization . 将这些蓝图转换为具有实时数据的工作流的过程称为实现

The core method for materializing a stream is called run() , and it is defined in the RunnableGraph class. 实现流的核心方法称为run() ,它在RunnableGraph类中定义 All other methods to run a stream (eg runWith on a Sink or Source ) eventually delegate to this method. 所有其他的方法来运行流(例如runWithSinkSource )最终委托给此方法。 You can see that this method returns Mat . 你可以看到这个方法返回Mat That is, materializing a stream yields a materialized value. 也就是说,实现流产生物化值。

For example, there is a sink which combines all the values in a stream into a single value, it is constructed with Sink.fold . 例如,有一个接收器将流中的所有值组合成单个值,它由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. 由于流是异步运行的,因此该值的自然类型是Future[T] ,其中T是折叠累加器的类型。 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. 事实证明, Sink.fold返回Sink[In, Future[T]] ,也就是说,这个Future[T]是它的物化值,因此,当你实现它时,你得到一个Future[T]的实例,然后你就可以了在您自己的代码中使用以进行进一步处理:如果流正确完成,它将以值结束,如果流已终止异常,则将以失败结束。

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). 例如, Source.queue化值是一个队列,您可以使用该队列将元素推送到流中,并且Sink.actorSubscriberActorRef化值是一个ActorRef ,您可以使用它与actor(创建者)进行交互当物流化时,通过物化器)。 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 . 另一方面, Flow.map是一个没有任何有意义的物化值的流(当你只将纯函数应用于流时,你无法从外部控制),因此它的物化值是NotUsed ,它基本上是Unit

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 . 例如,没有什么可以阻止您组合Source.queueSink.fold But RunnableGraph.run() can only return one materialized value. 但是RunnableGraph.run()只能返回一个物化值。 To overcome this, there are usually two variants of combining methods on Sink s, Flow s, and other graphs, usually called like method and methodMat , eg to and toMat . 为了克服这个问题,通常有合并的方法的两个变种Sink S, Flow ,且其它图形,通常被称为像methodmethodMat ,如totoMat 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()

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. 默认组合方法(没有Mat后缀)通常选择左或右物化值,具体取决于对这种特定类型流最自然的事情。 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. Keep对象包含返回左,右或两个参数的便捷方法,特别是为了将它们用作*Mat方法的最后一个参数,但没有什么能阻止您编写自己的组合函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM