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