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");
}
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");
}
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);
}
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);
}
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;
}
Aggregations