Last active
October 30, 2018 05:58
-
-
Save aashritag/63cdbfd9706edcf69d51ffb39656cf61 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
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