Alternative flows based on condition for akka stream

前端 未结 1 1528
梦毁少年i
梦毁少年i 2020-12-31 19:13

Have a stream with custom flows and at a certain stage I want to split the stream and have two alternative data handling which will merge again later.

E.g.



        
相关标签:
1条回答
  • 2020-12-31 19:44

    You can use Broadcast to split the stream, then you will able to use filter or collect on each of streams to filter required data.

    val split = builder.add(Broadcast[Int](2))
    
    Src -> F1 -> split -> filterCondA -> F3 -> F6 -> Merge -> Sink
                       -> filterCondB -> F4 -> F5 -> Merge
    

    Also, there is Partition stage which handles the number of output ports and the map function from value to port number f: T => Int.

    val portMapper(value: T): Int = value match {
      case CondA => 0
      case CondB => 1
    }
    
    val split = builder.add(Partition[T](2, portMapper))
    
    Src -> F1 -> split -> F3 -> F6 -> Merge -> Sink
                 split -> F4 -> F5 -> Merge
    

    Maybe there is some simpler way.

    0 讨论(0)
提交回复
热议问题