如何将 Flow[T, Seq[Seq[String]], NotUsed] 展平为 Flow[T, Seq[String], NotUsed]

How to flatten a Flow[T, Seq[Seq[String]], NotUsed] into a Flow[T, Seq[String], NotUsed]

我有一个类型为 Flow[T, Seq[Seq[String]], NotUsed] 的流。

我想以示例流

的方式将其展平
ev1: Seq(Seq("a", "b"), Seq("n", "m")
ev2: Seq(Seq("x", "y"))

应该变成下面的流:

ev1: Seq("a", "b")
ev2: Seq("n", "m")
ev3: Seq("x", "y")

使用mapConcat(identity):

  implicit val actorSystem = ActorSystem()
  implicit val materializer = ActorMaterializer()

  val events = Vector(
    Vector(Vector(1, 2), Vector(3, 4)),
    Vector(Vector(5, 6))
  )

  Source.apply(events)
    .mapConcat(identity)
    .runForeach(println)

  actorSystem.terminate()

打印

Vector(1, 2)
Vector(3, 4)
Vector(5, 6)

一般来说,使用 mapConcat 您可以将事件序列扁平化到主流。