Search in sources :

Example 21 with BasicDBList

use of com.mongodb.BasicDBList in project mongo-hadoop by mongodb.

the class SensorDataGenerator method run.

public void run() throws UnknownHostException {
    final List<Integer> models = new ArrayList<Integer>();
    final List<String> owners = new ArrayList<String>();
    final MongoClient client = new MongoClient();
    DB db = client.getDB("mongo_hadoop");
    DBCollection devices = db.getCollection("devices");
    DBCollection logs = db.getCollection("logs");
    if ("true".equals(System.getenv("SENSOR_DROP"))) {
        LOG.info("Dropping sensor data");
        devices.drop();
        logs.drop();
        devices.createIndex(new BasicDBObject("devices", 1));
    }
    db.getCollection("logs_aggregate").createIndex(new BasicDBObject("devices", 1));
    if (logs.count() == 0) {
        for (int i = 0; i < 10; i++) {
            owners.add(getRandomString(10));
        }
        for (int i = 0; i < 10; i++) {
            models.add(getRandomInt(10, 20));
        }
        List<ObjectId> deviceIds = new ArrayList<ObjectId>();
        for (int i = 0; i < NUM_DEVICES; i++) {
            DBObject device = new BasicDBObject("_id", new ObjectId()).append("name", getRandomString(5) + getRandomInt(3, 5)).append("type", choose(TYPES)).append("owner", choose(owners)).append("model", choose(models)).append("created_at", randomDate(new Date(2000, 1, 1, 16, 49, 29), new Date()));
            deviceIds.add((ObjectId) device.get("_id"));
            devices.insert(device);
        }
        for (int i = 0; i < NUM_LOGS; i++) {
            if (i % 50000 == 0) {
                LOG.info(format("Creating %d sensor log data entries: %d%n", NUM_LOGS, i));
            }
            BasicDBList location = new BasicDBList();
            location.add(getRandomInRange(-180, 180, 3));
            location.add(getRandomInRange(-90, 90, 3));
            DBObject log = new BasicDBObject("_id", new ObjectId()).append("d_id", choose(deviceIds)).append("v", getRandomInt(0, 10000)).append("timestamp", randomDate(new Date(2013, 1, 1, 16, 49, 29), new Date())).append("loc", location);
            logs.insert(log);
        }
    }
}
Also used : ObjectId(org.bson.types.ObjectId) ArrayList(java.util.ArrayList) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) Date(java.util.Date) MongoClient(com.mongodb.MongoClient) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList) DB(com.mongodb.DB)

Example 22 with BasicDBList

use of com.mongodb.BasicDBList in project morphia by mongodb.

the class CustomConvertersTest method customerIteratorConverter.

@Test
public void customerIteratorConverter() {
    getMorphia().getMapper().getConverters().addConverter(ListToMapConvert.class);
    getMorphia().getMapper().getOptions().setStoreEmpties(false);
    getMorphia().getMapper().getOptions().setStoreNulls(false);
    getMorphia().map(EntityWithListsAndArrays.class);
    final EntityWithListsAndArrays entity = new EntityWithListsAndArrays();
    entity.setListOfStrings(Arrays.asList("string 1", "string 2", "string 3"));
    getDs().save(entity);
    final DBObject dbObject = getDs().getCollection(EntityWithListsAndArrays.class).findOne();
    Assert.assertFalse(dbObject.get("listOfStrings") instanceof BasicDBList);
    final EntityWithListsAndArrays loaded = getDs().find(EntityWithListsAndArrays.class).get();
    Assert.assertEquals(entity.getListOfStrings(), loaded.getListOfStrings());
}
Also used : BasicDBList(com.mongodb.BasicDBList) EntityWithListsAndArrays(org.mongodb.morphia.entities.EntityWithListsAndArrays) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 23 with BasicDBList

use of com.mongodb.BasicDBList in project mongo-hadoop by mongodb.

the class MongoLoaderTest method testDeepness.

@Test
public void testDeepness() throws IOException {
    String userSchema = "b:{t:tuple(t1:chararray, b:{t:tuple(i1:int, i2:int)})}";
    BasicDBList innerBag = new BasicDBList();
    innerBag.add(new BasicDBObject().append("i1", 1).append("i2", 2));
    innerBag.add(new BasicDBObject().append("i1", 3).append("i2", 4));
    BasicDBList bag = new BasicDBList();
    bag.add(new BasicDBObject().append("t1", "t1_value").append("b", innerBag));
    MongoLoader ml = new MongoLoader(userSchema);
    DataBag result = (DataBag) BSONLoader.readField(bag, ml.getFields()[0]);
    assertEquals(1, result.size());
    Iterator<Tuple> bit = result.iterator();
    Tuple t = bit.next();
    assertEquals(2, t.size());
    DataBag innerBagResult = (DataBag) t.get(1);
    assertEquals(2, innerBagResult.size());
    Iterator<Tuple> innerBit = innerBagResult.iterator();
    Tuple innerT = innerBit.next();
    assertEquals(2, innerT.get(1));
}
Also used : BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) DataBag(org.apache.pig.data.DataBag) Tuple(org.apache.pig.data.Tuple) Test(org.junit.Test)

Example 24 with BasicDBList

use of com.mongodb.BasicDBList in project morphia by mongodb.

the class EmbeddedMapper method readCollection.

@SuppressWarnings("unchecked")
private void readCollection(final Datastore datastore, final Mapper mapper, final Object entity, final EntityCache cache, final MappedField mf, final DBObject dbObject) {
    Collection values;
    final Object dbVal = mf.getDbObjectValue(dbObject);
    if (dbVal != null) {
        // multiple documents in a List
        values = mf.isSet() ? mapper.getOptions().getObjectFactory().createSet(mf) : mapper.getOptions().getObjectFactory().createList(mf);
        final List dbValues;
        if (dbVal instanceof List) {
            dbValues = (List) dbVal;
        } else {
            dbValues = new BasicDBList();
            dbValues.add(dbVal);
        }
        EphemeralMappedField ephemeralMappedField = !mapper.isMapped(mf.getType()) && isMapOrCollection(mf) && (mf.getSubType() instanceof ParameterizedType) ? new EphemeralMappedField((ParameterizedType) mf.getSubType(), mf, mapper) : null;
        for (final Object o : dbValues) {
            Object newEntity = null;
            if (o != null) {
                //run converters
                if (mapper.getConverters().hasSimpleValueConverter(mf) || mapper.getConverters().hasSimpleValueConverter(mf.getSubClass())) {
                    newEntity = mapper.getConverters().decode(mf.getSubClass(), o, mf);
                } else {
                    newEntity = readMapOrCollectionOrEntity(datastore, mapper, cache, mf, ephemeralMappedField, (DBObject) o);
                }
            }
            values.add(newEntity);
        }
        if (!values.isEmpty() || mapper.getOptions().isStoreEmpties()) {
            if (mf.getType().isArray()) {
                mf.setFieldValue(entity, ReflectionUtils.convertToArray(mf.getSubClass(), ReflectionUtils.iterToList(values)));
            } else {
                mf.setFieldValue(entity, values);
            }
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) BasicDBList(com.mongodb.BasicDBList) Collection(java.util.Collection) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBList(com.mongodb.BasicDBList) ArrayList(java.util.ArrayList) List(java.util.List) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 25 with BasicDBList

use of com.mongodb.BasicDBList in project morphia by mongodb.

the class Mapper method toMongoObject.

/**
     * <p> Converts a java object to a mongo-compatible object (possibly a DBObject for complex mappings). Very similar to {@link
     * Mapper#toDBObject} </p> <p> Used (mainly) by query/update operations </p>
     */
Object toMongoObject(final Object javaObj, final boolean includeClassName) {
    if (javaObj == null) {
        return null;
    }
    Class origClass = javaObj.getClass();
    if (origClass.isAnonymousClass() && origClass.getSuperclass().isEnum()) {
        origClass = origClass.getSuperclass();
    }
    final Object newObj = getConverters().encode(origClass, javaObj);
    if (newObj == null) {
        LOG.warning("converted " + javaObj + " to null");
        return null;
    }
    final Class type = newObj.getClass();
    final boolean bSameType = origClass.equals(type);
    //Even if the converter changed it, should it still be processed?
    if (!bSameType && !(Map.class.isAssignableFrom(type) || Iterable.class.isAssignableFrom(type))) {
        return newObj;
    } else {
        //The converter ran, and produced another type, or it is a list/map
        boolean isSingleValue = true;
        boolean isMap = false;
        Class subType = null;
        if (type.isArray() || Map.class.isAssignableFrom(type) || Iterable.class.isAssignableFrom(type)) {
            isSingleValue = false;
            isMap = implementsInterface(type, Map.class);
            // subtype of Long[], List<Long> is Long
            subType = (type.isArray()) ? type.getComponentType() : getParameterizedClass(type, (isMap) ? 1 : 0);
        }
        if (isSingleValue && !isPropertyType(type)) {
            final DBObject dbObj = toDBObject(newObj);
            if (!includeClassName) {
                dbObj.removeField(CLASS_NAME_FIELDNAME);
            }
            return dbObj;
        } else if (newObj instanceof DBObject) {
            return newObj;
        } else if (isMap) {
            if (isPropertyType(subType)) {
                return toDBObject(newObj);
            } else {
                final HashMap m = new HashMap();
                for (final Map.Entry e : (Iterable<Map.Entry>) ((Map) newObj).entrySet()) {
                    m.put(e.getKey(), toMongoObject(e.getValue(), includeClassName));
                }
                return m;
            }
        //Set/List but needs elements converted
        } else if (!isSingleValue && !isPropertyType(subType)) {
            final List<Object> values = new BasicDBList();
            if (type.isArray()) {
                for (final Object obj : (Object[]) newObj) {
                    values.add(toMongoObject(obj, includeClassName));
                }
            } else {
                for (final Object obj : (Iterable) newObj) {
                    values.add(toMongoObject(obj, includeClassName));
                }
            }
            return values;
        } else {
            return newObj;
        }
    }
}
Also used : BasicDBList(com.mongodb.BasicDBList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ReflectionUtils.getParameterizedClass(org.mongodb.morphia.utils.ReflectionUtils.getParameterizedClass) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Aggregations

BasicDBList (com.mongodb.BasicDBList)41 BasicDBObject (com.mongodb.BasicDBObject)33 DBObject (com.mongodb.DBObject)26 DBCollection (com.mongodb.DBCollection)8 Test (org.junit.Test)8 DBCursor (com.mongodb.DBCursor)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 NotFoundException (org.graylog2.database.NotFoundException)5 Map (java.util.Map)4 ImmutableList (com.google.common.collect.ImmutableList)3 MongoClient (com.mongodb.MongoClient)3 MongoException (com.mongodb.MongoException)3 MongoInputSplit (com.mongodb.hadoop.input.MongoInputSplit)3 List (java.util.List)3 DataBag (org.apache.pig.data.DataBag)3 Tuple (org.apache.pig.data.Tuple)3 ValidationException (org.graylog2.plugin.database.ValidationException)3 NoSuchInputTypeException (org.graylog2.shared.inputs.NoSuchInputTypeException)3 JSONObject (org.json.JSONObject)3