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, @Nullable final MongoDriverInformation mongoDriverInformation) 

Source Link

Usage

From source file:edu.lander.twitter.TwitStreaming.java

public void connectMongoDB() {
    try {/*from w  w w.j a va 2s  .  com*/
        mongo = new Mongo(getMongoDBURL(), getMongoDBPort());
        db = mongo.getDB(getMongoDBName());
        collection = db.getCollection(getMongoDBCollecion());
    } catch (MongoException e) {
        e.printStackTrace();
    }
}

From source file:essex.bigessexnew.MongoListener.java

public MongoListener(String host, String port, String db, String collection) {
    Mongo mongo = new Mongo(host, Integer.getInteger(port));
    this.collection = mongo.getDB(db).getCollection(collection);
}

From source file:essex.bigessexnew.OplogListener.java

public OplogListener(String host, String port, String db, String collection) {

    Mongo mongo = new Mongo(host, Integer.parseInt(port));
    this.collection = mongo.getDB(db).getCollection(collection);
}

From source file:essex.bigessexnew.OplogListener.java

private void performListenTask(DBCursor cur) {
    EssexDataHandler dh = new EssexDataHandler(Params.getProperty("essexHostPath"));
    Mongo mongo2 = new Mongo(Params.getProperty("host"), Integer.parseInt(Params.getProperty("port")));
    DBCollection realCollection = mongo2.getDB(Params.getProperty("shardedDB"))
            .getCollection(Params.getProperty("shardedCollection"));
    Runnable task = () -> {/* w  w  w . j  a v  a 2 s  .c o  m*/
        System.out.println("\tWaiting for events");
        while (cur.hasNext()) {
            DBObject obj = cur.next();
            System.out.println(obj.toString());
            JSONObject output = new JSONObject(JSON.serialize(obj));
            String id = output.getJSONObject("o").getJSONObject("_id").getString("$oid");
            String opType = output.getString("op");
            String content = retrieveContent(realCollection, id);
            dh.handle(content, opType);

        }
    };
    new Thread(task).start();
}

From source file:essex.bigessexnew.TwitterStreamHandler.java

public TwitterStreamHandler(String host, String port, String DBName, String collectionName) {
    Mongo mongoClient = new Mongo(host, Integer.parseInt(port));
    collection = mongoClient.getDB(DBName).getCollection(collectionName);
}

From source file:eu.europeana.uim.gui.cp.server.RetrievalServiceImpl.java

License:Open Source License

/**
 * Creates a new instance of this class.
 *//*from   w  w w  .  j a  va 2 s.c om*/
public RetrievalServiceImpl() {

    super();

    PORTAL_SINGLE_RECORD_URL = PropertyReader.getProperty(UimConfigurationProperty.PORTAL_URI);

    PORTAL_PREVIEW_URL = PropertyReader.getProperty(UimConfigurationProperty.TESTPORTAL_URI);

    try {

        IBindingFactory bfact = BindingDirectory.getFactory(RDF.class);

        uctx = bfact.createUnmarshallingContext();

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        db = dbf.newDocumentBuilder();

        BlockingInitializer initializer = new BlockingInitializer() {

            @Override
            protected void initializeInternal() {
                solrServer = new HttpSolrServer(
                        PropertyReader.getProperty(UimConfigurationProperty.SOLR_HOSTURL)
                                + PropertyReader.getProperty(UimConfigurationProperty.SOLR_CORE));

            }
        };
        initializer.initialize(HttpSolrServer.class.getClassLoader());

        BlockingInitializer mongoInitializer = new BlockingInitializer() {

            @Override
            protected void initializeInternal() {
                try {
                    mongoServer = new EdmMongoServerImpl(
                            new Mongo(PropertyReader.getProperty(UimConfigurationProperty.MONGO_HOSTURL),
                                    Integer.parseInt(PropertyReader
                                            .getProperty(UimConfigurationProperty.MONGO_HOSTPORT))),
                            PropertyReader.getProperty(UimConfigurationProperty.MONGO_DB_EUROPEANA), "", "");
                } catch (NumberFormatException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (MongoDBException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (MongoException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        mongoInitializer.initialize(EdmMongoServerImpl.class.getClassLoader());
    } catch (Exception e) {
        log.log(Level.SEVERE, e.getMessage());
    }
}

From source file:eu.trentorise.smartcampus.mobility.util.GamificationHelper.java

License:Apache License

public static void main(String[] args)
        throws UnknownHostException, MongoException, SecurityException, RemoteException {
    MongoTemplate mg = new MongoTemplate(new Mongo("127.0.0.1", 37017), "mobility-logging");
    List<Map> findAll = mg.findAll(Map.class, "forgamification");
    for (Map m : findAll) {
        m.remove("_id");
        RemoteConnector.postJSON("http://localhost:8900", "/execute", JsonUtils.toJSON(m), null);
    }/*  w w w.j  a v  a2s  .com*/
}

From source file:eu.trentorise.smartcampus.parcheggiausiliari.AppConfig.java

License:Apache License

@Bean
public MongoTemplate getMongoTemplate() throws UnknownHostException, MongoException {
    return new MongoTemplate(new Mongo(mongohost, Integer.parseInt(mongoport)), mongoDatabaseName);
}

From source file:fr.cnes.sitools.datasource.mongodb.ActivationDataSourceResource.java

License:Open Source License

/**
 * Testing connections/*w  ww  .j  a  v a 2 s .  com*/
 * 
 * @param ds
 *          the DataSource to test
 * @param trace
 *          a {@link List} of String to store the trace of the test. Can be null
 * @return Representation results of the connection test.
 */
public boolean testDataSourceConnection(MongoDBDataSource ds, List<String> trace) {
    Boolean result = Boolean.TRUE;

    Mongo mongo = null;

    try {
        do {
            trace(trace, "Test mongo data source connection ...");
            try {
                mongo = new Mongo(ds.getUrl(), ds.getPortNumber());
            } catch (Exception e) {
                result = false;
                getMongoDBDataSourceAdministration().getLogger().info(e.getMessage());
                trace(trace, "Load driver class failed. Cause: " + e.getMessage());
                break;
            }

            try {
                // check if the database exists
                // List<String> databases = mongo.getDatabaseNames();
                // if (!databases.contains(ds.getDatabaseName())) {
                // result = false;
                // getMongoDBDataSourceAdministration().getLogger().info("Database does not exist");
                // trace.add("Database " + ds.getDatabaseName() + " does not exist");
                // break;
                // }

                DB db = mongo.getDB(ds.getDatabaseName());
                // if no user and password given lets authenticate on the database
                if (ds.isAuthentication() && !db.isAuthenticated()
                        && !db.authenticate(ds.getUserLogin(), ds.getUserPassword().toCharArray())) {
                    result = false;
                    getMongoDBDataSourceAdministration().getLogger().info("Authentication failed");
                    trace(trace, "Authentication failed");
                    break;
                }

                // try to get the stats of the database to check whether or not the database is accessible
                CommandResult cmd = db.getStats();
                if (!cmd.ok()) {
                    result = false;
                    getMongoDBDataSourceAdministration().getLogger()
                            .info("Error connecting to the database " + cmd);
                    trace(trace, "Error connecting to the database " + cmd);
                    break;
                }
                trace(trace, "Get connection to the database : OK");
            } catch (Exception e) {
                result = false;
                getMongoDBDataSourceAdministration().getLogger().info(e.getMessage());
                trace(trace, "Get connection to the database failed. Cause: " + e.getMessage());
                break;
            }
        } while (false);

    } finally {
        if (mongo != null) {
            mongo.close();
        }

    }

    return result;
}

From source file:fr.cnes.sitools.datasource.mongodb.business.SitoolsMongoDBDataSourceFactory.java

License:Open Source License

/**
 * Setup a dataSource for "users". Usage is for all users for consultation functions.
 * /* w  ww.  j a  v a 2  s  .c  o  m*/
 * @param dataSource
 *          the DataSource to update
 * @return SitoolsDataSource the new DataSource
 */
public SitoolsMongoDBDataSource setupDataSourceForUsers(MongoDBDataSource dataSource) {
    String key = dataSource.getId();
    SitoolsMongoDBDataSource foundDatasource = dataSources.get(key);
    if (foundDatasource == null) {

        Mongo mongo = null;
        try {
            MongoOptions options = new MongoOptions();
            ServerAddress address = new ServerAddress(dataSource.getUrl(), dataSource.getPortNumber());
            options.setConnectionsPerHost(dataSource.getMaxActive());
            mongo = new Mongo(address, options);
        } catch (UnknownHostException e) {
            logger.log(Level.INFO, null, e);
        } catch (MongoException e) {
            logger.log(Level.INFO, null, e);
        }

        foundDatasource = new SitoolsMongoDBDataSource(dataSource, mongo);
        dataSources.put(key, foundDatasource);
    }
    return foundDatasource;
}