Search in sources :

Example 96 with DBCollection

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

the class TestSharded method testShardedClusterWithGtLtQueryFormats.

@Test
public void testShardedClusterWithGtLtQueryFormats() {
    DBCollection collection = getMongos().getDB("mongo_hadoop").getCollection("yield_historical.out");
    collection.drop();
    MapReduceJob job = new MapReduceJob(TreasuryYieldXMLConfig.class.getName()).jar(JOBJAR_PATH).inputUris(getInputUri()).outputUri(getOutputUri()).param(SPLITS_USE_RANGEQUERY, "true");
    if (isHadoopV1()) {
        job.outputCommitter(MongoOutputCommitter.class);
    }
    job.execute(isRunTestInVm());
    compareResults(collection, getReference());
    collection.drop();
    job.param(INPUT_QUERY, "{\"_id\":{\"$gt\":{\"$date\":1182470400000}}}").inputUris(getInputUri()).execute(isRunTestInVm());
    // Make sure that this fails when rangequery is used with a query that conflicts
    assertEquals(0, collection.count());
}
Also used : DBCollection(com.mongodb.DBCollection) MapReduceJob(com.mongodb.hadoop.testutils.MapReduceJob) TreasuryYieldXMLConfig(com.mongodb.hadoop.examples.treasury.TreasuryYieldXMLConfig) Test(org.junit.Test)

Example 97 with DBCollection

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

the class TestMapping method testDbRefMapping.

@Test
public void testDbRefMapping() throws Exception {
    getMorphia().map(ContainsRef.class).map(Rectangle.class);
    final DBCollection stuff = getDb().getCollection("stuff");
    final DBCollection rectangles = getDb().getCollection("rectangles");
    assertTrue("'ne' field should not be persisted!", !getMorphia().getMapper().getMCMap().get(ContainsRef.class.getName()).containsJavaFieldName("ne"));
    final Rectangle r = new Rectangle(1, 1);
    final DBObject rDbObject = getMorphia().toDBObject(r);
    rDbObject.put("_ns", rectangles.getName());
    rectangles.save(rDbObject);
    final ContainsRef cRef = new ContainsRef();
    cRef.rect = new DBRef((String) rDbObject.get("_ns"), rDbObject.get("_id"));
    final DBObject cRefDbObject = getMorphia().toDBObject(cRef);
    stuff.save(cRefDbObject);
    final BasicDBObject cRefDbObjectLoaded = (BasicDBObject) stuff.findOne(BasicDBObjectBuilder.start("_id", cRefDbObject.get("_id")).get());
    final ContainsRef cRefLoaded = getMorphia().fromDBObject(getDs(), ContainsRef.class, cRefDbObjectLoaded, new DefaultEntityCache());
    assertNotNull(cRefLoaded);
    assertNotNull(cRefLoaded.rect);
    assertNotNull(cRefLoaded.rect.getId());
    assertNotNull(cRefLoaded.rect.getCollectionName());
    assertEquals(cRefLoaded.rect.getId(), cRef.rect.getId());
    assertEquals(cRefLoaded.rect.getCollectionName(), cRef.rect.getCollectionName());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) Rectangle(org.mongodb.morphia.testmodel.Rectangle) DBRef(com.mongodb.DBRef) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 98 with DBCollection

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

the class TestMapping method performBasicMappingTest.

private void performBasicMappingTest() {
    final DBCollection hotels = getDb().getCollection("hotels");
    final DBCollection agencies = getDb().getCollection("agencies");
    getMorphia().map(Hotel.class);
    getMorphia().map(TravelAgency.class);
    final Hotel borg = new Hotel();
    borg.setName("Hotel Borg");
    borg.setStars(4);
    borg.setTakesCreditCards(true);
    borg.setStartDate(new Date());
    borg.setType(Hotel.Type.LEISURE);
    borg.getTags().add("Swimming pool");
    borg.getTags().add("Room service");
    borg.setTemp("A temporary transient value");
    borg.getPhoneNumbers().add(new PhoneNumber(354, 5152233, PhoneNumber.Type.PHONE));
    borg.getPhoneNumbers().add(new PhoneNumber(354, 5152244, PhoneNumber.Type.FAX));
    final Address address = new Address();
    address.setStreet("Posthusstraeti 11");
    address.setPostCode("101");
    borg.setAddress(address);
    BasicDBObject hotelDbObj = (BasicDBObject) getMorphia().toDBObject(borg);
    assertTrue(!(((DBObject) ((List) hotelDbObj.get("phoneNumbers")).get(0)).containsField(Mapper.CLASS_NAME_FIELDNAME)));
    hotels.save(hotelDbObj);
    Hotel borgLoaded = getMorphia().fromDBObject(getDs(), Hotel.class, hotelDbObj, new DefaultEntityCache());
    assertEquals(borg.getName(), borgLoaded.getName());
    assertEquals(borg.getStars(), borgLoaded.getStars());
    assertEquals(borg.getStartDate(), borgLoaded.getStartDate());
    assertEquals(borg.getType(), borgLoaded.getType());
    assertEquals(borg.getAddress().getStreet(), borgLoaded.getAddress().getStreet());
    assertEquals(borg.getTags().size(), borgLoaded.getTags().size());
    assertEquals(borg.getTags(), borgLoaded.getTags());
    assertEquals(borg.getPhoneNumbers().size(), borgLoaded.getPhoneNumbers().size());
    assertEquals(borg.getPhoneNumbers().get(1), borgLoaded.getPhoneNumbers().get(1));
    assertNull(borgLoaded.getTemp());
    assertTrue(borgLoaded.getPhoneNumbers() instanceof Vector);
    assertNotNull(borgLoaded.getId());
    final TravelAgency agency = new TravelAgency();
    agency.setName("Lastminute.com");
    agency.getHotels().add(borgLoaded);
    final BasicDBObject agencyDbObj = (BasicDBObject) getMorphia().toDBObject(agency);
    agencies.save(agencyDbObj);
    final TravelAgency agencyLoaded = getMorphia().fromDBObject(getDs(), TravelAgency.class, agencies.findOne(new BasicDBObject(Mapper.ID_KEY, agencyDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
    assertEquals(agency.getName(), agencyLoaded.getName());
    assertEquals(1, agency.getHotels().size());
    assertEquals(agency.getHotels().get(0).getName(), borg.getName());
    // try clearing values
    borgLoaded.setAddress(null);
    borgLoaded.getPhoneNumbers().clear();
    borgLoaded.setName(null);
    hotelDbObj = (BasicDBObject) getMorphia().toDBObject(borgLoaded);
    hotels.save(hotelDbObj);
    hotelDbObj = (BasicDBObject) hotels.findOne(new BasicDBObject(Mapper.ID_KEY, hotelDbObj.get(Mapper.ID_KEY)));
    borgLoaded = getMorphia().fromDBObject(getDs(), Hotel.class, hotelDbObj, new DefaultEntityCache());
    assertNull(borgLoaded.getAddress());
    assertEquals(0, borgLoaded.getPhoneNumbers().size());
    assertNull(borgLoaded.getName());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) TravelAgency(org.mongodb.morphia.testmodel.TravelAgency) Address(org.mongodb.morphia.testmodel.Address) PhoneNumber(org.mongodb.morphia.testmodel.PhoneNumber) ArrayList(java.util.ArrayList) List(java.util.List) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) Vector(java.util.Vector) Hotel(org.mongodb.morphia.testmodel.Hotel) Date(java.util.Date)

Example 99 with DBCollection

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

the class TestExpireAfterSeconds method testIndexedField.

@Test
public void testIndexedField() {
    getMorphia().map(HasExpiryField.class);
    getDs().ensureIndexes();
    getDs().save(new HasExpiryField());
    final DB db = getDs().getDB();
    final DBCollection dbCollection = db.getCollection("HasExpiryField");
    final List<DBObject> indexes = dbCollection.getIndexInfo();
    Assert.assertNotNull(indexes);
    Assert.assertEquals(2, indexes.size());
    DBObject index = null;
    for (final DBObject candidateIndex : indexes) {
        if (candidateIndex.containsField("expireAfterSeconds")) {
            index = candidateIndex;
        }
    }
    Assert.assertNotNull(index);
    Assert.assertEquals(5, ((Number) index.get("expireAfterSeconds")).intValue());
}
Also used : DBCollection(com.mongodb.DBCollection) DBObject(com.mongodb.DBObject) DB(com.mongodb.DB) Test(org.junit.Test)

Example 100 with DBCollection

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

the class HiveMappingTest method nestedColumns.

@Test
public void nestedColumns() throws SQLException {
    DBCollection collection = getCollection("hive_addresses");
    collection.drop();
    dropTable("hive_addresses");
    collection.insert(user(1, "Jim", "Beam", "Clermont", "KY"));
    collection.insert(user(2, "Don", "Draper", "New York", "NY"));
    collection.insert(user(3, "John", "Elway", "Denver", "CO"));
    MongoClientURI uri = authCheck(new MongoClientURIBuilder().collection("mongo_hadoop", collection.getName())).build();
    Map<String, String> map = new HashMap<String, String>() {

        {
            put("id", "_id");
            put("firstName", "firstName");
            put("lastName", "lastName");
            put("place.municipality", "address.city");
            put("place.region", "address.state");
        }
    };
    execute(format("CREATE TABLE hive_addresses " + "(id INT, firstName STRING, lastName STRING, " + "place STRUCT<municipality:STRING, region:STRING>)\n" + "STORED BY '%s'\n" + "WITH SERDEPROPERTIES('mongo.columns.mapping'='%s')\n" + "TBLPROPERTIES ('mongo.uri'='%s')", MongoStorageHandler.class.getName(), JSON.serialize(map), uri));
    // Alias inner fields to avoid retrieving entire struct as a String.
    Results execute = query("SELECT place.municipality AS city, place.region AS state, firstname from hive_addresses");
    assertEquals("KY", execute.getRow(0).get("state"));
    assertEquals("Don", execute.getRow(1).get("firstname"));
    assertEquals("Denver", execute.getRow(2).get("city"));
}
Also used : DBCollection(com.mongodb.DBCollection) MongoClientURIBuilder(com.mongodb.hadoop.util.MongoClientURIBuilder) HashMap(java.util.HashMap) MongoClientURI(com.mongodb.MongoClientURI) Test(org.junit.Test)

Aggregations

DBCollection (com.mongodb.DBCollection)165 DBObject (com.mongodb.DBObject)90 BasicDBObject (com.mongodb.BasicDBObject)86 Test (org.junit.Test)69 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)29 DBCursor (com.mongodb.DBCursor)23 MongoException (com.mongodb.MongoException)22 DB (com.mongodb.DB)20 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)17 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)12 JSONObject (org.json.JSONObject)12 MongoClientURI (com.mongodb.MongoClientURI)11 QueryBuilder (com.mongodb.QueryBuilder)10 List (java.util.List)10 Map (java.util.Map)10 Stopwatch (com.google.common.base.Stopwatch)9 WriteResult (com.mongodb.WriteResult)9 HashMap (java.util.HashMap)9 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8