use of com.mongodb.BasicDBObjectBuilder 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.BasicDBObjectBuilder in project mongo-hadoop by mongodb.
the class PigTest method testPigProjection.
@Test
public void testPigProjection() throws IOException, ParseException {
DBCollection collection = mongoClient.getDB("mongo_hadoop").getCollection("projection_test");
String[] expected = new String[100];
for (int i = 0; i < expected.length; ++i) {
String letter = String.valueOf((char) ('a' + (i % 26)));
// {"_id": ObjectId(...), "i": <int>,
// "d": {"s": <string>, "j": <int>, "k": <int>}}
collection.insert(new BasicDBObjectBuilder().add("i", i).push("d").add("s", letter).add("j", i + 1).add("k", i % 5).pop().get());
expected[i] = "(" + i + "," + letter + "," + i % 5 + ")";
}
org.apache.pig.pigunit.PigTest test = new org.apache.pig.pigunit.PigTest(getClass().getResource("/pig/projection.pig").getPath());
test.assertOutput(expected);
}
use of com.mongodb.BasicDBObjectBuilder in project mongo-hadoop by mongodb.
the class MongoLoaderTest method testPushProjection.
@Test
public void testPushProjection() throws FrontendException {
String userSchema = "a:int, m:[]";
MongoLoader ml = new MongoLoader(userSchema);
ml.setUDFContextSignature("signature");
LoadPushDown.RequiredField aField = new LoadPushDown.RequiredField("a", 0, null, DataType.INTEGER);
List<LoadPushDown.RequiredField> mSubFields = Collections.singletonList(new LoadPushDown.RequiredField("x", 0, null, DataType.INTEGER));
LoadPushDown.RequiredField mField = new LoadPushDown.RequiredField("m", 1, mSubFields, DataType.MAP);
LoadPushDown.RequiredFieldList requiredFields = new LoadPushDown.RequiredFieldList(Arrays.asList(aField, mField));
LoadPushDown.RequiredFieldResponse response = ml.pushProjection(requiredFields);
assertTrue(response.getRequiredFieldResponse());
Properties props = UDFContext.getUDFContext().getUDFProperties(MongoLoader.class, new String[] { "signature" });
assertEquals(new BasicDBObjectBuilder().add("a", true).add("m.x", true).add("_id", false).get(), JSON.parse(props.getProperty(MongoConfigUtil.INPUT_FIELDS)));
}
use of com.mongodb.BasicDBObjectBuilder in project mongo-hadoop by mongodb.
the class MongoUpdateOutputReaderTest method testUpdate.
@Test
public void testUpdate() throws IOException {
BasicBSONObject query = new BasicDBObject("i", 42);
BasicBSONObject modifiers = new BasicDBObject("$set", new BasicDBObject("a", "b"));
DBObject update = new BasicDBObjectBuilder().add("_id", query).add("modifiers", modifiers).push("options").add("multi", true).add("upsert", false).pop().get();
MongoUpdateWritable muw = new MongoUpdateWritable(query, modifiers, false, true, false);
PipeMapRed pipeMapRed = mock(PipeMapRed.class);
when(pipeMapRed.getClientInput()).thenReturn(inputFromBSONObject(update));
MongoUpdateOutputReader reader = new MongoUpdateOutputReader();
reader.initialize(pipeMapRed);
assertTrue(reader.readKeyValue());
assertEquals(muw, reader.getCurrentValue());
}
Aggregations