add new Streams Quick Start
also move IOResult to akka.stream package
This commit is contained in:
parent
5d3a8256c1
commit
125c996fae
23 changed files with 459 additions and 26 deletions
75
akka-docs/rst/scala/code/docs/stream/QuickStartDocSpec.scala
Normal file
75
akka-docs/rst/scala/code/docs/stream/QuickStartDocSpec.scala
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
/**
|
||||
* Copyright (C) 2016 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
package docs.stream
|
||||
|
||||
//#imports
|
||||
import akka.stream._
|
||||
import akka.stream.scaladsl._
|
||||
//#imports
|
||||
import akka.{ NotUsed, Done }
|
||||
import akka.actor.ActorSystem
|
||||
import akka.util.ByteString
|
||||
|
||||
import org.scalatest._
|
||||
import org.scalatest.concurrent._
|
||||
import scala.concurrent._
|
||||
import scala.concurrent.duration._
|
||||
import java.io.File
|
||||
|
||||
class QuickStartDocSpec extends WordSpec with BeforeAndAfterAll with ScalaFutures {
|
||||
implicit val patience = PatienceConfig(5.seconds)
|
||||
|
||||
//#create-materializer
|
||||
implicit val system = ActorSystem("QuickStart")
|
||||
implicit val materializer = ActorMaterializer()
|
||||
//#create-materializer
|
||||
|
||||
override def afterAll(): Unit = {
|
||||
system.terminate()
|
||||
}
|
||||
|
||||
"demonstrate Source" in {
|
||||
//#create-source
|
||||
val source: Source[Int, NotUsed] = Source(1 to 100)
|
||||
//#create-source
|
||||
|
||||
//#run-source
|
||||
source.runForeach(i => println(i))(materializer)
|
||||
//#run-source
|
||||
|
||||
//#transform-source
|
||||
val factorials = source.scan(BigInt(1))((acc, next) => acc * next)
|
||||
|
||||
val result: Future[IOResult] =
|
||||
factorials
|
||||
.map(num => ByteString(s"$num\n"))
|
||||
.runWith(FileIO.toFile(new File("factorials.txt")))
|
||||
//#transform-source
|
||||
|
||||
//#use-transformed-sink
|
||||
factorials.map(_.toString).runWith(lineSink("factorial2.txt"))
|
||||
//#use-transformed-sink
|
||||
|
||||
//#add-streams
|
||||
val done: Future[Done] =
|
||||
factorials
|
||||
.zipWith(Source(0 to 100))((num, idx) => s"$idx! = $num")
|
||||
.throttle(1, 1.second, 1, ThrottleMode.shaping)
|
||||
//#add-streams
|
||||
.take(3)
|
||||
//#add-streams
|
||||
.runForeach(println)
|
||||
//#add-streams
|
||||
|
||||
done.futureValue
|
||||
}
|
||||
|
||||
//#transform-sink
|
||||
def lineSink(filename: String): Sink[String, Future[IOResult]] =
|
||||
Flow[String]
|
||||
.map(s => ByteString(s + "\n"))
|
||||
.toMat(FileIO.toFile(new File(filename)))(Keep.right)
|
||||
//#transform-sink
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue