本文介绍了Akka Stream 中的 Via/ViaMat/to/toMat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能解释清楚这 4 种方法有什么区别吗?什么时候使用更合适?另外一般来说这组方法的名称是什么?是否有更多方法可以完成相同的工作?scaladoc 的链接也有帮助.

-D-

解决方案

所有这些方法都是将两个流合并为一个流所必需的.例如,您可以从 SourceFlow 中创建一个 Source,或者您可以创建一个 Sink一个Flow和一个Sink,或者你可以用两个Flow创建一个Flow.>

为此,有两个基本操作,tovia.前者允许将 SourceFlow 连接到 Sink,而后者允许连接 Sourcecode> 或 FlowFlow:

source.to(sink) ->可运行图flow.to(sink) ->下沉source.via(flow) ->来源flow1.via(flow2) ->流动

作为参考,可运行图是一个完全连接的反应流,可以被物化和执行.

*Mat 各种操作的版本允许指定操作中包含的流的物化值应如何组合.您可能知道,每个流都有一个物化值,该值可以在流物化时获得.例如,Source.queue 生成一个队列对象,程序的另一部分可以使用该对象将元素发送到正在运行的流中.

默认情况下,源和流上的 tovia 只保留它被调用的流的物化值,忽略其参数的物化值:

source.to(sink) 产生源的 mat.valuesource.via(flow) 产生源的 mat.valueflow.to(sink) 产生流的 mat.valueflow1.via(flow2) 产生 flow1 的 mat.value

然而,有时您需要保留两个具体化的值或以某种方式将它们组合起来.这就是需要 Mat 方法变体的时候.它们允许您指定组合函数,该函数采用两个操作数的物化值并返回组合流的物化值:

source.to(sink) 等价于 source.toMat(sink)(Keep.left)flow1.via(flow2) 相当于 flow1.viaMat(flow2)(Keep.left)

例如,要保留两个物化值,可以使用 Keep.both 方法,或者如果您只需要正确"操作数的 mat.value,则可以使用 Keep.right 方法:

source.toMat(sink)(Keep.both) 产生一个元组(mat.value of source,mat.value of sink)

Can someone explain clearly what are the difference between those 4 methods ? When is it more appropriate to use each one ? Also generally speaking what is the name of this Group of method? Are there more method that does the same job ? A link to the scaladoc could also help.

-D-

解决方案

All these methods are necessary to join two streams into one stream. For example, you can create a Source out of a Source and a Flow, or you can create a Sink out of a Flow and a Sink, or you can create a Flow out of two Flows.

For this, there are two basic operations, to and via. The former allows one to connect either a Source or a Flow to a Sink, while the latter allows to connect a Source or a Flow to a Flow:

source.to(sink)   ->  runnable graph
flow.to(sink)     ->  sink

source.via(flow)  ->  source
flow1.via(flow2)  ->  flow

For the reference, a runnable graph is a fully connected reactive stream which is ready to be materialized and executed.

*Mat versions of various operations allow one to specify how materialized values of streams included in the operation should be combined. As you may know, each stream has a materialized value which can be obtained when the stream is materialized. For example, Source.queue yields a queue object which can be used by another part of your program to emit elements into the running stream.

By default to and via on sources and flows only keeps the materialized value of the stream it is called on, ignoring the materialized value of its argument:

source.to(sink)    yields   mat.value of source
source.via(flow)   yields   mat.value of source

flow.to(sink)      yields   mat.value of flow
flow1.via(flow2)   yields   mat.value of flow1

Sometimes, however, you need to keep both materialized values or to combined them somehow. That's when Mat variants of methods are needed. They allow you to specify the combining function which takes materialized values of both operands and returns a materialized value of the combined stream:

source.to(sink)    equivalent to   source.toMat(sink)(Keep.left)
flow1.via(flow2)   equivalent to   flow1.viaMat(flow2)(Keep.left)

For example, to keep both materialized values, you can use Keep.both method, or if you only need the mat.value of the "right" operand, you can use Keep.right method:

source.toMat(sink)(Keep.both)   yields   a tuple (mat.value of source, mat.value of sink)

这篇关于Akka Stream 中的 Via/ViaMat/to/toMat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 10:25