Search in sources :

Example 61 with BasicDBObject

use of com.mongodb.BasicDBObject in project mongo-hadoop by mongodb.

the class MongoStorageOptions method parseIndex.

private static void parseIndex(final Matcher match, final Index i) {
    // Build our index object
    i.index = new BasicDBObject();
    String index = match.group(1);
    Matcher indexKeys = KEY_VALUE_REGEX.matcher(index);
    while (indexKeys.find()) {
        i.index.put(indexKeys.group(1), Integer.parseInt(indexKeys.group(2)));
    }
    // Build our options object
    i.options = new BasicDBObject();
    String options = match.group(2);
    Matcher optionsKeys = KEY_VALUE_REGEX.matcher(options);
    while (optionsKeys.find()) {
        String value = optionsKeys.group(2);
        i.options.put(optionsKeys.group(1), Boolean.parseBoolean(value));
    }
    if (!i.options.containsField("sparse")) {
        i.options.put("sparse", false);
    }
    if (!i.options.containsField("unique")) {
        i.options.put("unique", false);
    }
    if (!i.options.containsField("dropDups")) {
        i.options.put("dropDups", false);
    }
    if (!i.options.containsField("background")) {
        i.options.put("background", false);
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Matcher(java.util.regex.Matcher)

Example 62 with BasicDBObject

use of com.mongodb.BasicDBObject in project mongo-hadoop by mongodb.

the class MongoLoaderTest method testBinaryNoSchema.

@Test
@SuppressWarnings("unchecked")
public void testBinaryNoSchema() throws IOException {
    byte[] data = new byte[] { 1, 2, 3 };
    BasicDBObject obj = new BasicDBObject("bytes", new Binary(data));
    MongoRecordReader rr = mock(MongoRecordReader.class);
    when(rr.nextKeyValue()).thenReturn(true);
    when(rr.getCurrentValue()).thenReturn(obj);
    // No explicit schema.
    MongoLoader ml = new MongoLoader();
    ml.prepareToRead(rr, null);
    Tuple result = ml.getNext();
    // Tuple just contains a Map.
    Map<String, Object> tupleContents;
    tupleContents = (Map<String, Object>) result.get(0);
    // Map contains DataByteArray with binary data.
    assertArrayEquals(data, ((DataByteArray) tupleContents.get("bytes")).get());
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) MongoRecordReader(com.mongodb.hadoop.input.MongoRecordReader) BasicDBObject(com.mongodb.BasicDBObject) Binary(org.bson.types.Binary) Tuple(org.apache.pig.data.Tuple) Test(org.junit.Test)

Example 63 with BasicDBObject

use of com.mongodb.BasicDBObject in project mongo-hadoop by mongodb.

the class MongoLoaderTest method testSimpleTupleIncorrectFieldType.

@Test
public void testSimpleTupleIncorrectFieldType() throws IOException {
    String userSchema = "t:tuple(t1:chararray, t2:float)";
    Object val = new BasicDBObject().append("t1", "t1_value").append("t2", "t2_value");
    MongoLoader ml = new MongoLoader(userSchema);
    Object result = BSONLoader.readField(val, ml.getFields()[0]);
    Tuple t = (Tuple) result;
    assertEquals(2, t.size());
    assertEquals("t1_value", t.get(0));
    assertNull(t.get(1));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicDBObject(com.mongodb.BasicDBObject) Tuple(org.apache.pig.data.Tuple) Test(org.junit.Test)

Example 64 with BasicDBObject

use of com.mongodb.BasicDBObject in project mongo-hadoop by mongodb.

the class MongoLoaderTest method testSimpleTuple.

@Test
public void testSimpleTuple() throws IOException {
    String userSchema = "t:tuple(t1:chararray, t2:chararray)";
    Object val = new BasicDBObject().append("t1", "t1_value").append("t2", "t2_value");
    MongoLoader ml = new MongoLoader(userSchema);
    Object result = BSONLoader.readField(val, ml.getFields()[0]);
    Tuple t = (Tuple) result;
    assertEquals(2, t.size());
    assertEquals("t1_value", t.get(0));
    assertEquals("t2_value", t.get(1));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicDBObject(com.mongodb.BasicDBObject) Tuple(org.apache.pig.data.Tuple) Test(org.junit.Test)

Example 65 with BasicDBObject

use of com.mongodb.BasicDBObject in project mongo-hadoop by mongodb.

the class MongoLoaderTest method testSimpleBag.

@Test
public void testSimpleBag() throws IOException {
    String userSchema = "b:{t:tuple(t1:chararray, t2:chararray)}";
    BasicDBList bag = new BasicDBList();
    bag.add(new BasicDBObject().append("t1", "t11_value").append("t2", "t12_value"));
    bag.add(new BasicDBObject().append("t1", "t21_value").append("t2", "t22_value"));
    MongoLoader ml = new MongoLoader(userSchema);
    Object result = BSONLoader.readField(bag, ml.getFields()[0]);
    DataBag b = (DataBag) result;
    Iterator<Tuple> bit = b.iterator();
    Tuple firstInnerT = bit.next();
    assertEquals(2, firstInnerT.size());
    assertEquals("t11_value", firstInnerT.get(0));
    assertEquals("t12_value", firstInnerT.get(1));
    Tuple secondInnerT = bit.next();
    assertEquals(2, secondInnerT.size());
    assertEquals("t21_value", secondInnerT.get(0));
    assertEquals("t22_value", secondInnerT.get(1));
    assertFalse(bit.hasNext());
}
Also used : BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) DataBag(org.apache.pig.data.DataBag) BasicDBObject(com.mongodb.BasicDBObject) Tuple(org.apache.pig.data.Tuple) Test(org.junit.Test)

Aggregations

BasicDBObject (com.mongodb.BasicDBObject)566 DBObject (com.mongodb.DBObject)338 Test (org.junit.Test)175 DBCollection (com.mongodb.DBCollection)77 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)68 ApiOperation (io.swagger.annotations.ApiOperation)65 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)64 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)54 ArrayList (java.util.ArrayList)53 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)52 ObjectId (org.bson.types.ObjectId)43 DBCursor (com.mongodb.DBCursor)41 HashMap (java.util.HashMap)35 BasicDBList (com.mongodb.BasicDBList)32 List (java.util.List)30 Map (java.util.Map)26 BSONObject (org.bson.BSONObject)23 MongoException (com.mongodb.MongoException)22 Date (java.util.Date)22 CustomGroupingOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation)18