
In this blog, I will discuss the basics of the Akka Streams. So lets start with the introduction of Akka Stream.
What is Akka Stream ?
Lets understand stream first, So Stream is basically a flow of sequence of large amount of data that can be infinite from one source to another. Akka Streams is a module built on top of Akka Actors to make the ingestion and processing of streams easy. It provides easy-to-use APIs to create streams that leverage the power of the Akka toolkit without explicitly defining actor behaviors and messages. Basically the Akka Streams is a library to process and transfer a sequence of elements using bounded buffer space.
What is Source ?
Source is basically receive data from file,database,message queue, collection. It has only one output to produce the data. We can say that source is the entry point of the stream. There must be at least one source in every stream. There are many ways we can create different types of sources. So lets understand with the examples how to create different types of sources.
val emptySource = Source.empty
val rangeSource: Source[Int, NotUsed] = Source(1 to 10)
val singleSource: Source[String, NotUsed] = Source.single("1")
val listSource: Source[Int, NotUsed] = Source(List(1, 2, 3,4,5))
What is Sink ?
Sink is the exit point of your stream. There must be at least one Sink in every stream. It send demand to the source then only source send the data. And it take two parameters the first one is the type of element sink consumes and other one is the materialized value. It takes data from source and produce the output. Below are the examples to create the different types of sink.
val sink = Sink.fold[Int, Int](0)(_ + _)
val returnFirstElement: Sink[Int, Future[Int]] = Sink.head
val ignoreElements: Sink[Int, Future[Done]] = Sink.ignore
What is Flow ?
The flow is basically the processing step within the stream. It combines one incoming channel and one outgoing channel. It takes input from the source and process and transform on that input and then transfer to the sink. And it is parameterized with the three values. The first parameter is the type of the element it take from source ,second parameter is the type that it produce and third is the type of the materialized value. Below are the examples to create flow.
val flow = Flow[Int].map(elem => elem * 2)
val fliterFlow = Flow[Int].filter(_ > 0)
What is Runnable Graph ?
Runnable graph is the combination of all the three components source, flow and sink. When these three components are connected then the graph is created and that graph is ready to run to start the flow of the stream. To create a graph the source must be connect with the flow using via method and flow is connect to a sink using to method. To run the created graph we use run method . Let’s understand how we can create graph with the example .
val source = Source(1 to 10)
val sink = Sink.foreach[Int](println)
val flow = Flow[Int].map(elem => elem * 2)
val runnable = source via flow to sink
runnable.run()
That’s all for this blog , I will discuss each feature of akka stream in details in my upcoming blogs.
Reference
https://doc.akka.io/docs/akka/current/stream/index.html