Search in sources :

Example 61 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) ArrayList(java.util.ArrayList) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject)

Example 62 with BasicBSONObject

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"));
}
Also used : NullWritable(org.apache.hadoop.io.NullWritable) SortedMapWritable(org.apache.hadoop.io.SortedMapWritable) Writable(org.apache.hadoop.io.Writable) MapWritable(org.apache.hadoop.io.MapWritable) DoubleWritable(org.apache.hadoop.io.DoubleWritable) LongWritable(org.apache.hadoop.io.LongWritable) BooleanWritable(org.apache.hadoop.io.BooleanWritable) ByteWritable(org.apache.hadoop.io.ByteWritable) BytesWritable(org.apache.hadoop.io.BytesWritable) ArrayWritable(org.apache.hadoop.io.ArrayWritable) FloatWritable(org.apache.hadoop.io.FloatWritable) IntWritable(org.apache.hadoop.io.IntWritable) Text(org.apache.hadoop.io.Text) BytesWritable(org.apache.hadoop.io.BytesWritable) DoubleWritable(org.apache.hadoop.io.DoubleWritable) SortedMapWritable(org.apache.hadoop.io.SortedMapWritable) SortedMapWritable(org.apache.hadoop.io.SortedMapWritable) MapWritable(org.apache.hadoop.io.MapWritable) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) BasicBSONObject(org.bson.BasicBSONObject) FloatWritable(org.apache.hadoop.io.FloatWritable) ArrayWritable(org.apache.hadoop.io.ArrayWritable) BooleanWritable(org.apache.hadoop.io.BooleanWritable) BasicBSONObject(org.bson.BasicBSONObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) LongWritable(org.apache.hadoop.io.LongWritable) ByteWritable(org.apache.hadoop.io.ByteWritable) IntWritable(org.apache.hadoop.io.IntWritable) Test(org.junit.Test)

Example 63 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 64 with BasicBSONObject

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);
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) DoubleWritable(org.apache.hadoop.io.DoubleWritable)

Example 65 with BasicBSONObject

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);
}
Also used : BasicBSONObject(org.bson.BasicBSONObject)

Aggregations

BasicBSONObject (org.bson.BasicBSONObject)71 Test (org.junit.Test)23 BSONObject (org.bson.BSONObject)20 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)14 BSONWritable (com.mongodb.hadoop.io.BSONWritable)13 ArrayList (java.util.ArrayList)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)7 DataBag (org.apache.pig.data.DataBag)6 Tuple (org.apache.pig.data.Tuple)5 Mongo (com.mongodb.Mongo)4 Date (java.util.Date)4 Map (java.util.Map)4 DoubleWritable (org.apache.hadoop.io.DoubleWritable)4 Text (org.apache.hadoop.io.Text)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3