1. Source :
* Represents a producer of data.
* Emits elements downstream.
Example : A file reader, a data generator, or an external data source.
val source = Source(1 to 10) // Emits numbers from 1 to 10?
2. Sink :
* Represents a consumer of data.
* Consumes elements emitted by the Source.
Example : Writing to a file, printing to a console, or saving to a database.
val source = Source(1 to 10) // Emits numbers from 1 to 10?
3. Flow :
* Represents a transformation stage in the stream.
* Transforms or processes data as it flows through the pipeline.
val flow = Flow[Int].map(_ * 2) // Multiplies each element by 2?
4. Runnable Graph :
* A fully connected stream consisting of a Source, optional Flows, and a Sink.
* Can be executed to start processing data.
val runnableGraph = source.via(flow).to(sink)
runnableGraph.run()?
5. Materialization :
* A stream definition is lazy; it does nothing until it is "materialized."
* Materializing a stream creates the underlying actors and starts the flow of data.
Basic Example : A simple Akka Streams pipeline to process and print numbers:
import akka.actor.ActorSystem
import akka.stream.scaladsl.{Source, Sink, Flow}
import akka.stream.Materializer
implicit val system: ActorSystem = ActorSystem("AkkaStreamsExample")
implicit val materializer: Materializer = Materializer(system)
val source = Source(1 to 10) // Emit numbers from 1 to 10
val flow = Flow[Int].map(_ * 2) // Multiply each number by 2
val sink = Sink.foreach[Int](println) // Print each number
val runnableGraph = source.via(flow).to(sink)
runnableGraph.run() // Runs the stream?
Output :
2
4
6
8
10
12
14
16
18
20?