doc: Partition Streams operator, #25468 (#28090)

* doc: Partition Streams operator, #25468

* example intro text
This commit is contained in:
Patrik Nordwall 2019-11-26 14:11:43 +01:00 committed by Christopher Batey
parent 4d99440e00
commit 38d41e11fb
3 changed files with 116 additions and 0 deletions

View file

@ -0,0 +1,44 @@
/*
* Copyright (C) 2019 Lightbend Inc. <https://www.lightbend.com>
*/
package docs.stream.operators
import akka.actor.ActorSystem
object PartitionDocExample {
implicit val system: ActorSystem = ???
//#partition
import akka.NotUsed
import akka.stream.Attributes
import akka.stream.Attributes.LogLevels
import akka.stream.ClosedShape
import akka.stream.scaladsl.Flow
import akka.stream.scaladsl.GraphDSL
import akka.stream.scaladsl.Partition
import akka.stream.scaladsl.RunnableGraph
import akka.stream.scaladsl.Sink
import akka.stream.scaladsl.Source
val source: Source[Int, NotUsed] = Source(1 to 10)
val even: Sink[Int, NotUsed] =
Flow[Int].log("even").withAttributes(Attributes.logLevels(onElement = LogLevels.Info)).to(Sink.ignore)
val odd: Sink[Int, NotUsed] =
Flow[Int].log("odd").withAttributes(Attributes.logLevels(onElement = LogLevels.Info)).to(Sink.ignore)
RunnableGraph
.fromGraph(GraphDSL.create() { implicit builder =>
import GraphDSL.Implicits._
val partition = builder.add(Partition[Int](2, element => if (element % 2 == 0) 0 else 1))
source ~> partition.in
partition.out(0) ~> even
partition.out(0) ~> odd
ClosedShape
})
.run()
//#partition
}