0
我想從S3存儲桶合併兩個JSON文件。第一個文件很好,但不是第二個文件。Scala:使用AmazonS3Client合併兩個JSON文件getObject期貨
val eventLogJsonFuture = Future(new AmazonS3Client(credentials))
.map(_.getObject(logBucket, logDirectory + "/" + id + "/event_log.json"))
.map(_.getObjectContent)
.map(Source.fromInputStream(_))
.map(_.mkString)
.map(Json.parse) map { archiveEvents =>
Json.toJson(Json.obj("success" -> true, "data" -> archiveEvents))
} recover {
case NonFatal(error) =>
Json.obj("success" -> false, "errorCode" -> "archive_does_not_exist", "message" -> error.getMessage)
}
val infoJsonFuture = Future(new AmazonS3Client(credentials))
.map(_.getObject(logBucket, logDirectory + "/" + id + "/info.json"))
.map(_.getObjectContent)
.map(Source.fromInputStream(_))
.map(_.mkString)
.map(Json.parse) map { archiveInfo =>
Json.toJson(Json.obj("success" -> true, "data" -> archiveInfo))
} recover {
case NonFatal(error) =>
Json.obj("success" -> false, "errorCode" -> "archive_does_not_exist", "message" -> error.getMessage)
}
val combinedJson = for {
eventLogJson <- eventLogJsonFuture
infoJson <- infoJsonFuture
}
yield {
Json.obj("info" -> infoJson, "events" -> eventLogJson)
}
這是JSON的結果看起來像......
有沒有寫這個的另一個(更好?)的方式?
有什麼問題你現在怎麼做呢?看起來優雅。 –
問題是,當文件確實存在於S3上時,infoJsonFuture正在拋出非致命錯誤...「輸入長度= 1」。是否可能是AmazonS3Client可能無法在兩個單獨的線程上異步運行? –
如果您等待來自不同來源的JSON的3個部分? 「 – 2016-05-20 14:12:41