本文整理汇总了Java中com.mongodb.client.model.WriteModel类的典型用法代码示例。如果您正苦于以下问题:Java WriteModel类的具体用法?Java WriteModel怎么用?Java WriteModel使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
WriteModel类属于com.mongodb.client.model包,在下文中一共展示了WriteModel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: write
import com.mongodb.client.model.WriteModel; //导入依赖的package包/类
@Override
public StorageWriteResult write(List<DataContainer> containers) throws Exception {
MongoCollection<Document> collection = MongoStorageAdapter.getCollection(MongoStorageAdapter.collectionEventRecordsName);
// Build an array of documents
List<WriteModel<Document>> documents = new ArrayList<>();
for (DataContainer container : containers) {
Document document = documentFromView(container);
//Prism.getLogger().debug(DataUtil.jsonFromDataView(container).toString());
// TTL
document.append("Expires", DateUtil.parseTimeStringToDate(expiration, true));
// Insert
documents.add(new InsertOneModel<>(document));
}
// Write
collection.bulkWrite(documents, bulkWriteOptions);
// @todo implement real results, BulkWriteResult
return new StorageWriteResult();
}
示例2: bulkWrite
import com.mongodb.client.model.WriteModel; //导入依赖的package包/类
public CompletableFuture<BulkWriteResult> bulkWrite(final String collectionName, final List<? extends WriteModel<? extends Document>> requests) {
return asyncExecutor.execute(new Callable<BulkWriteResult>() {
@Override
public BulkWriteResult call() throws Exception {
return dbExecutor.bulkWrite(collectionName, requests);
}
});
}
示例3: updateAll
import com.mongodb.client.model.WriteModel; //导入依赖的package包/类
@Override
public <P extends ParaObject> void updateAll(String appid, List<P> objects) {
if (StringUtils.isBlank(appid) || objects == null) {
return;
}
try {
ArrayList<WriteModel<Document>> updates = new ArrayList<WriteModel<Document>>();
List<String> ids = new ArrayList<String>(objects.size());
for (P object : objects) {
if (object != null) {
object.setUpdated(Utils.timestamp());
Document id = new Document(ID, object.getId());
Document data = new Document("$set", toRow(object, Locked.class, true));
UpdateOneModel<Document> um = new UpdateOneModel<Document>(id, data);
updates.add(um);
ids.add(object.getId());
}
}
BulkWriteResult res = getTable(appid).bulkWrite(updates, new BulkWriteOptions().ordered(true));
logger.debug("Updated: " + res.getModifiedCount() + ", keys: " + ids);
} catch (Exception e) {
logger.error(null, e);
}
logger.debug("DAO.updateAll() {}", objects.size());
}
示例4: fsync
import com.mongodb.client.model.WriteModel; //导入依赖的package包/类
@Override
public void fsync() {
super.fsync();
val count = new MutableInt();
Stream.of( data.values()
.stream()
.filter( m -> m.modified >= lastFsync ) )
.grouped( bulkSize )
.forEach( list -> {
count.add( list.size() );
final List<? extends WriteModel<Metadata<T>>> bulk = Lists.map( list,
metadata -> {
val id = identifier.get( metadata.object );
return new ReplaceOneModel<>( eq( "_id", new ObjectId( id ) ), metadata, UPDATE_OPTIONS_UPSERT );
} );
collection.bulkWrite( bulk );
} );
log.info( "[{}] fsync total: {}, modified: {}", collection.getNamespace(), size(), count.intValue() );
lastFsync = System.currentTimeMillis();
}
示例5: bulkDeleteDocuments
import com.mongodb.client.model.WriteModel; //导入依赖的package包/类
@Override
public BulkOperationResult bulkDeleteDocuments(
String dbName,
String collName,
BsonDocument filter,
BsonDocument shardedKeys) {
MongoDatabase mdb = client.getDatabase(dbName);
MongoCollection<BsonDocument> mcoll
= mdb.getCollection(collName, BsonDocument.class);
List<WriteModel<BsonDocument>> deletes = new ArrayList<>();
Bson _filter;
if (shardedKeys != null) {
_filter = and(filter, shardedKeys);
} else {
_filter = filter;
}
deletes.add(new DeleteManyModel<>(_filter));
BulkWriteResult result = mcoll.bulkWrite(deletes);
return new BulkOperationResult(HttpStatus.SC_OK, null, result);
}
示例6: bulkUpsertDocuments
import com.mongodb.client.model.WriteModel; //导入依赖的package包/类
public static BulkOperationResult bulkUpsertDocuments(
final MongoCollection<BsonDocument> coll,
final BsonArray documents,
final BsonDocument filter,
final BsonDocument shardKeys) {
Objects.requireNonNull(coll);
Objects.requireNonNull(documents);
ObjectId newEtag = new ObjectId();
List<WriteModel<BsonDocument>> wm = getBulkWriteModel(
coll,
documents,
filter,
shardKeys,
newEtag);
BulkWriteResult result = coll.bulkWrite(wm);
return new BulkOperationResult(HttpStatus.SC_OK, newEtag, result);
}
示例7: MongoBulkWriter
import com.mongodb.client.model.WriteModel; //导入依赖的package包/类
public MongoBulkWriter(String URI, String namespace)
{
logger = LoggerFactory.getLogger(MongoBulkWriter.class);
logger.info("Connecting to " + URI );
mongoClient = new MongoClient(new MongoClientURI(URI));
String[] parts = namespace.split("\\.");
db = mongoClient.getDatabase(parts[0]);
collection = db.getCollection(parts[1]);
ops = new ArrayList<WriteModel<Document>>();
}
示例8: bulkWrite
import com.mongodb.client.model.WriteModel; //导入依赖的package包/类
@Test
public void bulkWrite()
{
List<WriteModel<Document>> list = insertOneWithBulk();
coll.deleteOne(Filters.eq("name", "DELETEME"));
coll.bulkWrite(list, new BulkWriteOptions());
coll.deleteMany(Filters.eq("name", "DELETEME"));
}
示例9: insertOneWithBulk
import com.mongodb.client.model.WriteModel; //导入依赖的package包/类
private List<WriteModel<Document>> insertOneWithBulk()
{
List<WriteModel<Document>> list = new ArrayList<WriteModel<Document>>();
list.add(new InsertOneModel<Document>(createDeleteDocument()));
coll.bulkWrite(list);
return list;
}
示例10: put
import com.mongodb.client.model.WriteModel; //导入依赖的package包/类
/**
* Put the records in the sink.
*
* @param collection the set of records to send.
*/
@Override
public void put(Collection<SinkRecord> collection) {
List<SinkRecord> records = new ArrayList<>(collection);
for (int i = 0; i < records.size(); i++) {
Map<String, List<WriteModel<Document>>> bulks = new HashMap<>();
for (int j = 0; j < bulkSize && i < records.size(); j++, i++) {
SinkRecord record = records.get(i);
Map<String, Object> jsonMap = SchemaUtils.toJsonMap((Struct) record.value());
String topic = record.topic();
if (bulks.get(topic) == null) {
bulks.put(topic, new ArrayList<WriteModel<Document>>());
}
Document newDocument = new Document(jsonMap)
.append("_id", record.kafkaOffset());
log.trace("Adding to bulk: {}", newDocument.toString());
bulks.get(topic).add(new UpdateOneModel<Document>(
Filters.eq("_id", record.kafkaOffset()),
new Document("$set", newDocument),
new UpdateOptions().upsert(true)));
}
i--;
log.trace("Executing bulk");
for (String key : bulks.keySet()) {
try {
com.mongodb.bulk.BulkWriteResult result = mapping.get(key).bulkWrite(bulks.get(key));
} catch (Exception e) {
log.error(e.getMessage());
}
}
}
}
示例11: bulkWrite
import com.mongodb.client.model.WriteModel; //导入依赖的package包/类
public BulkWriteResult bulkWrite(final List<? extends WriteModel<? extends Document>> requests, final BulkWriteOptions options) {
if (options == null) {
return coll.bulkWrite(requests);
} else {
return coll.bulkWrite(requests, options);
}
}
示例12: bulkWrite
import com.mongodb.client.model.WriteModel; //导入依赖的package包/类
public CompletableFuture<BulkWriteResult> bulkWrite(final List<? extends WriteModel<? extends Document>> requests) {
return asyncExecutor.execute(new Callable<BulkWriteResult>() {
@Override
public BulkWriteResult call() throws Exception {
return collExecutor.bulkWrite(requests);
}
});
}
示例13: bulkWrite
import com.mongodb.client.model.WriteModel; //导入依赖的package包/类
@Override
public Observable<BulkWriteResult> bulkWrite(final List<? extends WriteModel<? extends TDocument>> requests,
final BulkWriteOptions options) {
return RxObservables.create(Observables.observe(new Block<SingleResultCallback<BulkWriteResult>>() {
@Override
public void apply(final SingleResultCallback<BulkWriteResult> callback) {
wrapped.bulkWrite(requests, options, callback);
}
}), observableAdapter);
}
示例14: updateSingleRecord
import com.mongodb.client.model.WriteModel; //导入依赖的package包/类
private void updateSingleRecord(List<WriteModel<Document>> bulkWriter,
Document key) {
// Key Query
rotateCollection();
Document query = new Document();
Document change;
if (key == null) {
int range = sequence * testOpts.workingset / 100;
int rest = sequence - range;
int recordno = rest + getNextVal(range);
query.append("_id",
new Document("w", workerID).append("i", recordno));
} else {
query.append("_id", key);
}
int updateFields = (testOpts.updateFields <= testOpts.numFields) ? testOpts.updateFields : testOpts.numFields;
if (updateFields == 1) {
long changedfield = (long) getNextVal((int) testOpts.NUMBER_SIZE);
Document fields = new Document("fld0", changedfield);
change = new Document("$set", fields);
} else {
TestRecord tr = createNewRecord();
tr.internalDoc.remove("_id");
change = new Document("$set", tr.internalDoc);
}
if (testOpts.findandmodify == false) {
bulkWriter.add(new UpdateManyModel<Document>(query, change));
} else {
this.coll.findOneAndUpdate(query, change); //These are immediate not batches
}
testResults.RecordOpsDone("updates", 1);
}
示例15: insertNewRecord
import com.mongodb.client.model.WriteModel; //导入依赖的package包/类
private TestRecord insertNewRecord(List<WriteModel<Document>> bulkWriter) {
int[] arr = new int[2];
arr[0] = testOpts.arraytop;
arr[1] = testOpts.arraynext;
TestRecord tr = createNewRecord();
bulkWriter.add(new InsertOneModel<Document>(tr.internalDoc));
return tr;
}