은 나를 위해 일한 :
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 개의 작업을 실행해야합니다. 업데이트 및 생성. –
빠른 응답을 주셔서 감사합니다 ..하지만 "클라란트 - 클라이언트 - 2.6.2.jar"어떤 "upsert"작업을 가지고 있지 않습니다. 이제 유일한 옵션은 입니다. 1. 각 문서를 당겨서 있는지 확인하십시오. 2. 발견되지 않으면 추가하십시오. 3. 발견되면 업데이트하십시오. –