Search in sources :

Example 81 with DBCursor

use of com.mongodb.DBCursor in project wechat by dllwh.

the class MongodbHelper method isExsit.

/**
 * @方法描述: 是否存在
 * @创建者: 皇族灬战狼
 * @创建时间: 2016年3月4日 下午7:11:35
 * @return
 */
public boolean isExsit(String connName, DBObject query) {
    boolean result = false;
    DBCursor dbCursor = null;
    conn = getDBCollection(connName);
    if (null != conn) {
        dbCursor = conn.find(query);
        if (null != dbCursor && dbCursor.hasNext()) {
            result = true;
        }
    }
    return result;
}
Also used : DBCursor(com.mongodb.DBCursor)

Example 82 with DBCursor

use of com.mongodb.DBCursor in project wechat by dllwh.

the class MongodbHelper method findAll.

/**
 * ==============================================================
 * 数据查询query、find操作
 * ==============================================================
 */
/**
 * @param <T>
 * @方法描述: 查询所有数据
 * @创建者: 皇族灬战狼
 * @创建时间: 2016年3月4日 下午4:55:55
 * @param connName
 * @param clazz
 * @param query
 * @return
 */
public <T> List<T> findAll(String connName, Class<T> clazz, DBObject query) {
    // 创建返回的结果集
    List<T> resultList = new ArrayList<T>();
    conn = getDBCollection(connName);
    T bean = null;
    DBCursor cursor = null;
    if (null != conn) {
        // 返回集合中所有的文档
        cursor = conn.find(query);
        try {
            while (cursor.hasNext()) {
                DBObject dbObject = (DBObject) cursor.next();
                if (null != clazz) {
                    bean = clazz.newInstance();
                } else {
                    bean = dB2Bean(dbObject, bean);
                }
                resultList.add(bean);
            }
        } catch (Exception e) {
            logger.error("根据指定条件获取 " + connName + " 中的部分数据异常", e);
            e.printStackTrace();
        }
    }
    return resultList;
}
Also used : DBCursor(com.mongodb.DBCursor) ArrayList(java.util.ArrayList) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ConfigurationException(org.apache.commons.configuration.ConfigurationException)

Example 83 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 84 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 85 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)

Aggregations

DBCursor (com.mongodb.DBCursor)87 BasicDBObject (com.mongodb.BasicDBObject)70 DBObject (com.mongodb.DBObject)54 DBCollection (com.mongodb.DBCollection)42 ArrayList (java.util.ArrayList)20 JSONObject (org.json.JSONObject)15 MongoException (com.mongodb.MongoException)11 DB (com.mongodb.DB)10 Test (org.junit.Test)9 BasicDBList (com.mongodb.BasicDBList)8 HashMap (java.util.HashMap)8 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)6 JSONArray (org.json.JSONArray)5 MongoClient (com.mongodb.MongoClient)4 ObjectId (org.locationtech.geogig.api.ObjectId)4 Function (com.google.common.base.Function)3 Date (java.util.Date)3 Iterator (java.util.Iterator)3 ObjectId (org.bson.types.ObjectId)3 WorkItem (com.example.entities.WorkItem)2