obl*_*ion 2 scala stream akka akka-stream
我有一个Future[Iterator].我想将这个迭代器提供给我的Stream.在这里,我仍然想像Iterator一样构建一个Source,就像我们使用的那样Source.fromIterator.
但是,Source.fromIterator由于Future ,我不能在这里使用.
也许我可以使用,Source.fromFuture但是当我尝试使用它时,在我的情况下它实际上似乎并没有从Iterator创建一个Source.来自文档:
/**
* Starts a new `Source` from the given `Future`. The stream will consist of
* one element when the `Future` is completed with a successful value, which
* may happen before or after materializing the `Flow`.
* The stream terminates with a failure if the `Future` is completed with a failure.
*/
def fromFuture[T](future: Future[T]): Source[T, NotUsed] =
fromGraph(new FutureSource(future))
Run Code Online (Sandbox Code Playgroud)
你可以使用的组合Source.fromFuture,flatMapConcat和Source.fromIterator.例如:
val futIter = Future(Iterator(1, 2, 3))
val source: Source[Int, _] =
Source.fromFuture(futIter).flatMapConcat(iter => Source.fromIterator(() => iter))
Run Code Online (Sandbox Code Playgroud)