Example usage for com.mongodb Mongo Mongo

List of usage examples for com.mongodb Mongo Mongo

Introduction

In this page you can find the example usage for com.mongodb Mongo Mongo.

Prototype

Mongo(final MongoClientURI mongoURI) 

Source Link

Usage

From source file:org.tuwien.pdfprocessor.configuration.MongoConfiguraiton.java

@Override
public Mongo mongo() throws Exception {
    return new Mongo("localhost:27017");
}

From source file:org.unitedid.shibboleth.attribute.resolver.provider.dataConnector.MongoDbDataConnector.java

License:Apache License

/**
 * Creates the mongo database connection
 *//*from   w  w w  .  java  2  s . c  o m*/
protected void initializeMongoDbConnection() {
    if (initialized) {
        log.debug("MongoDB connector initializing!");
        Mongo mongoCon = new Mongo(mongoHost);
        mongoCon.setReadPreference(getPreferredRead());
        db = mongoCon.getDB(mongoDbName);

        if (getMongoUser() != null && getMongoPassword() != null) {
            boolean dbAuth = db.authenticate(getMongoUser(), getMongoPassword().toCharArray());
            if (!dbAuth) {
                log.error(
                        "MongoDB data connector {} authentication failed for database {}, username or password!",
                        getId(), mongoDbName);
                throw new MongoException("MongoDB data connector " + getId() + " authentication failed!");
            } else {
                log.debug("MongoDB data connector {} authentication successful!", getId());
            }
        }
    }
}

From source file:org.unitedid.shibboleth.attribute.resolver.provider.dataConnector.MongoDbDataConnector.java

License:Apache License

/** {@inheritDoc} */
public void validate() throws AttributeResolutionException {
    log.debug("Validating data connector {} configuration.", getId());
    Mongo connection = null;//from   w w  w  . j a v a  2s.c  o  m
    try {
        connection = new Mongo(mongoHost);
        if (connection == null) {
            log.error("Unable to create connections using {} data connector ", getId());
            throw new AttributeResolutionException(
                    "Unable to create connections using " + getId() + " data connector.");
        }
    } catch (MongoException e) {
        log.error("Unable to validate {} data connector", getId(), e);
        throw new AttributeResolutionException("Unable to validate " + getId() + " data connector: ", e);
    } finally {
        connection.close();
    }
}

From source file:others.Capped.java

License:Apache License

public static void main(String[] args) throws MongoException, UnknownHostException {

    MongoURI uri = new MongoURI("mongodb://10.11.0.52:27017/test");
    DB db = new Mongo(uri).getDB("test");
    DBObject foo = new BasicDBObject();
    foo.put("create", "capped1");
    foo.put("capped", true);
    foo.put("size", 100000000);
    DBObject dbobj = db.command(foo);//from w  w w .  j  av a 2s. c  o m
    DBCollection c = db.getCollection("capped1");

    DBObject obj = new BasicDBObject();
    Date begin = new Date();
    for (int i = 1; i <= 1000000; i++) {
        obj.removeField("x");
        obj.put("x", i);
        c.insert(obj);
    }
    Date end = new Date();
    System.out.println("One by one:" + ((end.getTime() - begin.getTime()) / 1000));

    DBObject foo2 = new BasicDBObject();
    foo.put("create", "capped2");
    foo.put("capped", true);
    foo.put("size", 100000000);
    DBObject dbobj2 = db.command(foo);
    DBCollection c2 = db.getCollection("capped2");

    begin = new Date();
    for (int i = 1; i <= 1000; i++) {
        List<DBObject> list = new ArrayList<DBObject>(1000);
        for (int j = 1; j <= 1000; j++) {
            DBObject dbo = new BasicDBObject();
            dbo.put("x", j + (i - 1) * 1000);
            list.add(dbo);
        }
        c2.insert(list);
    }
    end = new Date();
    System.out.println("Batch(per 1000):" + ((end.getTime() - begin.getTime()) / 1000));
}

From source file:ru.frostman.web.MongoPlugin.java

License:Apache License

@Override
public boolean reload() {
    if (firstLoad) {
        MorphiaLoggerFactory.registerLogger(SLF4JLogrImplFactory.class);
        firstLoad = false;/*from   w ww  . jav a2  s . c  om*/
    }

    boolean changed = MongoConfig.update();

    if (changed || mongo == null || morphia == null) {
        try {
            mongo = new Mongo(MongoConfig.get().getMongoReplicaSet());

            String mongoVersion = mongo.getVersion();
            log.debug("Successfully connected to MongoDB v." + mongoVersion);

            morphia = new Morphia();
        } catch (UnknownHostException e) {
            throw new JavinRuntimeException("Can't connect to MongoDB", e);
        }
        log.info("Mongo plugin loaded successfully");
    }

    return changed;
}

From source file:se.inera.axel.riv.impl.MongoDBTestContextConfig.java

License:Open Source License

public @Bean(destroyMethod = "close") Mongo mongo() throws Exception {
    MongodProcess mongodProcess = mongodProcess();

    return new Mongo(new ServerAddress(mongodProcess.getConfig().net().getServerAddress(),
            mongodProcess.getConfig().net().getPort()));
}

From source file:Server.Service.java

private void DBConnect() {
    try {//from  ww  w. j  a  v a 2  s . c o  m
        mongo = new Mongo(new ServerAddress("", 27017));
        db = mongo.getDB("bank");
    } catch (UnknownHostException | MongoException e) {
        this.DBShutdown();
    }
}

From source file:things.config.mongo.MongoConfig.java

License:Open Source License

@Override
public Mongo mongo() throws Exception {
    return new Mongo("localhost");
}

From source file:TweetAnalytics.DBManager.java

public DBManager(String conStr) {

    // create the database
    try {/*from www  .  j a va  2  s .  c  o m*/
        // make the initial connection to the mongoDB
        @SuppressWarnings("deprecation")
        Mongo tweetsMongoClient = new Mongo(new MongoURI(conStr));
        db = tweetsMongoClient.getDB("twitter_mini");
    } catch (UnknownHostException ex) {
        System.err.println("The database could not be initialized because of an UnknownHostException.");
        Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:TweetCollector.DBManager.java

@SuppressWarnings("deprecation")
public DBManager(String conStr) {

    // create the database
    try {// w  ww  . j ava 2s  .  c  om
        // make the initial connection to the mongoDB
        Mongo tweetsMongoClient = new Mongo(new MongoURI(conStr));
        DBManager.db = tweetsMongoClient.getDB("twitter_mini");
    } catch (UnknownHostException ex) {
        System.err.println("The database could not be initialized because of an UnknownHostException.");
        Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
    }

    // create the tweets collection
    DBManager.tweetsCollection = DBManager.db.getCollection("tweets");
    // tweetsCollection.ensureIndex(new BasicDBObject("text", "text"));

    // create the trends collection
    DBManager.trendsCollection = DBManager.db.getCollection("trends");
}