简体   繁体   English

流中的Akka流

[英]Akka-Stream stream within stream

I am trying to figure out how to handle a situation where in one of your stage you need to make a call that return an InputStream, where I would deal with that stream as a Source of the stage that comes further down. 我试图弄清楚如何处理在您的一个阶段中需要进行返回InputStream的调用的情况,在该情况下,我将把该流作为下一个阶段的Source处理。

eg 例如

 Source.map(e => Calls that return an InputStream)
 .via(processingFlow).runwith(sink.ignore)

I would like that the element going to Processing flow as those coming from the InputStream. 我希望去处理流的元素就像那些来自InputStream的元素一样。 This is basically a situation where I am tailing a file, reading each line, the line give me the information about a call I need to make against a CLI api, when making that call I get the Stdout as an InputStream from which to read the result. 这基本上是一种情况,我拖尾一个文件,读取每一行,该行为我提供了有关我需要针对CLI api进行的调用的信息,在进行该调用时,我将Stdout作为InputStream来从中读取结果。 Result are going to be huge most of the time, so I can just collect the all thing in memory. 在大多数情况下,结果将是巨大的,因此我可以将所有内容收集到内存中。

  • you can use StreamConverters utilities to get Source s and Sink s from java.io streams. 您可以使用StreamConverters实用程序从java.io流中获取SourceSink More info here . 更多信息在这里
  • you can use flatMapConcat or flatMapMerge to flatten a stream of Source s into a single stream. 您可以使用flatMapConcatflatMapMergeSource的流展平为单个流。 More info here . 更多信息在这里

A quick example could be: 一个简单的例子可能是:

  val source: Source[String, NotUsed] = ???
  def gimmeInputStream(name: String): InputStream = ???
  val processingFlow: Flow[ByteString, ByteString, NotUsed] = ???

  source
    .map(gimmeInputStream)
    .flatMapConcat(is ⇒ StreamConverters.fromInputStream(() ⇒ is, chunkSize = 8192))
    .via(processingFlow)
    .runWith(Sink.ignore)

However Akka Streams offers a more idiomatic DSL to read/write files in the FileIO object. 但是,Akka Streams提供了更惯用的DSL来读写FileIO对象中的文件。 More info here . 更多信息在这里

The example becomes: 该示例变为:

  val source: Source[String, NotUsed] = ???
  val processingFlow: Flow[ByteString, ByteString, NotUsed] = ???

  source
    .flatMapConcat(name ⇒ FileIO.fromPath(Paths.get(name)))
    .via(processingFlow)
    .runWith(Sink.ignore)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM