Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import akka.stream.scaladsl.{Flow, Framing}
- // Given a stream of bytestrings delimited by the system line separator we can get lines represented as Strings
- val lines = Flow[ByteString]
- .via(Framing.delimiter(ByteString(System.lineSeparator), 10000, allowTruncation = true))
- .map(bs => bs.utf8String)
- // given as stream of Paths we read those files and count the number of lines
- val lineCounter = Flow[Path]
- .flatMapConcat(path => FileIO.fromPath(path).via(lines))
- .fold(0l)((count, line) => count + 1)
- .toMat(Sink.head)(Keep.right)
- // Here's our test data source (replace paths with real paths)
- val testFiles = Source(List("somePathToFile1", "somePathToFile2").map(new File(_).toPath))
- // Runs the line counter over the test files, returns a Future, which contains the number of lines, which we then print out to the console when it completes
- testFiles.runWith(lineCounter).foreach(println)
Add Comment
Please, Sign In to add comment