首页 > 代码库 > mongodbdb启用wiredTiger引擎及zlib压缩
mongodbdb启用wiredTiger引擎及zlib压缩
public static void main(String[] args) {
MongoClientOptions.Builder build = new MongoClientOptions.Builder();
// 与数据最大连接数50
build.connectionsPerHost(50);
// 如果当前所有的connection都在使用中,则每个connection上可以有50个线程排队等待
build.threadsAllowedToBlockForConnectionMultiplier(50);
build.connectTimeout(1 * 60 * 1000);
build.maxWaitTime(2 * 60 * 1000);
MongoClientOptions options = build.build();
String host="192.168.80.100";
int port=27017;
MongoClient client = new MongoClient(new ServerAddress(host, port), options);
// 获取数据库test,不存在的话,会自动建立该数据库
MongoDatabase db = client.getDatabase("fanyin");
// { storageEngine: {
// wiredTiger: { configString: ‘block_compressor=zlib‘ }}
CreateCollectionOptions createCollectionOptions = new CreateCollectionOptions();
BasicDBObject configString = new BasicDBObject();
configString.put("configString", "block_compressor=zlib");
BasicDBObject bson = new BasicDBObject();
bson.put("wiredTiger", configString);
createCollectionOptions.storageEngineOptions(bson);
boolean isCollectionExist=collectionExists(db, "data");
if(!isCollectionExist){
db.createCollection("data", createCollectionOptions);
}
MongoCollection<Document> users = db.getCollection("data");
for (int i = 0; i < 1; i++) {
Document document = new Document();
document.append("address2", "sichuan chengdu444444444444444444444444");
document.append("address3", "sichuan chengdu44455555555555555555555555555555");
users.insertOne(document);
}
MongoCollection<Document> users1 = db.getCollection("data1");
for (int i = 0; i < 1; i++) {
Document document = new Document();
document.append("address2", "sichuan chengdu444444444444444444444444");
document.append("address3", "sichuan chengdu44455555555555555555555555555555");
users1.insertOne(document);
}
// MongoClient使用完后必须要close释放资源
client.close();
System.out.println("ooooooooooooookkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk");
}
//判断collection是否存在
private static boolean collectionExists(MongoDatabase database,String collectionName){
final MongoIterable<String> iterable = database.listCollectionNames();
try (final MongoCursor<String> it = iterable.iterator()) {
while (it.hasNext()) {
if (it.next().equalsIgnoreCase(collectionName)) {
return true;
}
}
}
return false;
}
mongodbdb启用wiredTiger引擎及zlib压缩