Package com.mongodb

Examples of com.mongodb.MongoURI


    public void startup() {
        if (!_running.compareAndSet(false, true)) throw new IllegalStateException("startup called but already running");

        _lock.lock();
        try {
            _mongo = new Mongo(new MongoURI(_options.getMongoUri()));

            // Init the db/collection.
            LockDao.setup(_mongo, _options);
            if (_options.getEnableHistory()) LockHistoryDao.setup(_mongo, _options);
View Full Code Here


        MAX_SPLIT_NUM);
    bulkInsertLine = param.getIntValue(ParamKey.bulkInsertLine,
        this.bulkInsertLine);
    dropCollectionBeforeInsertionSwitch = param.getBooleanValue(ParamKey.dropCollectionBeforeInsertionSwitch, false);

    MongoURI uri = new MongoURI(outputUri);
    Mongo mongo = null;
    try {
      mongo = new Mongo(uri);
    } catch (UnknownHostException e) {
      throw new IllegalStateException(
          " Unable to connect to MongoDB at '" + uri + "'", e);
    }
    DB db = mongo.getDB(uri.getDatabase());

    // if there's a username and password
    if (uri.getUsername() != null && uri.getPassword() != null
        && !db.isAuthenticated()) {
      boolean auth = db
          .authenticate(uri.getUsername(), uri.getPassword());
      if (auth) {
        log.info("Sucessfully authenticated with collection.");
      } else {
        throw new IllegalArgumentException(
            "Unable to connect to collection. You have to check your username and password");
      }
    }
   
    if (dropCollectionBeforeInsertionSwitch){
      log.info("start to drop collection " + uri.getCollection());
      DBCollection coll = MongoUtils.getCollection(uri);
      coll.drop();
      log.info("drop collection " + uri.getCollection() + " before insert data successfully");
    }

    param.putValue(ParamKey.outputUri, this.outputUri);
    param.putValue(ParamKey.outputFields, this.outputFields);
    param.putValue(ParamKey.concurrency, String.valueOf(this.concurrency));
View Full Code Here

    fieldNames = json.keySet().toArray(new String[json.keySet().size()]);   
  }

  @Override
  public void connection() {
    MongoURI uri = new MongoURI(outputUri);
    log.info("try to connect " + uri.toString());
    coll = MongoUtils.getCollection(uri);
  }
View Full Code Here

        splits.add(param);
        return splits;
      }
    }
   
    MongoURI uri = new MongoURI(inputUri);
    Mongo mongo = null;
    try {
      mongo = uri.connect();
    } catch (UnknownHostException e) {
      throw new IllegalStateException( " Unable to connect to MongoDB at '" + uri + "'", e);
    }
        DB db = mongo.getDB(uri.getDatabase());
        DBCollection coll = db.getCollection(uri.getCollection());
        final CommandResult stats = coll.getStats();
       
        final boolean isSharded = stats.getBoolean( "sharded", false );
        log.info("Collection Sharded? " + isSharded);
       
View Full Code Here

    inputFields = param.getValue(ParamKey.inputFields, this.inputFields).trim();
    inputQuery = param.getValue(ParamKey.inputQuery, this.inputQuery).trim();
    inputSort = param.getValue(ParamKey.inputSort, this.inputSort).trim();
    inputLimit = param.getValue(ParamKey.inputLimit, this.inputLimit).trim();
   
    MongoURI uri = new MongoURI(inputUri);
    Mongo mongo = null;
    try {
      mongo = new Mongo(uri);
    }catch (UnknownHostException e) {
      throw new IllegalStateException(" Unable to connect to MongoDB at '" + uri + "'", e);
    }
    DB db = mongo.getDB(uri.getDatabase());
   
    //if there's a username and password
        if(uri.getUsername() != null && uri.getPassword() != null && !db.isAuthenticated()){
            boolean auth = db.authenticate(uri.getUsername(), uri.getPassword());
            if(auth) {
                log.info("Sucessfully authenticated with collection.");
            }
            else {
                throw new IllegalArgumentException("Unable to connect to collection. You have to check your username and password" );
View Full Code Here

    fieldNeedSplit = getParam().getBooleanValue(ParamKey.fieldNeedSplit, false);
    fieldSplitChar = getParam().getCharValue(ParamKey.filedSplitChar, (char)0);
    dataTransform = getParam().getValue(ParamKey.dataTransformClass, "");

    log.info("inputUri:" + inputUri);
    uri = new MongoURI(inputUri);
    fields = MongoUtils.convertStringToDBObject(inputFields);
    query = MongoUtils.convertStringToDBObject(inputQuery);
    sort = MongoUtils.convertStringToDBObject(inputSort);
   
   
View Full Code Here

    verify(env);
  }

  @Test
  public void morphiaDatastore() {
    MongoURI uri = createMock(MongoURI.class);
    expect(uri.getDatabase()).andReturn("mydb");

    Mongo mongo = createMock(Mongo.class);

    Datastore datastore = createMock(Datastore.class);
View Full Code Here

    Environment env = createMock(Environment.class);
    expect(env.getRequiredProperty("db")).andReturn("mongodb://localhost/mydb");

    replay(env);

    MongoURI mongoURI = new MongoModule().mongoURI(env);
    assertNotNull(mongoURI);
    assertNotNull(mongoURI.getHosts());
    assertTrue(mongoURI.getHosts().contains("localhost"));
    assertEquals("mydb", mongoURI.getDatabase());

    verify(env);
  }
View Full Code Here

  @Test
  public void mongo() throws Exception {
    Mongo mongo = createMock(Mongo.class);

    MongoURI uri = createMock(MongoURI.class);
    expect(uri.connect()).andReturn(mongo);

    replay(uri, mongo);

    Mongo result = new MongoModule().mongo(uri);
    assertEquals(mongo, result);
View Full Code Here

  }

  @Test
  public void mongoDbFactory() throws Exception {
    String database = "mydb";
    MongoURI uri = createMock(MongoURI.class);
    expect(uri.getUsername()).andReturn(null);
    expect(uri.getPassword()).andReturn(null);
    expect(uri.getDatabase()).andReturn(database);

    Mongo mongo = createMock(Mongo.class);

    MongoDbFactory mongoDbFactory =
        PowerMock
View Full Code Here

TOP

Related Classes of com.mongodb.MongoURI

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.