Search in sources :

Example 81 with BasicBSONObject

use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.

the class TagsReducer method reduce.

@Override
public void reduce(final Text key, final Iterator<BSONWritable> values, final OutputCollector<NullWritable, MongoUpdateWritable> output, final Reporter reporter) throws IOException {
    BasicDBObject query = new BasicDBObject("_id", key.toString());
    ArrayList<BSONObject> books = new ArrayList<BSONObject>();
    while (values.hasNext()) {
        books.add(values.next().getDoc());
    }
    BasicBSONObject update = new BasicBSONObject("$set", new BasicBSONObject("books", books));
    reduceResult.setQuery(query);
    reduceResult.setModifiers(update);
    output.collect(null, reduceResult);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicBSONObject(org.bson.BasicBSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) ArrayList(java.util.ArrayList)

Example 82 with BasicBSONObject

use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.

the class MongoRecordReaderTest method testGetCurrentKey.

@Test
public void testGetCurrentKey() throws Exception {
    MongoClient client = new MongoClient("localhost", 27017);
    MongoClientURI uri = new MongoClientURIBuilder().collection("mongo_hadoop", "mongo_record_reader_test").build();
    DBCollection collection = client.getDB(uri.getDatabase()).getCollection(uri.getCollection());
    collection.drop();
    BasicDBList colors = new BasicDBList() {

        {
            add(new BasicBSONObject("red", 255));
            add(new BasicBSONObject("blue", 255));
            add(new BasicBSONObject("green", 0));
        }
    };
    collection.insert(new BasicDBObject("_id", 0).append("address", new BasicDBObject("street", "foo street")).append("colors", colors));
    // Default case: "_id" is used as inputKey.
    MongoInputSplit split = new MongoInputSplit();
    split.setInputURI(uri);
    MongoRecordReader reader = new MongoRecordReader(split);
    assertTrue(reader.nextKeyValue());
    assertEquals(reader.getCurrentKey(), 0);
    // Use a nested field as inputKey.
    split = new MongoInputSplit();
    split.setInputURI(uri);
    split.setKeyField("address.street");
    reader = new MongoRecordReader(split);
    assertTrue(reader.nextKeyValue());
    assertEquals(reader.getCurrentKey(), "foo street");
    // Use a key within an array as the inputKey.
    split = new MongoInputSplit();
    split.setInputURI(uri);
    split.setKeyField("colors.1");
    reader = new MongoRecordReader(split);
    assertTrue(reader.nextKeyValue());
    assertEquals(reader.getCurrentKey(), new BasicBSONObject("blue", 255));
}
Also used : MongoClient(com.mongodb.MongoClient) DBCollection(com.mongodb.DBCollection) BasicDBList(com.mongodb.BasicDBList) BasicBSONObject(org.bson.BasicBSONObject) BasicDBObject(com.mongodb.BasicDBObject) MongoInputSplit(com.mongodb.hadoop.input.MongoInputSplit) MongoClientURIBuilder(com.mongodb.hadoop.util.MongoClientURIBuilder) MongoRecordReader(com.mongodb.hadoop.input.MongoRecordReader) MongoClientURI(com.mongodb.MongoClientURI) Test(org.junit.Test)

Example 83 with BasicBSONObject

use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.

the class MongoUpdateStorage method putNext.

@Override
public void putNext(final Tuple tuple) throws IOException {
    try {
        // perform substitution on variables "marked" for replacements
        BasicBSONObject[] toUpdate = pigReplace.substitute(tuple, schema, unnamedStr);
        // 'query' JSON
        BasicBSONObject q = toUpdate[0];
        // 'update' JSON
        BasicBSONObject u = toUpdate[1];
        // update options
        BasicBSONObject mu = toUpdate.length > 2 ? toUpdate[2] : null;
        boolean isUpsert = true;
        boolean isMulti = false;
        boolean isReplace = false;
        if (mu != null) {
            isUpsert = mu.getBoolean("upsert", true);
            isMulti = mu.getBoolean("multi", false);
            isReplace = mu.getBoolean("replace", false);
        }
        muw.setQuery(q);
        muw.setModifiers(u);
        muw.setUpsert(isUpsert);
        muw.setMultiUpdate(isMulti);
        muw.setReplace(isReplace);
        recordWriter.write(null, muw);
    } catch (Exception e) {
        throw new IOException("Couldn't convert tuple to bson: ", e);
    }
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) IOException(java.io.IOException) IOException(java.io.IOException)

Example 84 with BasicBSONObject

use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.

the class JSONPigReplaceTest method testSampleQueryUpdateReplace.

@Test
public void testSampleQueryUpdateReplace() throws Exception {
    // create tuple ("Daniel", "Alabi", 19, {("a"), ("b"), ("c")})
    // with schema 'f:chararray,l:chararray,age:int,cars:{t:(t:chararray)}'
    DataBag b = bagFactory.newDefaultBag();
    b.add(tupleFactory.newTuple("a"));
    b.add(tupleFactory.newTuple("b"));
    b.add(tupleFactory.newTuple("c"));
    Tuple t = tupleFactory.newTuple(4);
    t.set(0, "Daniel");
    t.set(1, "Alabi");
    t.set(2, 19);
    t.set(3, b);
    JSONPigReplace j = new JSONPigReplace(new String[] { "{first:'$f', last:'$l'}", "{$set: {age: '$age'}, $pushAll : {cars: '$cars'}}" });
    BasicBSONObject[] bs = j.substitute(t, "f:chararray,l:chararray,age:int,cars:{t:(t:chararray)}", "t");
    assertTrue(bs.length == 2);
    // should produce
    // { "first" : "Daniel" , "last" : "Alabi"}
    // { "$set" : { "age" : 19} , "$pushAll" : { "cars" : [ "a" , "b" , "c"]}}
    BasicBSONObject res1 = bs[0];
    BasicBSONObject res2 = bs[1];
    assertEquals(res1.get("first"), "Daniel");
    assertEquals(((BasicBSONObject) res2.get("$set")).get("age"), 19);
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) DataBag(org.apache.pig.data.DataBag) Tuple(org.apache.pig.data.Tuple) Test(org.junit.Test)

Example 85 with BasicBSONObject

use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.

the class JSONPigReplaceTest method testNonNestedReplace.

@Test
public void testNonNestedReplace() throws Exception {
    // create tuple ("Daniel", "Alabi")
    // with schema 'first:chararray, last:chararray'
    Tuple t = tupleFactory.newTuple(2);
    t.set(0, "Daniel");
    t.set(1, "Alabi");
    JSONPigReplace j = new JSONPigReplace(new String[] { "{first: '$first', last: '$last'}" });
    BasicBSONObject[] bs = j.substitute(t, "first:chararray, last:chararray", null);
    assertNotNull(bs);
    assertTrue(bs.length == 1);
    // should produce BSONObject
    // {first:"Daniel", last:"Alabi"}
    BasicBSONObject res = bs[0];
    assertEquals(res.get("first"), "Daniel");
    assertEquals(res.get("last"), "Alabi");
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) Tuple(org.apache.pig.data.Tuple) Test(org.junit.Test)

Aggregations

BasicBSONObject (org.bson.BasicBSONObject)88 Test (org.junit.Test)39 BSONObject (org.bson.BSONObject)37 ArrayList (java.util.ArrayList)15 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)14 BSONWritable (com.mongodb.hadoop.io.BSONWritable)13 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)13 MapObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)13 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)13 BasicDBObject (com.mongodb.BasicDBObject)11 ObjectId (org.bson.types.ObjectId)11 IOException (java.io.IOException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 DataBag (org.apache.pig.data.DataBag)6 Map (java.util.Map)5 Tuple (org.apache.pig.data.Tuple)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 Mongo (com.mongodb.Mongo)4 Date (java.util.Date)4 DoubleWritable (org.apache.hadoop.io.DoubleWritable)4