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 BSONWritableTest method testToBSON.
@Test
public void testToBSON() {
assertEquals(null, toBSON(null));
assertEquals(null, toBSON(NullWritable.get()));
assertEquals("hello", toBSON(new Text("hello")));
DBObject obj = new BasicDBObject("hello", "world");
assertEquals(obj, toBSON(new BSONWritable(obj)));
final BasicBSONObject bsonResult = new BasicBSONObject("one", 1);
SortedMapWritable smw = new SortedMapWritable();
smw.put(new Text("one"), new IntWritable(1));
assertEquals(bsonResult, toBSON(smw));
MapWritable mw = new MapWritable();
mw.put(new Text("one"), new IntWritable(1));
assertEquals(bsonResult, toBSON(mw));
String[] expectedObjects = new String[] { "one", "two" };
Writable[] writableObjects = new Writable[] { new Text("one"), new Text("two") };
ArrayWritable aw = new ArrayWritable(Text.class, writableObjects);
Object[] actual = (Object[]) toBSON(aw);
assertTrue(Arrays.equals(expectedObjects, actual));
assertEquals(false, toBSON(new BooleanWritable(false)));
byte[] bytes = new byte[] { '0', '1', '2' };
assertEquals(bytes, toBSON(new BytesWritable(bytes)));
byte b = (byte) 'c';
assertEquals(b, toBSON(new ByteWritable(b)));
assertEquals(3.14159, toBSON(new DoubleWritable(3.14159)));
assertEquals(3.14159f, toBSON(new FloatWritable(3.14159f)));
assertEquals(42L, toBSON(new LongWritable(42L)));
assertEquals(42, toBSON(new IntWritable(42)));
// Catchall
assertEquals("hi", toBSON("hi"));
}
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 TreasuryYieldReducer method reduce.
@Override
public void reduce(final IntWritable pKey, final Iterable<DoubleWritable> pValues, final Context pContext) throws IOException, InterruptedException {
int count = 0;
double sum = 0;
for (final DoubleWritable value : pValues) {
sum += value.get();
count++;
}
final double avg = sum / count;
if (LOG.isDebugEnabled()) {
LOG.debug("Average 10 Year Treasury for " + pKey.get() + " was " + avg);
}
BasicBSONObject output = new BasicBSONObject();
output.put("count", count);
output.put("avg", avg);
output.put("sum", sum);
reduceResult.setDoc(output);
pContext.write(pKey, reduceResult);
}
use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.
the class TreasuryYieldReducer method reduce.
@Override
public void reduce(final IntWritable key, final Iterator<DoubleWritable> values, final OutputCollector<IntWritable, BSONWritable> output, final Reporter reporter) throws IOException {
int count = 0;
double sum = 0;
while (values.hasNext()) {
sum += values.next().get();
count++;
}
final double avg = sum / count;
if (LOG.isDebugEnabled()) {
LOG.debug("Average 10 Year Treasury for " + key.get() + " was " + avg);
}
BasicBSONObject bsonObject = new BasicBSONObject();
bsonObject.put("count", count);
bsonObject.put("avg", avg);
bsonObject.put("sum", sum);
reduceResult.setDoc(bsonObject);
output.collect(key, reduceResult);
}
Aggregations