有人可以清楚地解释这四种方法之间的区别吗?什么时候更适合使用每个?一般来讲,这组方法的名称是什么?还有更多的方法可以完成相同的工作吗?指向scaladoc的链接也可能会有所帮助。
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-
推荐答案所有这些方法对于加入两流合一。例如,您可以在 Source 和 Flow $中创建一个 Source c $ c>,也可以从 Flow 和 Sink中创建一个 Sink ,或者您可以在两个 Flow 中创建一个 Flow 。
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.
为此,有两个基本操作,到和 via 。前者允许将 Source 或 Flow 连接到 Sink ,而后者允许将 Source 或 Flow 连接到流量:
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 版本的各种操作允许指定操作中包含的流的物化值应如何指定被合并。如您所知,每个流都有一个物化值,可以在实现该值时获得。例如, Source.queue 产生一个队列对象,程序的另一部分可以使用该队列对象将元素发射到正在运行的流中。
*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.
在源和流上默认为至和 via 仅保留其流的物化值被调用,而忽略其参数的物化值:
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有时,您需要保留两个实现的值或以某种方式组合它们。那时需要 Mat 方法的变体。它们允许您指定合并函数,该函数接受两个操作数的物化值并返回合并流的物化值:
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)例如,要保留两个物化值,可以使用 Keep.both 方法,或者,如果您只需要 right操作数的mat.value,则可以使用 Keep.right 方法:
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
发布评论