Scala调度流响应逐行 [英] Scala dispatch stream response line by line

查看:104
本文介绍了Scala调度流响应逐行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Scala分派,但既是Scala的新手,又是Dispatch api疯狂的象征.我对如何流式传输大型http响应并逐行处理它感到困惑.任何帮助将不胜感激.

I am attempting to use Scala dispatch but been both a Scala neewbie and the face that Dispatch api is symbol crazy Im confused on how I can stream a large http response and process it line by line. Any help would be appreciated.

干杯,克里斯.

注意:

这对我不起作用:

Http(url(Config.publisherUrl) > as.stream.Lines(line => println(line)))

从不打印行.

正在打印行,但仅当响应中有多行时.问题似乎是我可以逐行传输数据,但是最后一行被省略了.

The lines were being printed, but only when there was more than one line in the response. The issue seems to be that I can stream the data line by line, but the very last line is omitted.

推荐答案

我遇到了同样的问题.你能做什么.我将其读取为输入流,然后将其转换为Akka流并返回了源.

I had the same problem. What you could do. I read it as input stream, then converted it to Akka stream and returned source.

import akka.stream.scaladsl.{Source, StreamConverters}
  val futureStream = Http(url(urlString) > as.Response(_.getResponseBodyAsStream))
        futureStream.map { inputStream =>
          val source = () => inputStream
          StreamConverters.fromInputStream(source)
        }

对我有用.

这篇关于Scala调度流响应逐行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆