2017-03-05 5 views
0

blogを参照してドキュメントを一括アップロードすることができます。私の問題は、すでにクラウドに存在するドキュメントがほとんどないことです。したがって、提案されたアプローチは失敗しています。文書が存在する場合は、文書が雲の中に存在しない場合は、一括追加のハンドリング方法を提案してください。コードの下利用可能な場合は、クラウド内のドキュメントを更新する、またはJavaクラウドAPIを使用してドキュメントを挿入する最善のアプローチは

+0

チェックを 'upsert' opertationは、それが不在であるか、そうでない場合は、それを更新した場合の新規ドキュメントを作成する(存在する場合)。それ以外の場合は、最初にクエリを実行して、すでに存在するドキュメントを確認し、代わりに2つの操作を実行する必要があります。更新と作成。 –

+0

クイックレスポンスありがとうございました..しかし、 "cloudant-client-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); 

} 
関連する問題