Skip to content

Instantly share code, notes, and snippets.

@aashritag
Last active October 30, 2018 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aashritag/63cdbfd9706edcf69d51ffb39656cf61 to your computer and use it in GitHub Desktop.
Save aashritag/63cdbfd9706edcf69d51ffb39656cf61 to your computer and use it in GitHub Desktop.
val users = List(
User(1, "Aashrita", 22),
User(2, "Mark", 12),
User(3, "Ron", 19),
User(4, "Adam", 20)
)
val userSource: Source[User, NotUsed] = Source(users)
def writeToFile(filename: String): Sink[User, Future[IOResult]] = {
Flow[User]
.map(_.toString)
.map(ByteString(_))
.toMat(FileIO.toPath(Paths.get(filename)))(Keep.right)
}
val g = RunnableGraph.fromGraph(GraphDSL.create() { implicit builder =>
import GraphDSL.Implicits._
val bcast = builder.add(Broadcast[User](2))
userSource ~> bcast.in
bcast.out(0) ~> Flow[User].filter(_.age >= 18) ~> writeToFile("a.txt")
bcast.out(1) ~> Flow[User].filter(_.age < 18) ~> writeToFile("b.txt")
ClosedShape
})
g.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment