は私のために働いた:
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);
}
チェックを 'upsert' opertationは、それが不在であるか、そうでない場合は、それを更新した場合の新規ドキュメントを作成する(存在する場合)。それ以外の場合は、最初にクエリを実行して、すでに存在するドキュメントを確認し、代わりに2つの操作を実行する必要があります。更新と作成。 –
クイックレスポンスありがとうございました..しかし、 "cloudant-client-2.6.2.jar"には "upsert"操作がありません。 これで唯一のオプションは です。1.各文書を取り出し、存在するかどうかを確認します。 2.見つからない場合は追加してください。 3.見つかったら更新します。 –