Operator doc for flatmap merge (#28488)

This commit is contained in:
Christopher Batey 2020-01-15 12:20:41 +00:00 committed by Johan Andrén
parent f891f17abd
commit a8086e86e5
3 changed files with 89 additions and 7 deletions

View file

@ -0,0 +1,32 @@
/*
* Copyright (C) 2019-2020 Lightbend Inc. <https://www.lightbend.com>
*/
package docs.stream.operators.sourceorflow
import akka.NotUsed
import akka.actor.ActorSystem
import akka.stream.scaladsl.Source
object FlatMapMerge {
implicit val system: ActorSystem = ActorSystem()
// #flatmap-merge
val source: Source[String, NotUsed] = Source(List("customer-1", "customer-2"))
// e.g. could b a query to a database
def lookupCustomerEvents(customerId: String): Source[String, NotUsed] = {
Source(List(s"$customerId-evt-1", s"$customerId-evt2"))
}
source.flatMapMerge(10, customerId => lookupCustomerEvents(customerId)).runForeach(println)
// prints - events from different customers could interleave
// customer-1-evt-1
// customer-2-evt-1
// customer-1-evt-2
// customer-2-evt-2
// #flatmap-merge
}