Mongodbdb enables wiredTiger engine and zlib compression
Public static void main (String [] args) {
MongoClientOptions.Builder build = new MongoClientOptions.Builder ()
/ / maximum number of connections with data 50
Build.connectionsPerHost (50)
/ / if all current connection are in use, there can be 50 threads queuing on each connection.
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)
/ / get the database test. If it does not exist, the database will be established automatically.
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 users = db.getCollection ("data")
For (int I = 0; I < 1; iTunes +) {
Document document = new Document ()
Document.append ("address2", "sichuan chengdu444444444444444444444444")
Document.append ("address3", "sichuan chengdu44455555555555555555555555555555")
Users.insertOne (document)
}
MongoCollection users1 = db.getCollection ("data1")
For (int I = 0; I < 1; iTunes +) {
Document document = new Document ()
Document.append ("address2", "sichuan chengdu444444444444444444444444")
Document.append ("address3", "sichuan chengdu44455555555555555555555555555555")
Users1.insertOne (document)
}
/ / close must release resources after MongoClient is used.
Client.close ()
System.out.println ("ooooooooooooookkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk")
}
/ / determine whether collection exists
Private static boolean collectionExists (MongoDatabase database,String collectionName) {
Final MongoIterable iterable = database.listCollectionNames ()
Try (final MongoCursor it = iterable.iterator ()) {
While (it.hasNext ()) {
If (it.next () .equalsIgnoreCase (collectionName)) {
Return true
}
}
}
Return false
}