简体   繁体   English

Akka Stream 中的物化值是如何工作的

[英]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而不是torun返回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 the runWith() itself. runWith()是一种方便的方法,它会自动忽略任何其他运算符的物化值,除了那些由runWith()本身附加的runWith() In the above example it translates to using Keep.right as the combiner for materialized values.在上面的示例中,它转换为使用Keep.right作为物化值的组合器。

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

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