Search in sources :

Example 36 with DBCursor

use of com.mongodb.DBCursor in project mongo-hadoop by mongodb.

the class PigTest method testPigUpdateReplace.

@Test
public void testPigUpdateReplace() throws IOException, ParseException {
    DBCollection replaceCollection = db.getCollection("replace_test");
    for (int i = 0; i < 10; ++i) {
        replaceCollection.insert(new BasicDBObject("i", i));
    }
    runScript("/pig/replace_mus.pig");
    DBCursor cursor = replaceCollection.find().sort(new BasicDBObject("i", 1));
    for (int i = 1; i <= 10; ++i) {
        assertEquals(i, cursor.next().get("i"));
    }
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) Test(org.junit.Test) BaseHadoopTest(com.mongodb.hadoop.testutils.BaseHadoopTest)

Example 37 with DBCursor

use of com.mongodb.DBCursor in project mongo-hadoop by mongodb.

the class MongoCollectionSplitter method getShardsMap.

/**
     * Contacts the config server and builds a map of each shard's name to its
     * host(s) by examining config.shards.
     * @return a Map of shard name onto shard hostnames
     */
protected Map<String, List<String>> getShardsMap() {
    DBCursor cur = null;
    HashMap<String, List<String>> shardsMap = new HashMap<String, List<String>>();
    DB configDB;
    try {
        configDB = getConfigDB();
        DBCollection shardsCollection = configDB.getCollection("shards");
        cur = shardsCollection.find();
        while (cur.hasNext()) {
            final BasicDBObject row = (BasicDBObject) cur.next();
            String host = row.getString("host");
            // for replica sets host will look like: "setname/localhost:20003,localhost:20004"
            int slashIndex = host.indexOf('/');
            if (slashIndex > 0) {
                host = host.substring(slashIndex + 1);
            }
            shardsMap.put(row.getString("_id"), Arrays.asList(host.split(",")));
        }
    } finally {
        if (cur != null) {
            cur.close();
        }
    }
    return shardsMap;
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) HashMap(java.util.HashMap) List(java.util.List) DB(com.mongodb.DB)

Example 38 with DBCursor

use of com.mongodb.DBCursor in project mongo-hadoop by mongodb.

the class MongoPaginatingSplitter method calculateSplits.

public List<InputSplit> calculateSplits() throws SplitFailedException {
    Configuration conf = getConfiguration();
    if (!MongoConfigUtil.isRangeQueryEnabled(conf)) {
        throw new IllegalArgumentException("Cannot split using " + getClass().getName() + " when " + MongoConfigUtil.SPLITS_USE_RANGEQUERY + " is disabled.");
    }
    DBObject splitKeyObj = MongoConfigUtil.getInputSplitKey(conf);
    Set<String> splitKeys = splitKeyObj.keySet();
    if (splitKeys.size() > 1) {
        throw new IllegalArgumentException("Cannot split using " + getClass().getName() + " when " + MongoConfigUtil.INPUT_SPLIT_KEY_PATTERN + " describes a " + "compound key.");
    }
    String splitKey = splitKeys.iterator().next();
    DBObject splitKeyProjection = new BasicDBObject(splitKey, 1);
    if (!splitKey.equals("_id")) {
        splitKeyProjection.put("_id", 0);
    }
    int minDocs = MongoConfigUtil.getInputSplitMinDocs(conf);
    DBCollection inputCollection = MongoConfigUtil.getInputCollection(conf);
    DBObject query = MongoConfigUtil.getQuery(conf);
    DBObject rangeObj = null;
    List<InputSplit> splits = new ArrayList<InputSplit>();
    Object minBound = null, maxBound;
    DBCursor cursor;
    try {
        do {
            if (null == minBound) {
                cursor = inputCollection.find(query, splitKeyProjection);
            } else {
                if (null == rangeObj) {
                    rangeObj = new BasicDBObjectBuilder().push(splitKey).add("$gte", minBound).pop().get();
                    rangeObj.putAll(query);
                } else {
                    ((DBObject) rangeObj.get(splitKey)).put("$gte", minBound);
                }
                cursor = inputCollection.find(rangeObj, splitKeyProjection);
            }
            cursor = cursor.sort(splitKeyObj).skip(minDocs).limit(1).setOptions(Bytes.QUERYOPTION_NOTIMEOUT);
            if (cursor.hasNext()) {
                maxBound = cursor.next().get(splitKey);
            } else {
                maxBound = null;
            }
            BasicDBObject lowerBound = null, upperBound = null;
            if (minBound != null) {
                lowerBound = new BasicDBObject(splitKey, minBound);
            }
            if (maxBound != null) {
                upperBound = new BasicDBObject(splitKey, maxBound);
            }
            splits.add(createRangeQuerySplit(lowerBound, upperBound, query));
            minBound = maxBound;
        } while (maxBound != null);
    } finally {
        MongoConfigUtil.close(inputCollection.getDB().getMongo());
    }
    return splits;
}
Also used : BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) Configuration(org.apache.hadoop.conf.Configuration) ArrayList(java.util.ArrayList) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) BasicDBObject(com.mongodb.BasicDBObject) DBCollection(com.mongodb.DBCollection) DBCursor(com.mongodb.DBCursor) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) InputSplit(org.apache.hadoop.mapreduce.InputSplit)

Example 39 with DBCursor

use of com.mongodb.DBCursor in project mongo-java-driver by mongodb.

the class GridFS method find.

/**
     * Finds a list of files matching the given query.
     *
     * @param query the filter to apply
     * @param sort  the fields to sort with
     * @return list of gridfs files
     * @throws com.mongodb.MongoException if the operation fails
     */
public List<GridFSDBFile> find(final DBObject query, final DBObject sort) {
    List<GridFSDBFile> files = new ArrayList<GridFSDBFile>();
    DBCursor cursor = filesCollection.find(query);
    if (sort != null) {
        cursor.sort(sort);
    }
    try {
        while (cursor.hasNext()) {
            files.add(injectGridFSInstance(cursor.next()));
        }
    } finally {
        cursor.close();
    }
    return Collections.unmodifiableList(files);
}
Also used : DBCursor(com.mongodb.DBCursor) ArrayList(java.util.ArrayList)

Example 40 with DBCursor

use of com.mongodb.DBCursor in project GNS by MobilityFirst.

the class MongoRecords method selectRecordsWithin.

private MongoRecordCursor selectRecordsWithin(String collectionName, ColumnField valuesMapField, String key, String value, boolean explain) throws FailedDBOperationException {
    db.requestEnsureConnection();
    DBCollection collection = db.getCollection(collectionName);
    BasicDBList box = parseJSONArrayLocationStringIntoDBList(value);
    String fieldName = valuesMapField.getName() + "." + key;
    BasicDBObject shapeClause = new BasicDBObject("$box", box);
    BasicDBObject withinClause = new BasicDBObject("$geoWithin", shapeClause);
    BasicDBObject query = new BasicDBObject(fieldName, withinClause);
    DBCursor cursor = null;
    try {
        cursor = collection.find(query);
    } catch (MongoException e) {
        DatabaseConfig.getLogger().log(Level.FINE, "{0} selectRecordsWithin failed: {1}", new Object[] { dbName, e.getMessage() });
        throw new FailedDBOperationException(collectionName, fieldName, "Original mongo exception:" + e.getMessage());
    }
    if (explain) {
        System.out.println(cursor.explain().toString());
    }
    return new MongoRecordCursor(cursor, mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) MongoException(com.mongodb.MongoException) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Aggregations

DBCursor (com.mongodb.DBCursor)58 DBObject (com.mongodb.DBObject)49 BasicDBObject (com.mongodb.BasicDBObject)48 DBCollection (com.mongodb.DBCollection)24 HashMap (java.util.HashMap)9 MongoException (com.mongodb.MongoException)8 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)7 Function (com.google.common.base.Function)6 BasicDBList (com.mongodb.BasicDBList)6 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)6 JSONObject (org.json.JSONObject)6 QueryBuilder (com.mongodb.QueryBuilder)5 DB (com.mongodb.DB)4 ObjectId (org.locationtech.geogig.api.ObjectId)4 Iterator (java.util.Iterator)3 Nonnull (javax.annotation.Nonnull)3 NodeDocument (org.apache.jackrabbit.oak.plugins.document.NodeDocument)3 Mongo (com.mongodb.Mongo)2 GridFSDBFile (com.mongodb.gridfs.GridFSDBFile)2