2017-03-05 5 views
0

blog 참조 문서를 대량 업로드 할 수 있습니다. 내 문제는 클라우드에 이미 존재하는 문서가 거의 없다는 것입니다. 그래서 제안 된 접근법은 실패합니다. 문서가 클라우드에 존재하지 않는 경우 일괄 추가를 처리하는 방법을 제안하십시오. 그렇지 않으면 문서가있는 경우 업데이트하십시오. 코드 아래클라우드가있는 경우 문서를 업데이트하거나 Java 클라우드 API를 사용하여 문서를 삽입하는 가장 좋은 방법은 무엇입니까

+0

확인'upsert' opertation 그것이 부재하거나 업데이트를 것입니다 경우 새 문서를 생성하는 존재합니다. 그렇지 않으면 먼저 어떤 문서가 있는지 확인하고 대신 2 개의 작업을 실행해야합니다. 업데이트 및 생성. –

+0

빠른 응답을 주셔서 감사합니다 ..하지만 "클라란트 - 클라이언트 - 2.6.2.jar"어떤 "upsert"작업을 가지고 있지 않습니다. 이제 유일한 옵션은 입니다. 1. 각 문서를 당겨서 있는지 확인하십시오. 2. 발견되지 않으면 추가하십시오. 3. 발견되면 업데이트하십시오. –

답변

0

은 나를 위해 일한 :

public void upsert(List<JsonObject> bulkData,String dbName) 
{ 
    if (bulkData == null) { 
     return; 
    } 

    if(bulkData.isEmpty()){ 
     return; 
    } 
    if(null==dbName || dbName.length() <1){ 
     return; 
    } 

    int totalDocumentsToSave = 0; 
    int totalDocumentToInsert = 0; 
    int totalDocumentToUpdate = 0; 
    int totalUpdatesFailed=0; 
    int totalInsertsFailed =0; 

    totalDocumentsToSave = bulkData.size(); 


    Database db = client.database(dbName, false); 

     try { 
      for (JsonObject aDoc : bulkData) { 
       if (aDoc.get("_id") != null) { 
        String _id= aDoc.get("_id").getAsString(); 

        if(db.contains(_id)) 
        { 
         try 
         { 
          Map<String, String> aDocOnCloudant = db.getAllDocsRequestBuilder() 
             .keys(_id) 
             .includeDocs(true) 
             .build() 
             .getResponse() 
             .getIdsAndRevs(); 

           String _revId = aDocOnCloudant.get(_id); 
           aDoc.addProperty("_rev", _revId); 
           db.update(aDoc); 
           totalDocumentToUpdate++; 
         } 
         catch(Exception e) 
         { 
          totalUpdatesFailed++; 
         } 

        } 
        else 
        { 
         try 
         { 
         db.save(aDoc); 
         totalDocumentsToSave++; 
         } 
         catch(Exception e) 
         { 
          totalInsertsFailed++; 
         } 
        } 
       } 
      } 

      db.ensureFullCommit(); 

} 
     catch(Exception e){ 

     } 

     String log = " .Number of Documents to Save: " + totalDocumentsToSave + 
        " .Number of Documents inserted: " + totalDocumentToInsert + 
        " .Number of Documents to Updated: " + totalDocumentToUpdate + 
        " .Failed Inserts: " + totalInsertsFailed + 
        " .Failed Updates: " + totalUpdatesFailed + 
        " .Cloudant full commit completed"; 
     System.out.println(log); 

}