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:test.mongodb.servlet.MongoDBServlet.java

License:Open Source License

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
    String host = System.getenv("MONGODB_HOST");
    String sport = System.getenv("MONGODB_PORT");
    String db = System.getenv("MONGODB_DATABASE");
    if (db == null)
        db = "mydb";
    String user = System.getenv("MONGODB_USER");
    String password = System.getenv("MONGODB_PASSWORD");
    int port = Integer.decode(sport.trim());

    try {/*from  w ww. j a  v  a  2 s  . co  m*/
        mongo = new Mongo(host, port);
    } catch (UnknownHostException e) {
        throw new ServletException("Failed to access Mongo server", e);
    }
    mongoDB = mongo.getDB(db);
    if (mongoDB.authenticate(user, password.toCharArray()) == false) {
        throw new ServletException("Failed to authenticate against db: " + db);
    }
}

From source file:Twitter.FilterStream.java

License:Apache License

public void LinkMongodb() throws Exception {

    /*  //from w  w w.ja v  a 2 s  . c  om
     * Link Mongodb   
     * build a data named FourS2  
     * build a collection named Foursquare  
     *    
     */
    mongo = new Mongo("localhost", 27017);
    DB db = mongo.getDB("TwitterRT");
    collection = db.getCollection("tweet2");
    System.out.println("Link Mongodb!");
}

From source file:TwitterCrawler.PrintSampleStream.java

License:Apache License

public void LinkMongodb() throws Exception {

    /*//from  w  ww  .j  a v a2s .c  o  m
     * Link Mongodb 
     * build a data named FourS2
     * build a collection named Foursquare
     *  
     */
    mongo = new Mongo("localhost", 27017);
    DB db = mongo.getDB("Twitter_131026");
    collection = db.getCollection("BarackObama");
    System.out.println("Link Mongodb!");
}

From source file:uk.ac.soton.itinnovation.sad.coordinator.Coordinator.java

License:Open Source License

/**
 * Requires valid path to configuration file.
 *
 * @param pathToConfigurationFile//from  w  w w  .  j  a v  a2 s.c o m
 */
public Coordinator(String pathToConfigurationFile) {

    logger.debug("New SAD coordinator initialising with the following path: " + pathToConfigurationFile);

    if (pathToConfigurationFile == null) {
        throw new RuntimeException("SAD coordinator configuration path can not be null");
    }

    if (pathToConfigurationFile.length() < 1) {
        throw new RuntimeException("SAD coordinator configuration path can not be empty");
    }

    File configurationFile = new File(pathToConfigurationFile);

    if (!configurationFile.exists()) {
        throw new RuntimeException("SAD coordinator configuration file does not exist on the following path: "
                + pathToConfigurationFile);
    }

    if (!configurationFile.isFile()) {
        throw new RuntimeException(
                "SAD coordinator configuration file is not a file: " + pathToConfigurationFile);
    }

    String absPathToConfigurationFile;
    try {
        absPathToConfigurationFile = configurationFile.getCanonicalPath();
    } catch (IOException ex) {
        throw new RuntimeException("Failed to resolve absolute path to Coordinator configuration file", ex);
    }

    logger.debug("Resolved SAD coordinator file path to: " + absPathToConfigurationFile);

    StringBuilder fileContents = new StringBuilder((int) configurationFile.length());
    Scanner scanner;
    try {
        scanner = new Scanner(configurationFile);
    } catch (FileNotFoundException ex) {
        throw new RuntimeException("Coordinator configuration file not found", ex);
    }
    while (scanner.hasNextLine()) {
        fileContents.append(scanner.nextLine());
        fileContents.append(lineSeparator);
    }
    scanner.close();

    String configAsString = fileContents.toString();

    if (configAsString.length() < 1) {
        throw new RuntimeException(
                "SAD coordinator configuration file can not be empty: " + absPathToConfigurationFile);
    }

    logger.debug("Loading SAD coordinator configuration: " + configAsString);

    configuration = JSONObject.fromObject(configAsString);

    if (!configuration.containsKey("database")) {
        throw new RuntimeException("SAD coordinator configuration file \'" + pathToConfigurationFile
                + "\' must contain field \'database\'");
    }

    if (!configuration.containsKey("pojo_packages")) {
        throw new RuntimeException("SAD coordinator configuration file \'" + pathToConfigurationFile
                + "\' must contain field \'pojo_packages\'");
    }

    JSONObject configurationDatabase = configuration.getJSONObject("database");

    if (!configurationDatabase.containsKey("mongo_server")) {
        throw new RuntimeException("SAD coordinator configuration file \'" + pathToConfigurationFile
                + "\' must contain field \'database/mongo_server\'");
    }
    if (!configurationDatabase.containsKey("name")) {
        throw new RuntimeException("SAD coordinator configuration file \'" + pathToConfigurationFile
                + "\' must contain field \'database/name\'");
    }

    JSONObject configurationDatabaseMongoServer = configurationDatabase.getJSONObject("mongo_server");

    if (!configurationDatabaseMongoServer.containsKey("host")) {
        throw new RuntimeException("SAD coordinator configuration file \'" + pathToConfigurationFile
                + "\' must contain field \'database/mongo_server/host\'");
    }
    if (!configurationDatabaseMongoServer.containsKey("port")) {
        throw new RuntimeException("SAD coordinator configuration file \'" + pathToConfigurationFile
                + "\' must contain field \'database/mongo_server/port\'");
    }

    configurationDatabaseMongoServerHost = configurationDatabaseMongoServer.getString("host");
    configurationDatabaseMongoServerPort = configurationDatabaseMongoServer.getInt("port");
    configurationDatabaseName = configurationDatabase.getString("name");

    configurationPojoPackages = configuration.getJSONArray("pojo_packages");

    if (configurationPojoPackages.isEmpty()) {
        logger.warn("No POJO package names detected");
    }

    logger.debug("Testing database connection");

    // connect to Mongo
    try {
        mongo = new Mongo(configurationDatabaseMongoServerHost, configurationDatabaseMongoServerPort);
    } catch (UnknownHostException ex) {
        throw new RuntimeException("Failed to connect to Mongo server, unknown host", ex);
    }

    logger.debug("Coordinator initialized successfully, call setupDatabase() or deleteDatabase() next");
}

From source file:Util.MongoConnection.java

public void connect(String username, String password, String hostname, String database, int port)
        throws Exception {
    try {//w ww  . ja  va2 s.co m
        MongoConnection.m = new Mongo(hostname, port);
        MongoConnection.db = m.getDB(database);
    } catch (Exception e) {
        System.out.print("No se pudo conectar a Mongo");
    }

}

From source file:uuidupdater.DatabaseTool.java

public DatabaseTool(String host, int port, int threads) {
    this(threads);
    try {//  w  ww  . j av  a2  s.c om
        this.mongo = new Mongo(host, port);
        db = null;
        collection = null;
    } catch (UnknownHostException ex) {
        Logger.getLogger(DatabaseTool.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:uuidupdater.DatabaseTool.java

public void connect(String host, int port) throws UnknownHostException {
    this.mongo = new Mongo(host, port);
    db = null;//w w  w  .j  a  v  a  2  s.  co m
    collection = null;
}