[英]How does materialised value work in Akka Stream
I understood that when I run an Akka Stream graph, it will materialised the most right component.我明白当我运行 Akka Stream 图时,它会物化最正确的组件。
But doing this:但是这样做:
Source.range(1,100).to(Sink.reduce((a,b) -> a+b)).run(materializer);
Will materialise NotUsed
though the most left component is a sink that returns integer.尽管最左边的组件是一个返回整数的接收器,但将实现NotUsed
。
However, doing the same with runWith
works fine:但是,对runWith
执行相同的操作可以runWith
工作:
Source.range(1, 100).runWith(Sink.reduce((a, b) -> a + b), materializer)
.thenAccept(value -> LOGGER.info("The final value is {}", value));
What is it that I didn't understand well about the run
method?我对run
方法不太了解的地方是什么?
By default, to
retains the materialized value of the stream operator that calls that method.默认情况下, to
保留调用该方法的流运算符的物化值。 In your example...在你的例子中...
Source.range(1, 100).to(Sink.reduce((a, b) -> a + b)).run(materializer);
// ^
...the Source
invokes to
, so calling run
on the stream returns the materialized value of the Source
, which is NotUsed
, and ignores the materialized value of the Sink
. ... Source
调用to
,因此在流上调用run
返回Source
的物化值,即NotUsed
,并忽略Sink
的物化值。 This is the equivalent of running source.toMat(sink, Keep.left())
.这相当于运行source.toMat(sink, Keep.left())
。
In contrast, calling runWith
instead of to
and run
in this case returns the materialized value of the Sink
, because runWith
is a shorthand way of using Keep.right()
.相比之下,在这种情况下调用runWith
而不是to
和run
返回Sink
的物化值,因为runWith
是使用Keep.right()
的简写方式。
From the documentation :从文档:
final CompletionStage<Integer> sum = tweets.map(t -> 1).runWith(sumSink, system);
runWith()
is a convenience method that automatically ignores the materialized value of any other operators except those appended by therunWith()
itself.runWith()
是一种方便的方法,它会自动忽略任何其他运算符的物化值,除了那些由runWith()
本身附加的runWith()
。 In the above example it translates to usingKeep.right
as the combiner for materialized values.在上面的示例中,它转换为使用Keep.right
作为物化值的组合器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.