2016-04-30 8 views
0

S3バケットから2つのJSONファイルをマージしようとしています。最初のファイルは正常に取得されますが、2番目のファイルは正常ではありません。Scala:AmazonS3Client getObject Futuresを使用して2つの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

はこれを書いている別の(よりよい?)方法はありますか?

+0

?エレガントに見えます。 –

+0

問題は、infoJsonFutureがNonFatalエラーをスローしているということです。ファイルがS3上に本当に存在する場合、「Input length = 1」です。 AmazonS3Clientは2つの独立したスレッドで非同期に実行できないことがありますか? –

+0

別のソースからJSONを3部待つべきですか? –

答えて

0

別のソースからJSONを3部待つべきですか?

私は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 ... 

     } 
関連する問題