简体   繁体   中英

Unable to read files by using akka stream

We are trying to read files from akka streams. Files contains logs and the application is simple for write the logs into new files. But when, run our program, we are getting unexpected output. Our code is a below:

class LogsAkkaStream {

  implicit val system = ActorSystem("AkkaStreams")
  implicit val ec = system.dispatcher
  implicit val materializer = ActorMaterializer()

  val source: Source[ByteString, Future[IOResult]] = FileIO.fromPath(Paths.get("/home/harmeet/workspace/mylogs.logs"))
  val sink: Sink[ByteString, Future[IOResult]] = FileIO.toPath(Paths.get("."), Set(CREATE, WRITE, APPEND))
  val runnableGraph: RunnableGraph[Future[IOResult]] = source.to(sink)

  runnableGraph.run().foreach { result =>
    println(s"${result.status}, ${result.count} bytes read. ")
  }
}

object LogsAkkaStream extends App {
  new LogsAkkaStream
}

mylogs.logs contains 1000 line record, but output of this program is : Success(Done), 0 bytes read. Still, we are not getting the actual program.

When I put in known-working paths to files then it works in my REPL.

You'll want to make sure the path to your source file is correct, and you'll want to put a correct destination path to a file .

Also, you'll want to use toMat instead of to so that you can get both the IOResult of the Read operations as well as the Write operations. (I'd recommend doing _ zip _ so you get a Future[(IOResult, IOResult)] ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM