Last active
April 17, 2018 21:10
-
-
Save margorczynski/358f5f372bbc4aad0ca6928b777a4d26 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample.stream | |
import akka.{Done, NotUsed} | |
import akka.actor.ActorSystem | |
import akka.stream.ActorMaterializer | |
import akka.stream.scaladsl._ | |
import scala.concurrent.Future | |
object SimpleStreamExample { | |
def main(args: Array[String]): Unit = { | |
implicit val system = ActorSystem("Sys") | |
implicit val materializer = ActorMaterializer() | |
val numbers = 1 to 1000 | |
//We create a Source that will iterate over the number sequence | |
val numberSource: Source[Int, NotUsed] = Source.fromIterator(() => numbers.iterator) | |
//Only let pass even numbers through the Flow | |
val isEvenFlow: Flow[Int, Int, NotUsed] = Flow[Int].filter((num) => num % 2 == 0) | |
//Create a Source of even random numbers by combining the random number Source with the even number filter Flow | |
val evenNumbersSource: Source[Int, NotUsed] = numberSource.via(isEvenFlow) | |
//A Sink that will write its input onto the console | |
val consoleSink: Sink[Int, Future[Done]] = Sink.foreach[Int](println) | |
//Connect the Source with the Sink and run it using the materializer | |
evenNumbersSource.runWith(consoleSink) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment