Search in sources :

Example 41 with BasicDBObjectBuilder

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;
}
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 42 with BasicDBObjectBuilder

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);
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) Test(org.junit.Test) BaseHadoopTest(com.mongodb.hadoop.testutils.BaseHadoopTest)

Example 43 with BasicDBObjectBuilder

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)));
}
Also used : BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) LoadPushDown(org.apache.pig.LoadPushDown) Properties(java.util.Properties) Test(org.junit.Test)

Example 44 with BasicDBObjectBuilder

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());
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) BasicDBObject(com.mongodb.BasicDBObject) BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) MongoUpdateWritable(com.mongodb.hadoop.io.MongoUpdateWritable) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) PipeMapRed(org.apache.hadoop.streaming.PipeMapRed) Test(org.junit.Test)

Aggregations

BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)44 DBObject (com.mongodb.DBObject)30 Test (org.junit.Test)21 DBCollection (com.mongodb.DBCollection)18 BasicDBObject (com.mongodb.BasicDBObject)12 ArrayList (java.util.ArrayList)7 BasicDBList (com.mongodb.BasicDBList)6 List (java.util.List)4 ResourceFieldSchema (org.apache.pig.ResourceSchema.ResourceFieldSchema)4 HiveTest (com.mongodb.hadoop.hive.HiveTest)3 IOException (java.io.IOException)3 Map (java.util.Map)3 MongoException (com.mongodb.MongoException)2 Configuration (org.apache.hadoop.conf.Configuration)2 JobConf (org.apache.hadoop.mapred.JobConf)2 InputSplit (org.apache.hadoop.mapreduce.InputSplit)2 ResourceSchema (org.apache.pig.ResourceSchema)2 RyaStatement (org.apache.rya.api.domain.RyaStatement)2 BasicBSONObject (org.bson.BasicBSONObject)2 GeometryType (org.teiid.core.types.GeometryType)2