Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

akka stream toMat

Tags:

I am trying to understand what does toMat in akka streaming. For example:

val sink1:Sink[Int, Future[Int]]=Sink.fold[Int,Int](0)(_ + _)  val flow=Flow[Int].fold[Int](0){(x,y)=> x+y}  val runnable = Source (1 to 10).viaMat(flow)(Keep.right).toMat(sink1)(Keep.both) 
  1. what is use of viaMat vs via?
  2. what is toMat is doing between viaMat to toMat?
  3. what is use of keep.both, does it mean I can have value materialized from previous and the current one if yes then how I can get those values back.

Thanks Arun

like image 644
ASe Avatar asked Mar 05 '16 18:03

ASe


People also ask

What is a materialized value Akka?

The Akka Streams library calls them materialized values. That's because, when you plug components together, you have an inert graph, but when you call the run method, the graph comes alive, or is materialized. The Jedi value returned by materializing a graph is called a materialized value.

How does Akka stream work?

Akka Streams components work with a demand-based protocol. In other words, data flows through the graph as a response to demand from receivers. Producers then comply and send more elements downstream. A second (transparent) protocol kicks in when production of elements is faster than demand.

Can you describe what are 3 main components of Akka streams?

Akka streams consist of 3 major components in it – Source, Flow, Sink – and any non-cyclical stream consist of at least 2 components Source, Sink and any number of Flow element. Here we can say Source and Sink are the special cases of Flow.

What is flow in Akka stream?

A Flow is a set of stream processing steps that has one open input and one open output. Source Flow.scala.


2 Answers

  1. via is just a shortcut for viaMat(...)(Keep.left), and in fact this is how it's implemented: override def via[T, Mat2](flow: Graph[FlowShape[Out, T], Mat2]): Repr[T] = viaMat(flow)(Keep.left)

  2. toMat is the same as viaMat but for sinks, it lets you keep the materialized value from the left (source/flow) or right (sink) side or both

  3. Keep.both is just an alias for (a:A,b:B) => (a, b), that is a function that takes the two input parameters and return them as a tuple. It's used to have the materialized value of both left and right side when combining two flows (or source and flow or flow and sink etc)

I'll dissect your line of code:

// you're keeping the materialized value of flow val source2 = Source (1 to 10).viaMat(flow)(Keep.right) // you're keeping both materialized values, i.e. the one of flow from previous step // and the one o sink.      val runnableGraph = source2.toMat(sink)(Keep.both)  runnableGraph.run() // returns a tuple (flowMatVal, sinkMatVal) 

When you join two parts of a flow (i.e source and flow/sink or flow and sink) each of them has a materialized value that you get when you run the flow. The default behaviour when combining with via/to is keeping the left side. If you use viaMat/toMat you can choose to keep the right materialized value or both of them as a tuple.

like image 90
Giovanni Caporaletti Avatar answered Sep 21 '22 13:09

Giovanni Caporaletti


some detail I could able to get from akka-user group

https://groups.google.com/forum/#!topic/akka-user/Ofnx_XzWrTU

like image 43
ASe Avatar answered Sep 24 '22 13:09

ASe