Scala:使用AmazonS3Client getObject Futures合并两个JSON文件

时间:2016-04-30 00:22:37

标签: json scala amazon-s3

我正在尝试合并来自S3存储桶的两个JSON文件。第一个文件拉得很好,但不是第二个文件。

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的结果......

this is the json result

还有另一种(更好的?)写作方式吗?

1 个答案:

答案 0 :(得分:0)

你应该等待来自不同来源的3部分JSON吗?

我可以推荐使用case class DTO

的解决方案

简单示例:

val firstJson = Future {
         //case class json1(...)
    }

    val secondJson = Future {
   //case class json2(...)
      ...
    }

    val finalFson = for {
      f <- first
      s <- second
    } yield (f, s)

finalJson onComplete {
      case Success(jsons) => {
        //merge json here
        jsons._1 + jsons._2 ... 

      }