2016-02-16 16:56:06 +01:00
|
|
|
/**
|
2018-01-04 17:26:29 +00:00
|
|
|
* Copyright (C) 2016-2018 Lightbend Inc. <https://www.lightbend.com>
|
2016-02-16 16:56:06 +01:00
|
|
|
*/
|
|
|
|
|
package docs.stream
|
|
|
|
|
|
2016-06-18 11:15:17 +02:00
|
|
|
//#stream-imports
|
2016-02-16 16:56:06 +01:00
|
|
|
import akka.stream._
|
|
|
|
|
import akka.stream.scaladsl._
|
2016-06-18 11:15:17 +02:00
|
|
|
//#stream-imports
|
|
|
|
|
|
|
|
|
|
//#other-imports
|
2016-02-16 16:56:06 +01:00
|
|
|
import akka.{ NotUsed, Done }
|
|
|
|
|
import akka.actor.ActorSystem
|
|
|
|
|
import akka.util.ByteString
|
|
|
|
|
import scala.concurrent._
|
|
|
|
|
import scala.concurrent.duration._
|
2016-04-25 19:25:26 +10:00
|
|
|
import java.nio.file.Paths
|
2016-06-18 11:15:17 +02:00
|
|
|
//#other-imports
|
|
|
|
|
|
|
|
|
|
import org.scalatest._
|
|
|
|
|
import org.scalatest.concurrent._
|
2016-02-16 16:56:06 +01:00
|
|
|
|
2017-04-06 11:17:10 +02:00
|
|
|
//#main-app
|
|
|
|
|
object Main extends App {
|
|
|
|
|
// Code here
|
|
|
|
|
}
|
|
|
|
|
//#main-app
|
|
|
|
|
|
2016-02-16 16:56:06 +01:00
|
|
|
class QuickStartDocSpec extends WordSpec with BeforeAndAfterAll with ScalaFutures {
|
|
|
|
|
implicit val patience = PatienceConfig(5.seconds)
|
|
|
|
|
|
2016-04-06 01:23:21 +02:00
|
|
|
def println(any: Any) = () // silence printing stuff
|
|
|
|
|
|
2016-02-16 16:56:06 +01:00
|
|
|
"demonstrate Source" in {
|
2017-04-06 11:17:10 +02:00
|
|
|
//#create-materializer
|
|
|
|
|
implicit val system = ActorSystem("QuickStart")
|
|
|
|
|
implicit val materializer = ActorMaterializer()
|
|
|
|
|
//#create-materializer
|
|
|
|
|
|
2016-02-16 16:56:06 +01:00
|
|
|
//#create-source
|
|
|
|
|
val source: Source[Int, NotUsed] = Source(1 to 100)
|
|
|
|
|
//#create-source
|
|
|
|
|
|
|
|
|
|
//#run-source
|
2017-10-06 10:30:28 +02:00
|
|
|
source.runForeach(i ⇒ println(i))(materializer)
|
2016-02-16 16:56:06 +01:00
|
|
|
//#run-source
|
|
|
|
|
|
|
|
|
|
//#transform-source
|
2017-10-06 10:30:28 +02:00
|
|
|
val factorials = source.scan(BigInt(1))((acc, next) ⇒ acc * next)
|
2016-02-16 16:56:06 +01:00
|
|
|
|
|
|
|
|
val result: Future[IOResult] =
|
|
|
|
|
factorials
|
2017-10-06 10:30:28 +02:00
|
|
|
.map(num ⇒ ByteString(s"$num\n"))
|
2016-04-25 19:25:26 +10:00
|
|
|
.runWith(FileIO.toPath(Paths.get("factorials.txt")))
|
2016-02-16 16:56:06 +01:00
|
|
|
//#transform-source
|
|
|
|
|
|
|
|
|
|
//#use-transformed-sink
|
|
|
|
|
factorials.map(_.toString).runWith(lineSink("factorial2.txt"))
|
|
|
|
|
//#use-transformed-sink
|
|
|
|
|
|
|
|
|
|
//#add-streams
|
2017-04-06 11:17:10 +02:00
|
|
|
factorials
|
2017-10-06 10:30:28 +02:00
|
|
|
.zipWith(Source(0 to 100))((num, idx) ⇒ s"$idx! = $num")
|
2017-04-06 11:17:10 +02:00
|
|
|
.throttle(1, 1.second, 1, ThrottleMode.shaping)
|
|
|
|
|
//#add-streams
|
|
|
|
|
.take(3)
|
|
|
|
|
//#add-streams
|
|
|
|
|
.runForeach(println)
|
2016-02-16 16:56:06 +01:00
|
|
|
//#add-streams
|
|
|
|
|
|
2017-04-06 11:17:10 +02:00
|
|
|
//#run-source-and-terminate
|
2017-10-06 10:30:28 +02:00
|
|
|
val done: Future[Done] = source.runForeach(i ⇒ println(i))(materializer)
|
2017-04-06 11:17:10 +02:00
|
|
|
|
|
|
|
|
implicit val ec = system.dispatcher
|
2017-10-06 10:30:28 +02:00
|
|
|
done.onComplete(_ ⇒ system.terminate())
|
2017-04-06 11:17:10 +02:00
|
|
|
//#run-source-and-terminate
|
|
|
|
|
|
2016-02-16 16:56:06 +01:00
|
|
|
done.futureValue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//#transform-sink
|
|
|
|
|
def lineSink(filename: String): Sink[String, Future[IOResult]] =
|
|
|
|
|
Flow[String]
|
2017-10-06 10:30:28 +02:00
|
|
|
.map(s ⇒ ByteString(s + "\n"))
|
2016-04-25 19:25:26 +10:00
|
|
|
.toMat(FileIO.toPath(Paths.get(filename)))(Keep.right)
|
2016-02-16 16:56:06 +01:00
|
|
|
//#transform-sink
|
|
|
|
|
|
|
|
|
|
}
|