Search in sources :

Example 6 with ObjectId

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

the class DeviceReducer method reduce.

@Override
public void reduce(final Text pKey, final Iterable<Text> pValues, final Context pContext) throws IOException, InterruptedException {
    BasicBSONObject query = new BasicBSONObject("_id", pKey.toString());
    ArrayList<ObjectId> devices = new ArrayList<ObjectId>();
    for (Text val : pValues) {
        devices.add(new ObjectId(val.toString()));
    }
    BasicBSONObject update = new BasicBSONObject("$pushAll", new BasicBSONObject("devices", devices));
    reduceResult.setQuery(query);
    reduceResult.setModifiers(update);
    pContext.write(null, reduceResult);
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) ObjectId(org.bson.types.ObjectId) ArrayList(java.util.ArrayList) Text(org.apache.hadoop.io.Text)

Example 7 with ObjectId

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

the class LogReducer method reduce.

@Override
public void reduce(final Text pKey, final Iterable<IntWritable> pValues, final Context pContext) throws IOException, InterruptedException {
    int count = 0;
    for (IntWritable val : pValues) {
        count += val.get();
    }
    BasicBSONObject query = new BasicBSONObject("devices", new ObjectId(pKey.toString()));
    BasicBSONObject update = new BasicBSONObject("$inc", new BasicBSONObject("logs_count", count));
    if (LOG.isDebugEnabled()) {
        LOG.debug("query: " + query);
        LOG.debug("update: " + update);
    }
    reduceResult.setQuery(query);
    reduceResult.setModifiers(update);
    pContext.write(null, reduceResult);
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) ObjectId(org.bson.types.ObjectId) IntWritable(org.apache.hadoop.io.IntWritable)

Example 8 with ObjectId

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

the class BSONLoader method convertBSONtoPigType.

/**
     * Convert an object from a MongoDB document into a type that Pig can
     * understand, based on the type of the input object.
     * @param o object from a MongoDB document
     * @return object appropriate for pig
     * @throws ExecException for lower-level Pig errors
     */
public static Object convertBSONtoPigType(final Object o) throws ExecException {
    if (o == null) {
        return null;
    } else if (o instanceof Number || o instanceof String) {
        return o;
    } else if (o instanceof Date) {
        return ((Date) o).getTime();
    } else if (o instanceof ObjectId) {
        return o.toString();
    } else if (o instanceof UUID) {
        return o.toString();
    } else if (o instanceof BasicBSONList) {
        BasicBSONList bl = (BasicBSONList) o;
        Tuple t = tupleFactory.newTuple(bl.size());
        for (int i = 0; i < bl.size(); i++) {
            t.set(i, convertBSONtoPigType(bl.get(i)));
        }
        return t;
    } else if (o instanceof Map) {
        //TODO make this more efficient for lazy objects?
        Map<String, Object> fieldsMap = (Map<String, Object>) o;
        HashMap<String, Object> pigMap = new HashMap<String, Object>(fieldsMap.size());
        for (Map.Entry<String, Object> field : fieldsMap.entrySet()) {
            pigMap.put(field.getKey(), convertBSONtoPigType(field.getValue()));
        }
        return pigMap;
    } else if (o instanceof byte[]) {
        return new DataByteArray((byte[]) o);
    } else if (o instanceof Binary) {
        return new DataByteArray(((Binary) o).getData());
    } else if (o instanceof DBRef) {
        HashMap<String, String> pigMap = new HashMap<String, String>(2);
        pigMap.put("$ref", ((DBRef) o).getCollectionName());
        pigMap.put("$id", ((DBRef) o).getId().toString());
        return pigMap;
    } else {
        return o;
    }
}
Also used : ObjectId(org.bson.types.ObjectId) HashMap(java.util.HashMap) BasicBSONList(org.bson.types.BasicBSONList) DBRef(com.mongodb.DBRef) Date(java.util.Date) BasicBSONObject(org.bson.BasicBSONObject) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) Binary(org.bson.types.Binary) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple)

Example 9 with ObjectId

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

the class UDFTest method setUpClass.

@BeforeClass
public static void setUpClass() {
    INPUT_COLLECTION.drop();
    insertedDocuments = new ArrayList<Document>(100);
    for (int i = 0; i < 100; ++i) {
        ObjectId id = new ObjectId();
        insertedDocuments.add(new Document("_id", id).append("minkey", new MinKey()).append("maxkey", new MaxKey()).append("dbref", new DBRef("othercollection", new ObjectId())).append("binary", new Binary(new byte[] { 1, 2, 3, 4, 5 })).append("oidBytes", new Binary(id.toByteArray())));
    }
    INPUT_COLLECTION.insertMany(insertedDocuments);
}
Also used : MinKey(org.bson.types.MinKey) ObjectId(org.bson.types.ObjectId) MaxKey(org.bson.types.MaxKey) DBRef(com.mongodb.DBRef) Binary(org.bson.types.Binary) Document(org.bson.Document) BeforeClass(org.junit.BeforeClass)

Example 10 with ObjectId

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

the class UDFTest method testUDFsSchemaless.

@Test
public void testUDFsSchemaless() throws IOException, ParseException {
    // Test that one of our UDFs can work without any schemas being
    // specified. This mostly tests that BSONStorage can infer the type
    // correctly.
    PigTest.runScript("/pig/udfschemaless.pig");
    assertEquals(insertedDocuments.size(), OUTPUT_COLLECTION.count());
    Iterator<Document> it = insertedDocuments.iterator();
    for (Document doc : OUTPUT_COLLECTION.find()) {
        // We don't know what Pig will call the fields that aren't "_id".
        ObjectId expectedId = it.next().getObjectId("_id");
        for (Map.Entry<String, Object> entry : doc.entrySet()) {
            // interested in.
            if ("_id".equals(entry.getKey())) {
                continue;
            }
            assertEquals(expectedId, entry.getValue());
        }
    }
}
Also used : ObjectId(org.bson.types.ObjectId) Document(org.bson.Document) Map(java.util.Map) Test(org.junit.Test)

Aggregations

ObjectId (org.bson.types.ObjectId)194 Test (org.junit.Test)113 BasicDBObject (com.mongodb.BasicDBObject)46 DBObject (com.mongodb.DBObject)28 Date (java.util.Date)23 Document (org.bson.Document)21 ArrayList (java.util.ArrayList)20 BsonObjectId (org.bson.BsonObjectId)16 StreamRuleMock (org.graylog2.streams.matchers.StreamRuleMock)14 Message (org.graylog2.plugin.Message)13 BasicBSONObject (org.bson.BasicBSONObject)12 List (java.util.List)11 Binary (org.bson.types.Binary)10 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)9 Map (java.util.Map)8 DBRef (com.mongodb.DBRef)7 Code (org.bson.types.Code)7 Stream (org.graylog2.plugin.streams.Stream)7 ImmutableList (com.google.common.collect.ImmutableList)6 BsonDocument (org.bson.BsonDocument)6