Search in sources :

Example 41 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 42 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 43 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 44 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 45 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)497 DBObject (com.mongodb.DBObject)289 Test (org.junit.Test)166 DBCollection (com.mongodb.DBCollection)72 ArrayList (java.util.ArrayList)48 ObjectId (org.bson.types.ObjectId)40 DBCursor (com.mongodb.DBCursor)38 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)38 ApiOperation (io.swagger.annotations.ApiOperation)37 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)36 HashMap (java.util.HashMap)32 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)30 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)29 BasicDBList (com.mongodb.BasicDBList)28 List (java.util.List)24 Map (java.util.Map)23 BSONObject (org.bson.BSONObject)23 MongoException (com.mongodb.MongoException)22 Date (java.util.Date)18 IOException (java.io.IOException)15