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;
}
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;
}
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;
}
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;
}
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"));
}
}
Aggregations