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