我有以下问题。我正在向服务器查询一些数据并将其作为 HttpEntity.Chunked 取回。响应字符串看起来像这样,最多有 10.000.000 行,如下所示:
[{"name":"param1","value":122343,"time":45435345},
{"name":"param2","value":243,"time":4325435},
......]
现在我想将传入的数据放入 Array[String] 中,其中每个 String 都是响应中的一行,因为稍后应该将它导入到 apache spark 数据帧中。目前我正在这样做:
//For the http request
trait StartHttpRequest {
implicit val system: ActorSystem
implicit val materializer: ActorMaterializer
def httpRequest(data: String, path: String, targetPort: Int, host: String): Future[HttpResponse] = {
val connectionFlow: Flow[HttpRequest, HttpResponse, Future[OutgoingConnection]] = {
Http().outgoingConnection(host, port = targetPort)
}
val responseFuture: Future[HttpResponse] =
Source.single(RequestBuilding.Post(uri = path, entity = HttpEntity(ContentTypes.`application/json`, data)))
.via(connectionFlow)
.runWith(Sink.head)
responseFuture
}
}
//result of the request
val responseFuture: Future[HttpResponse] = httpRequest(.....)
//convert to string
responseFuture.flatMap { response =>
response.status match {
case StatusCodes.OK =>
Unmarshal(response.entity).to[String]
}
}
//and then something like this, but with even more stupid stuff
responseFuture.onSuccess { str:String =>
masterActor! str.split("""\},\{""")
}
我的问题是,将结果放入数组的更好方法是什么?如何直接解组响应实体?因为 .to[Array[String]] 例如不起作用。而且因为有这么多行来,我可以用流来做吗?更有效?