Search in sources :

Example 26 with BasicBSONObject

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

the class JSONPigReplaceTest method testSimpleMultipleReplace.

@Test
public void testSimpleMultipleReplace() throws Exception {
    // create tuple ({("Daniel", "Alabi")}, "Carleton College")
    // with schema 'b:{b:(f:chararray,l:chararray)}, s:chararray'
    Tuple t1 = tupleFactory.newTuple(2);
    t1.set(0, "Daniel");
    t1.set(1, "Alabi");
    DataBag b = bagFactory.newDefaultBag();
    b.add(t1);
    Tuple t = tupleFactory.newTuple(2);
    t.set(0, b);
    t.set(1, "Carleton College");
    JSONPigReplace j = new JSONPigReplace(new String[] { "{first:'$f', last:'$l', school:'$s'}", "{$push : {schools: '$s'}}" });
    BasicBSONObject[] bs = j.substitute(t, "b:{t:(f:chararray,l:chararray)}, s:chararray", null);
    assertNotNull(bs);
    assertTrue(bs.length == 2);
    // should produce
    // { "first" : "Daniel" , "last" : "Alabi" , "school" : "Carleton College"}
    // and
    // { "$push" : { "schools" : "Carleton College"}}
    BasicBSONObject res1 = bs[0];
    BasicBSONObject res2 = bs[1];
    assertEquals(res1.get("first"), "Daniel");
    assertEquals(res1.get("last"), "Alabi");
    assertEquals(((BasicBSONObject) res2.get("$push")).get("schools"), "Carleton College");
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) DataBag(org.apache.pig.data.DataBag) Tuple(org.apache.pig.data.Tuple) Test(org.junit.Test)

Example 27 with BasicBSONObject

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

the class JSONPigReplaceTest method testNamedArrayReplace.

@Test
public void testNamedArrayReplace() throws Exception {
    // create tuple ({("a"), ("b"), ("c")})
    // with schema 'cars:{f:(t:chararray)}'
    DataBag b = bagFactory.newDefaultBag();
    b.add(tupleFactory.newTuple("a"));
    b.add(tupleFactory.newTuple("b"));
    b.add(tupleFactory.newTuple("c"));
    JSONPigReplace j = new JSONPigReplace(new String[] { "{days : [1,2,3], age : 19, cars : '$cars'}" });
    BasicBSONObject[] bs = j.substitute(tupleFactory.newTuple(b), "cars : {f:(t:chararray)}", null);
    assertNotNull(bs);
    assertTrue(bs.length == 1);
    // should produce BSONObject
    // { "days" : [ 1 , 2 , 3] , "age" : 19 , "cars" : [ { "t" : "a"} , { "t" : "b"} , { "t" : "c"}]}
    BasicBSONObject res = bs[0];
    ArrayList cars = (ArrayList) res.get("cars");
    assertEquals(cars.size(), 3);
    Object o = cars.get(0);
    assertEquals(((Map) o).get("t"), "a");
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) DataBag(org.apache.pig.data.DataBag) ArrayList(java.util.ArrayList) BasicBSONObject(org.bson.BasicBSONObject) Test(org.junit.Test)

Example 28 with BasicBSONObject

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

the class TagsReducer method reduce.

@Override
protected void reduce(final Text key, final Iterable<BSONWritable> values, final Context context) throws IOException, InterruptedException {
    BasicDBObject query = new BasicDBObject("_id", key.toString());
    ArrayList<BSONObject> books = new ArrayList<BSONObject>();
    for (BSONWritable val : values) {
        books.add(val.getDoc());
    }
    BasicBSONObject update = new BasicBSONObject("$set", new BasicBSONObject("books", books));
    reduceResult.setQuery(query);
    reduceResult.setModifiers(update);
    context.write(null, reduceResult);
}
Also used : BSONWritable(com.mongodb.hadoop.io.BSONWritable) BasicDBObject(com.mongodb.BasicDBObject) BasicBSONObject(org.bson.BasicBSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) ArrayList(java.util.ArrayList)

Example 29 with BasicBSONObject

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

the class BSONSplitterTest method testCreateFileSplitFromBSON.

@Test
public void testCreateFileSplitFromBSON() throws IOException {
    BSONObject splitSpec = new BasicBSONObject();
    splitSpec.put("s", 0L);
    splitSpec.put("l", file.getLen());
    BSONFileSplit splitResult = SPLITTER.createFileSplitFromBSON(splitSpec, fs, file);
    assertOneSplit(splitResult);
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) BSONFileSplit(com.mongodb.hadoop.input.BSONFileSplit) Test(org.junit.Test)

Example 30 with BasicBSONObject

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

the class BSONComparator method compareValues.

/**
 * @param one, two - two objects with the same type Cast the two objects and compare the values
 */
private int compareValues(final Object one, final Object two) {
    int diff = 0;
    if (one instanceof Number) {
        // Need to be comparing all numeric values to one another,
        // so cast all of them to Double
        diff = Double.valueOf(one.toString()).compareTo(Double.valueOf(two.toString()));
    } else if (one instanceof String) {
        diff = ((String) one).compareTo((String) two);
    } else if (one instanceof BSONObject) {
        // BasicBSONObject and BasicBSONList both covered in this cast
        diff = compare((BSONObject) one, (BSONObject) two);
    } else if (one instanceof Binary) {
        ByteBuffer buff1 = ByteBuffer.wrap(((Binary) one).getData());
        ByteBuffer buff2 = ByteBuffer.wrap(((Binary) two).getData());
        diff = buff1.compareTo(buff2);
    } else if (one instanceof byte[]) {
        ByteBuffer buff1 = ByteBuffer.wrap((byte[]) one);
        ByteBuffer buff2 = ByteBuffer.wrap((byte[]) two);
        diff = buff1.compareTo(buff2);
    } else if (one instanceof ObjectId) {
        diff = ((ObjectId) one).compareTo((ObjectId) two);
    } else if (one instanceof Boolean) {
        diff = ((Boolean) one).compareTo((Boolean) two);
    } else if (one instanceof Date) {
        diff = ((Date) one).compareTo((Date) two);
    } else if (one instanceof BSONTimestamp) {
        diff = ((BSONTimestamp) one).compareTo((BSONTimestamp) two);
    }
    return diff;
}
Also used : ObjectId(org.bson.types.ObjectId) LazyBSONObject(org.bson.LazyBSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) BSONTimestamp(org.bson.types.BSONTimestamp) Binary(org.bson.types.Binary) ByteBuffer(java.nio.ByteBuffer) Date(java.util.Date)

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