Example usage for com.mongodb DBCollection initializeOrderedBulkOperation

List of usage examples for com.mongodb DBCollection initializeOrderedBulkOperation

Introduction

In this page you can find the example usage for com.mongodb DBCollection initializeOrderedBulkOperation.

Prototype

public BulkWriteOperation initializeOrderedBulkOperation() 

Source Link

Document

Creates a builder for an ordered bulk write operation, consisting of an ordered collection of write requests, which can be any combination of inserts, updates, replaces, or removes.

Usage

From source file:com.impetus.client.mongodb.MongoDBClient.java

License:Apache License

@Override
public int executeBatch() {
    Map<String, BulkWriteOperation> bulkWriteOperationMap = new HashMap<String, BulkWriteOperation>();
    int size = 0;
    for (Node node : nodes) {
        if (node.isDirty()) {
            node.handlePreEvent();//from ww  w.  j  a  v a 2s.  co  m
            // delete can not be executed in batch
            if (node.isInState(RemovedState.class)) {
                delete(node.getData(), node.getEntityId());
            } else {
                List<RelationHolder> relationHolders = getRelationHolders(node);
                EntityMetadata metadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata,
                        node.getDataClass());
                Map<String, DBObject> documents = handler.getDocumentFromEntity(metadata, node.getData(),
                        relationHolders, kunderaMetadata);
                for (String tableName : documents.keySet()) {
                    if (!bulkWriteOperationMap.containsKey(tableName)) {
                        DBCollection collection = mongoDb.getCollection(tableName);
                        BulkWriteOperation builder = null;
                        if (isOrderedBulkOperation()) {
                            builder = collection.initializeOrderedBulkOperation();
                        } else {
                            builder = collection.initializeUnorderedBulkOperation();
                        }
                        bulkWriteOperationMap.put(tableName, builder);
                    }

                    if (!node.isUpdate()) {
                        bulkWriteOperationMap.get(tableName).insert(documents.get(tableName));
                    }

                    else {
                        bulkWriteOperationMap.get(tableName).find(new BasicDBObject("_id", node.getEntityId()))
                                .upsert().replaceOne(documents.get(tableName));
                    }
                    size++;
                }
                indexNode(node, metadata);
            }
            node.handlePostEvent();
        }
    }
    onFlushBatch(bulkWriteOperationMap);
    return size;
}

From source file:com.nec.strudel.tkvs.store.tokumx.TokumxTransaction.java

License:Apache License

public TokumxTransaction(String gName, Key gKey, String dbname, DB db, TokumxReader kvs, DBCollection coll,
        TransactionProfiler prof) {//  w  w  w  .ja  va 2s .  c  o m
    super(gName, gKey, kvs, prof);
    db.requestStart();
    this.db = db;
    //this.coll = coll;
    this.kvs = kvs;
    this.gName = gName;
    this.prof = prof;
    this.bulkwriter = coll.initializeOrderedBulkOperation();

    DBObject cmd = new BasicDBObject("beginTransaction", 1).append("isolation", "serializable");
    //Default is MVCC snapshot isolation level.
    //       DBObject cmd = new BasicDBObject("beginTransaction", 1);
    CommandResult res = runCommand(db, cmd);
    if (!res.ok()) {
        LOGGER.warn("failed to begin transaction: " + res.getErrorMessage());
    }
}

From source file:com.zjy.mongo.output.MongoOutputCommitter.java

License:Apache License

@Override
public void commitTask(final TaskAttemptContext taskContext) throws IOException {
    LOG.info("Committing task.");

    collections = MongoConfigUtil.getOutputCollections(taskContext.getConfiguration());
    numberOfHosts = collections.size();// ww  w  .j  a  va  2s  .co m

    // Get temporary file.
    Path tempFilePath = getTaskAttemptPath(taskContext);
    LOG.info("Committing from temporary file: " + tempFilePath.toString());
    long filePos = 0, fileLen;
    FSDataInputStream inputStream = null;
    try {
        FileSystem fs = FileSystem.get(taskContext.getConfiguration());
        inputStream = fs.open(tempFilePath);
        fileLen = fs.getFileStatus(tempFilePath).getLen();
    } catch (IOException e) {
        LOG.error("Could not open temporary file for committing", e);
        cleanupAfterCommit(inputStream, taskContext);
        throw e;
    }

    int maxDocs = MongoConfigUtil.getBatchSize(taskContext.getConfiguration());
    int curBatchSize = 0;
    DBCollection coll = getDbCollectionByRoundRobin();
    BulkWriteOperation bulkOp = coll.initializeOrderedBulkOperation();

    // Read Writables out of the temporary file.
    BSONWritable bw = new BSONWritable();
    MongoUpdateWritable muw = new MongoUpdateWritable();
    while (filePos < fileLen) {
        try {
            // Determine writable type, and perform corresponding operation
            // on MongoDB.
            int mwType = inputStream.readInt();
            if (MongoWritableTypes.BSON_WRITABLE == mwType) {
                bw.readFields(inputStream);
                bulkOp.insert(new BasicDBObject(bw.getDoc().toMap()));
            } else if (MongoWritableTypes.MONGO_UPDATE_WRITABLE == mwType) {
                muw.readFields(inputStream);
                DBObject query = new BasicDBObject(muw.getQuery().toMap());
                DBObject modifiers = new BasicDBObject(muw.getModifiers().toMap());
                if (muw.isMultiUpdate()) {
                    if (muw.isUpsert()) {
                        bulkOp.find(query).upsert().update(modifiers);
                    } else {
                        bulkOp.find(query).update(modifiers);
                    }
                } else {
                    if (muw.isUpsert()) {
                        bulkOp.find(query).upsert().updateOne(modifiers);
                    } else {
                        bulkOp.find(query).updateOne(modifiers);
                    }
                }
            } else {
                throw new IOException("Unrecognized type: " + mwType);
            }
            filePos = inputStream.getPos();
            // Write to MongoDB if the batch is full, or if this is the last
            // operation to be performed for the Task.
            if (++curBatchSize >= maxDocs || filePos >= fileLen) {
                try {
                    bulkOp.execute();
                } catch (MongoException e) {
                    LOG.error("Could not write to MongoDB", e);
                    throw e;
                }
                coll = getDbCollectionByRoundRobin();
                bulkOp = coll.initializeOrderedBulkOperation();
                curBatchSize = 0;

                // Signal progress back to Hadoop framework so that we
                // don't time out.
                taskContext.progress();
            }
        } catch (IOException e) {
            LOG.error("Error reading from temporary file", e);
            throw e;
        }
    }

    cleanupAfterCommit(inputStream, taskContext);
}

From source file:edu.csulaerp.db.ReferenceMongo.java

License:Apache License

/**
 * Run this main method to see the output of this quick example.
 *
 * @param args takes no args//  w w  w  .j a v  a  2s  . com
 * @throws UnknownHostException if it cannot connect to a MongoDB instance at localhost:27017
 */
public static void main(final String[] args) throws UnknownHostException {
    // connect to the local database server
    MongoClient mongoClient = new MongoClient();

    /*
    // Authenticate - optional
    MongoCredential credential = MongoCredential.createMongoCRCredential(userName, database, password);
    MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential));
    */

    // get handle to "mydb"
    DB db = mongoClient.getDB("mydb");

    // get a list of the collections in this database and print them out
    Set<String> collectionNames = db.getCollectionNames();
    for (final String s : collectionNames) {
        System.out.println(s);
    }

    // get a collection object to work with
    DBCollection coll = db.getCollection("testCollection");

    // drop all the data in it
    coll.drop();

    // make a document and insert it
    BasicDBObject doc = new BasicDBObject("name", "MongoDB").append("type", "database").append("count", 1)
            .append("info", new BasicDBObject("x", 203).append("y", 102));

    coll.insert(doc);

    // get it (since it's the only one in there since we dropped the rest earlier on)
    DBObject myDoc = coll.findOne();
    System.out.println(myDoc);

    // now, lets add lots of little documents to the collection so we can explore queries and cursors
    for (int i = 0; i < 100; i++) {
        coll.insert(new BasicDBObject().append("i", i));
    }
    System.out
            .println("total # of documents after inserting 100 small ones (should be 101) " + coll.getCount());

    // lets get all the documents in the collection and print them out
    DBCursor cursor = coll.find();
    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    } finally {
        cursor.close();
    }

    // now use a query to get 1 document out
    BasicDBObject query = new BasicDBObject("i", 71);
    cursor = coll.find(query);

    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    } finally {
        cursor.close();
    }

    // $ Operators are represented as strings
    query = new BasicDBObject("j", new BasicDBObject("$ne", 3)).append("k", new BasicDBObject("$gt", 10));

    cursor = coll.find(query);

    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    } finally {
        cursor.close();
    }

    // now use a range query to get a larger subset
    // find all where i > 50
    query = new BasicDBObject("i", new BasicDBObject("$gt", 50));
    cursor = coll.find(query);

    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    } finally {
        cursor.close();
    }

    // range query with multiple constraints
    query = new BasicDBObject("i", new BasicDBObject("$gt", 20).append("$lte", 30));
    cursor = coll.find(query);

    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    } finally {
        cursor.close();
    }

    // Count all documents in a collection but take a maximum second to do so
    coll.find().maxTime(1, SECONDS).count();

    // Bulk operations
    BulkWriteOperation builder = coll.initializeOrderedBulkOperation();
    builder.insert(new BasicDBObject("_id", 1));
    builder.insert(new BasicDBObject("_id", 2));
    builder.insert(new BasicDBObject("_id", 3));

    builder.find(new BasicDBObject("_id", 1)).updateOne(new BasicDBObject("$set", new BasicDBObject("x", 2)));
    builder.find(new BasicDBObject("_id", 2)).removeOne();
    builder.find(new BasicDBObject("_id", 3)).replaceOne(new BasicDBObject("_id", 3).append("x", 4));

    BulkWriteResult result = builder.execute();
    System.out.println("Ordered bulk write result : " + result);

    // Unordered bulk operation - no guarantee of order of operation
    builder = coll.initializeUnorderedBulkOperation();
    builder.find(new BasicDBObject("_id", 1)).removeOne();
    builder.find(new BasicDBObject("_id", 2)).removeOne();

    result = builder.execute();
    System.out.println("Ordered bulk write result : " + result);

    // parallelScan
    ParallelScanOptions parallelScanOptions = ParallelScanOptions.builder().numCursors(3).batchSize(300)
            .build();

    List<Cursor> cursors = coll.parallelScan(parallelScanOptions);
    for (Cursor pCursor : cursors) {
        while (pCursor.hasNext()) {
            System.out.println(pCursor.next());
        }
    }

    // release resources
    db.dropDatabase();
    mongoClient.close();
}

From source file:io.hipstogram.trident.mongodb.MongoDBState.java

License:Apache License

@Override
public void commit(Long txid) {
    LOG.debug("Commiting [{}]", txid);
    DBCollection coll = client.getCollection(configuration);
    BulkWriteOperation builder = coll.initializeOrderedBulkOperation();

    int i = 0;/*from w ww  .j  ava 2 s. c  o m*/
    for (CRUDOperation operation : this.operations) {
        operation.addToBulkOperation(builder);
        i++;
        if (i >= this.maxBatchSize) {
            builder.execute();
            builder = coll.initializeOrderedBulkOperation();
            i = 0;
        }
    }

    builder.execute();
}

From source file:org.springframework.data.mongodb.core.DefaultBulkOperations.java

License:Apache License

private final BulkWriteOperation initBulkOperation() {

    DBCollection collection = mongoOperations.getCollection(collectionName);

    switch (bulkMode) {
    case ORDERED:
        return collection.initializeOrderedBulkOperation();
    case UNORDERED:
        return collection.initializeUnorderedBulkOperation();
    }/* w w  w  .j a v a2 s .  c o m*/

    throw new IllegalStateException("BulkMode was null!");
}