Search in sources :

Example 6 with BasicDBList

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

the class MongoLoaderTest method testSimpleBag.

@Test
public void testSimpleBag() throws IOException {
    String userSchema = "b:{t:tuple(t1:chararray, t2:chararray)}";
    BasicDBList bag = new BasicDBList();
    bag.add(new BasicDBObject().append("t1", "t11_value").append("t2", "t12_value"));
    bag.add(new BasicDBObject().append("t1", "t21_value").append("t2", "t22_value"));
    MongoLoader ml = new MongoLoader(userSchema);
    Object result = BSONLoader.readField(bag, ml.getFields()[0]);
    DataBag b = (DataBag) result;
    Iterator<Tuple> bit = b.iterator();
    Tuple firstInnerT = bit.next();
    assertEquals(2, firstInnerT.size());
    assertEquals("t11_value", firstInnerT.get(0));
    assertEquals("t12_value", firstInnerT.get(1));
    Tuple secondInnerT = bit.next();
    assertEquals(2, secondInnerT.size());
    assertEquals("t21_value", secondInnerT.get(0));
    assertEquals("t22_value", secondInnerT.get(1));
    assertFalse(bit.hasNext());
}
Also used : BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) DataBag(org.apache.pig.data.DataBag) BasicDBObject(com.mongodb.BasicDBObject) Tuple(org.apache.pig.data.Tuple) Test(org.junit.Test)

Example 7 with BasicDBList

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

the class Mapper method toMongoObject.

/**
     * Converts a java object to a mongo-compatible object (possibly a DBObject for complex mappings).  Very similar to {@link
     * Mapper#toDBObject}.  Used (mainly) by query/update operations.
     *
     * @param mf    the MappedField for this value
     * @param mc    the MappedClass for this value
     * @param value the value to convert
     * @return the MongoDB compatible object
     */
@SuppressWarnings("deprecation")
public Object toMongoObject(final MappedField mf, final MappedClass mc, final Object value) {
    if (value == null) {
        return null;
    }
    Object mappedValue = value;
    if (value instanceof Query) {
        mappedValue = ((QueryImpl) value).getQueryObject();
    } else if (isAssignable(mf, value) || isEntity(mc)) {
        //convert the value to Key (DBRef) if the field is @Reference or type is Key/DBRef, or if the destination class is an @Entity
        try {
            if (value instanceof Iterable) {
                MappedClass mapped = getMappedClass(mf.getSubClass());
                if (mapped != null && (Key.class.isAssignableFrom(mapped.getClazz()) || mapped.getEntityAnnotation() != null)) {
                    mappedValue = getDBRefs(mf, (Iterable) value);
                } else {
                    if (mf.hasAnnotation(Reference.class)) {
                        mappedValue = getDBRefs(mf, (Iterable) value);
                    } else {
                        mappedValue = toMongoObject(value, false);
                    }
                }
            } else {
                if (mf != null) {
                    Reference refAnn = mf.getAnnotation(Reference.class);
                    Class<?> idType = null;
                    if (!mf.getType().equals(Key.class) && isMapped(mf.getType())) {
                        idType = getMappedClass(mf.getType()).getMappedIdField().getType();
                    }
                    boolean valueIsIdType = mappedValue.getClass().equals(idType);
                    if (refAnn != null) {
                        if (!valueIsIdType) {
                            Key<?> key = value instanceof Key ? (Key<?>) value : getKey(value);
                            if (key != null) {
                                mappedValue = refAnn.idOnly() ? keyToId(key) : keyToDBRef(key);
                            }
                        }
                    } else if (mf.getType().equals(Key.class)) {
                        mappedValue = keyToDBRef(valueIsIdType ? createKey(mf.getSubClass(), value) : value instanceof Key ? (Key<?>) value : getKey(value));
                        if (mappedValue == value) {
                            throw new ValidationException("cannot map to Key<T> field: " + value);
                        }
                    }
                }
                if (mappedValue == value) {
                    mappedValue = toMongoObject(value, false);
                }
            }
        } catch (Exception e) {
            LOG.error("Error converting value(" + value + ") to reference.", e);
            mappedValue = toMongoObject(value, false);
        }
    } else if (mf != null && mf.hasAnnotation(Serialized.class)) {
        //serialized
        try {
            mappedValue = Serializer.serialize(value, !mf.getAnnotation(Serialized.class).disableCompression());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } else if (value instanceof DBObject) {
        //pass-through
        mappedValue = value;
    } else {
        mappedValue = toMongoObject(value, EmbeddedMapper.shouldSaveClassName(value, mappedValue, mf));
        if (mappedValue instanceof BasicDBList) {
            final BasicDBList list = (BasicDBList) mappedValue;
            if (list.size() != 0) {
                if (!EmbeddedMapper.shouldSaveClassName(extractFirstElement(value), list.get(0), mf)) {
                    for (Object o : list) {
                        if (o instanceof DBObject) {
                            ((DBObject) o).removeField(CLASS_NAME_FIELDNAME);
                        }
                    }
                }
            }
        } else if (mappedValue instanceof DBObject && !EmbeddedMapper.shouldSaveClassName(value, mappedValue, mf)) {
            ((DBObject) mappedValue).removeField(CLASS_NAME_FIELDNAME);
        }
    }
    return mappedValue;
}
Also used : ValidationException(org.mongodb.morphia.query.ValidationException) Query(org.mongodb.morphia.query.Query) ProxiedEntityReference(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReference) Reference(org.mongodb.morphia.annotations.Reference) Serialized(org.mongodb.morphia.annotations.Serialized) IOException(java.io.IOException) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ValidationException(org.mongodb.morphia.query.ValidationException) IOException(java.io.IOException) BasicDBList(com.mongodb.BasicDBList) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ReflectionUtils.getParameterizedClass(org.mongodb.morphia.utils.ReflectionUtils.getParameterizedClass) Key(org.mongodb.morphia.Key)

Example 8 with BasicDBList

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

the class MappedFieldTest method nestedCollectionsMapping.

@Test
public void nestedCollectionsMapping() {
    final MappedField field = new MappedField(getField(TestEntity.class, "listOfListOfString"), TestEntity.class, getMorphia().getMapper());
    Assert.assertFalse(field.isSingleValue());
    Assert.assertTrue(field.isMultipleValues());
    Assert.assertFalse(field.isArray());
    Assert.assertTrue(List.class == field.getType());
    final List<MappedField> level1Types = field.getTypeParameters();
    final MappedField typeParameter = level1Types.get(0);
    Assert.assertTrue(List.class == typeParameter.getConcreteType());
    final List<MappedField> level2Types = typeParameter.getTypeParameters();
    final MappedField nested = level2Types.get(0);
    Assert.assertTrue(String.class == nested.getConcreteType());
    Assert.assertEquals("listOfListOfString", field.getJavaFieldName());
    Assert.assertEquals("listOfListOfString", field.getNameToStore());
    final BasicDBList list = new BasicDBList();
    list.add(dbList("a", "b", "c"));
    list.add(dbList("d", "e", "f"));
    final TestEntity entity = getMorphia().getMapper().fromDb(getDs(), new BasicDBObject("listOfListOfString", list), new TestEntity(), new DefaultEntityCache());
    final List<String> strings = asList("a", "b", "c");
    final List<String> strings1 = asList("d", "e", "f");
    final List<List<String>> expected = new ArrayList<List<String>>();
    expected.add(strings);
    expected.add(strings1);
    Assert.assertEquals(expected, entity.listOfListOfString);
}
Also used : BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) ArrayList(java.util.ArrayList) BasicDBList(com.mongodb.BasicDBList) ArrayList(java.util.ArrayList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) Test(org.junit.Test)

Example 9 with BasicDBList

use of com.mongodb.BasicDBList in project spring-data-document-examples by spring-projects.

the class ChartController method getFavoritesData.

private DefaultCategoryDataset getFavoritesData() {
    MongoTemplate mongoTemplate;
    DefaultCategoryDataset ds = null;
    try {
        Mongo m = new Mongo();
        mongoTemplate = new MongoTemplate(m, "mvc");
        DBObject result = getTopRecommendedRestaurants(mongoTemplate);
        /* Example data.
			 * [ { "parameters.p1" : "1" , "count" : 5.0} , 
			 *   { "parameters.p1" : "2" , "count" : 6.0} , 
			 *   { "parameters.p1" : "3" , "count" : 3.0} , 
			 *   { "parameters.p1" : "4" , "count" : 8.0}]
			 */
        ds = new DefaultCategoryDataset();
        if (result instanceof BasicDBList) {
            BasicDBList dbList = (BasicDBList) result;
            for (Iterator iterator = dbList.iterator(); iterator.hasNext(); ) {
                DBObject dbo = (DBObject) iterator.next();
                System.out.println(dbo);
                Restaurant r = restaurantDao.findRestaurant(Long.parseLong(dbo.get("parameters.p1").toString()));
                ds.addValue(Double.parseDouble(dbo.get("count").toString()), "recommended", r.getName());
            }
        }
        return ds;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return ds;
}
Also used : BasicDBList(com.mongodb.BasicDBList) Restaurant(com.springone.myrestaurants.domain.Restaurant) Mongo(com.mongodb.Mongo) Iterator(java.util.Iterator) MongoTemplate(org.springframework.data.document.mongodb.MongoTemplate) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 10 with BasicDBList

use of com.mongodb.BasicDBList in project graylog2-server by Graylog2.

the class InputImpl method getStaticFields.

@Override
public Map<String, String> getStaticFields() {
    if (fields.get(EMBEDDED_STATIC_FIELDS) == null) {
        return Collections.emptyMap();
    }
    final BasicDBList list = (BasicDBList) fields.get(EMBEDDED_STATIC_FIELDS);
    final Map<String, String> staticFields = Maps.newHashMapWithExpectedSize(list.size());
    for (final Object element : list) {
        try {
            final DBObject field = (DBObject) element;
            staticFields.put((String) field.get(FIELD_STATIC_FIELD_KEY), (String) field.get(FIELD_STATIC_FIELD_VALUE));
        } catch (Exception e) {
            LOG.error("Cannot build static field from persisted data. Skipping.", e);
        }
    }
    return staticFields;
}
Also used : BasicDBList(com.mongodb.BasicDBList) DBObject(com.mongodb.DBObject) DBObject(com.mongodb.DBObject)

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