[英]Akka-http - write to response output stream
I need to create a large tab separated file as a response to HTTP GET request. 我需要创建一个大的选项卡分隔文件作为对HTTP GET请求的响应。 In my route a create some Scala objects and then I want to write some custom representation of those objects to the Output Stream. 在我的路线中,创建一些Scala对象,然后将这些对象的一些自定义表示形式写入输出流。
It is not just serialization to tab separated instead of JSON, because I need also to create a header with column names, so IMHO this can't be solved with custom marshaling. 这不仅仅是序列化到制表符分隔的格式,而不仅仅是JSON,因为我还需要创建带有列名的标头,所以恕我直言,这不能通过自定义封送处理解决。
So how can I get a writer or outputstream from HttpRequest? 那么如何从HttpRequest获取编写器或输出流?
Something like 就像是
~path("export") {
get {
val sampleExonRPKMs = exonRPKMService.getRPKMs(samples)
val writer = HttpResponse().getWriter // this does not exists
writeHeader(writer)
... // write objects tab separated
}
}
You can complete an Akka HTTP route with a marshallable source. 您可以使用可编组的源代码完成Akka HTTP路由。 If you don't want to use custom marshallers, you can always complete with a Source[ByteString, _]
. 如果您不想使用自定义编组器,则始终可以使用Source[ByteString, _]
。 See the docs for more info. 有关更多信息,请参阅文档 。
Your route will look something like 您的路线看起来像
get {
val sampleExonRPKMs = exonRPKMService.getRPKMs(samples)
val headers: String = ???
Source.single(headers).concat(Source(sampleExonRPKMs).map(_.toTSVLine)).intersperse("\n").map(ByteString.apply)
}
Note as a separate issue: if you are dealing with large amounts of data, the getRPKMs
call will result in loading all of it in memory. 请注意,这是一个单独的问题:如果您要处理大量数据,则getRPKMs
调用将导致将所有数据加载到内存中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.