-
-
Save concerned3rdparty/7f92525cc3c0dabe0899 to your computer and use it in GitHub Desktop.
Akka Streams Customer Example
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
import akka.actor.ActorSystem | |
import akka.stream.ActorFlowMaterializer | |
import akka.stream.scaladsl.{Flow, Sink, Source} | |
import scala.collection.immutable | |
import scala.util.Random | |
object InputCustomer { | |
def random():InputCustomer = { | |
InputCustomer(s"FirstName${Random.nextInt(1000)} LastName${Random.nextInt(1000)}") | |
} | |
} | |
case class InputCustomer(name: String) | |
case class OutputCustomer(firstName: String, lastName: String) | |
object CustomersExample extends App { | |
implicit val actorSystem = ActorSystem() | |
import actorSystem.dispatcher | |
implicit val flowMaterializer = ActorFlowMaterializer() | |
val inputCustomers = Source((1 to 100).map(_ => InputCustomer.random())) | |
val normalize = Flow[InputCustomer].map(c => c.name.split(" ").toList).collect { | |
case firstName::lastName::Nil => OutputCustomer(firstName, lastName) | |
} | |
val writeCustomers = Sink.foreach[OutputCustomer] { customer => | |
println(customer) | |
} | |
inputCustomers.via(normalize).runWith(writeCustomers).andThen { | |
case _ => | |
actorSystem.shutdown() | |
actorSystem.awaitTermination() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment