mongoClient备忘

转载 2016年08月30日 18:02:57

1. MongoDBCredential.java

public class MongoDBCredential {


private String databaseName;

private String username;

private String password;


public String getDatabaseName() {

return databaseName;

}


public void setDatabaseName(String databaseName) {

this.databaseName = databaseName;

}


public String getUsername() {

return username;

}


public void setUsername(String username) {

this.username = username;

}


public String getPassword() {

return password;

}


public void setPassword(String password) {

this.password = password;

}

}


2.MongoDBConfig.java

public class MongoDBConfig {


private String addresses;

private List<MongoDBCredentialT> credentials;


public String getAddresses() {

return addresses;

}


public void setAddresses(String addresses) {

this.addresses = addresses;

}


public List<MongoDBCredentialT> getCredentials() {

return credentials;

}


public void setCredentials(List<MongoDBCredentialT> credentials) {

this.credentials = credentials;

}

public List<MongoCredential> buildCredentials() {

List<MongoCredential> _credentials = new ArrayList<MongoCredential>();

for(MongoDBCredentialT credential : credentials) {

MongoCredential mc = MongoCredential.createCredential(credential.getUsername(), credential.getDatabaseName(), credential.getPassword().toCharArray());

_credentials.add(mc);

}

return _credentials;

}

public List<ServerAddress> buildAddresses() {

List<ServerAddress> addresses = new ArrayList<ServerAddress>();

String[] sources = this.addresses.split(";");

for(String item : sources) {

String[] hp = item.split(":");

addresses.add(new ServerAddress(hp[0], Integer.valueOf(hp[1])));

}

return addresses;

}

}


3.MongoDBDriver.java

public class MongoDBDriver {


private MongoClient mongoClient;

private MongoDBConfigT mongoDBConfig;

private Properties properties;


public void setMongoDBConfig(MongoDBConfigT mongoDBConfig) {

this.mongoDBConfig = mongoDBConfig;

}


public void setProperties(Properties properties) {

this.properties = properties;

}

private Integer connectionsPerHost = 32;

    private Integer threadsAllowedToBlockForConnectionMultiplier = 5;

    private Integer maxWaitTime = 30000;

    private Integer connectTimeout = 30000;

    private Integer socketTimeout = 30000;

    private Integer maxConnectionIdle = 60000;

    

    public void init() {

    if(properties != null) {

    String perHost = properties.getProperty("mongo.driver.connectionsPerHost");

    if(StringUtils.isNotBlank(perHost)) {

    connectionsPerHost = Integer.valueOf(perHost);

    }

    String multiplier = properties.getProperty("mongo.driver.threadsAllowedToBlockForConnectionMultiplier");

    if(StringUtils.isNotBlank(multiplier)) {

    threadsAllowedToBlockForConnectionMultiplier = Integer.valueOf(multiplier);

    }

    String _maxWaitTime = properties.getProperty("mongo.driver.maxWaitTime");

    if(StringUtils.isNotBlank(_maxWaitTime)) {

    maxWaitTime = Integer.valueOf(_maxWaitTime);

    }

    String _connectTimeout = properties.getProperty("mongo.driver.connectTimeout");

    if(StringUtils.isNotBlank(_connectTimeout)) {

    connectTimeout = Integer.valueOf(_connectTimeout);

    }

    String _socketTimeout = properties.getProperty("mongo.driver.socketTimeout");

    if(StringUtils.isNotBlank(_socketTimeout)) {

    socketTimeout = Integer.valueOf(_socketTimeout);

    }

    String _maxConnectionIdle = properties.getProperty("mongo.driver.maxConnectionIdle");

    if(StringUtils.isNotBlank(_maxConnectionIdle)) {

    maxConnectionIdle = Integer.valueOf(_maxConnectionIdle);

    }

    }

    

    execute();

    }

    

    public void execute() {

    MongoClientOptions.Builder builder = MongoClientOptions.builder();

    builder.connectionsPerHost(connectionsPerHost);

    builder.threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier);

    builder.maxWaitTime(maxWaitTime);

    builder.connectTimeout(connectTimeout);

    builder.socketTimeout(socketTimeout);

    builder.maxConnectionIdleTime(maxConnectionIdle);

    

    MongoClientOptions options = builder.build();

    mongoClient = new MongoClient(mongoDBConfig.buildAddresses(), mongoDBConfig.buildCredentials(), options);

    }


public MongoClient getMongoClient() {

return mongoClient;

}


public MongoDatabase getDatabase(String databaseName) {

return this.mongoClient.getDatabase(databaseName);

}

public DB getDB(String dbName) {

return this.mongoClient.getDB(dbName);

}

}


4.MongoDBClient.java

public class MongoDBClientT {

private MongoDBDriverT mongoDBDriver;

private String databaseName;


public void setMongoDBDriver(MongoDBDriverT mongoDBDriver) {

this.mongoDBDriver = mongoDBDriver;

}


public void setDatabaseName(String databaseName) {

this.databaseName = databaseName;

}

public MongoDatabase getDatabase() {

return this.mongoDBDriver.getDatabase(this.databaseName);

}

public static void main(String[] args) {

MongoDBDriverT driver = new MongoDBDriverT();

List<MongoDBCredentialT> credentials = new ArrayList<MongoDBCredentialT>();

MongoDBCredentialT credential = new MongoDBCredentialT();

credential.setDatabaseName("moin-fs");

credential.setUsername("root");

credential.setPassword("root123");

credentials.add(credential);

MongoDBConfigT config = new MongoDBConfigT();

config.setCredentials(credentials);

config.setAddresses("10.10.10.42:27017");

driver.setMongoDBConfig(config);

driver.init();

MongoDBClientT client = new MongoDBClientT();

client.setMongoDBDriver(driver);

client.setDatabaseName("moin-fs");

MongoDatabase db = client.getDatabase();

ListCollectionsIterable<Document> collectionsdb.listCollections();

MongoCursor<Document> it = collections.iterator();

while(it.hasNext()) {

System.out.print(it.next().toJson());

}

}

}


相关文章推荐

C#任务备忘

  • 2015-05-18 11:19
  • 5.33MB
  • 下载

备忘提醒工具

  • 2015-07-25 17:09
  • 340KB
  • 下载

MongoDB -- MongoClient连接池用法

主要的调整在于2.10.0版本中引入了MongoClient类,同时在其API中也说明了Mongo类会在将来的版本中被MongoClient替换(Note: This class has been s...

php使用mongoclient简单操作mongodb数据库

使用php的mongoclient简单操作mongo数据库

c#正则表达式备忘

  • 2011-12-18 12:53
  • 418KB
  • 下载

extjs备忘案例

  • 2012-01-18 20:28
  • 37KB
  • 下载

mongodb及mongoclient在win7下的编译和使用

1、导言 折腾mongodb几个小时终于有结果了。呃!现在就简单总结一下。 其实我的需求很简单,就是在C++代码中调用mongodb的库函数,也就是要得到mongoclient.lib。本来想直接下载...

前端备忘标签

  • 2012-06-04 23:48
  • 401KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)