1. Download the Mongo-java-driver.jar package to the project.
2.
Package Cn.nubia.apps.mongo;
Public interface Dbtemplate<t> {public
T fetchlist (String tablename);
public void Insert (String tablename);
3.
Package Cn.nubia.apps.mongo;
Import com.mongodb.MongoClientOptions;
public class Mongoclientoptionsbuilder {private int connectionsperhost;
Private Boolean autoconnectretry;
private int threadsallowed;
private int maxwaittime;
private int connecttimeout;
public int Getconnectionsperhost () {return connectionsperhost;
The public void setconnectionsperhost (int connectionsperhost) {this.connectionsperhost = Connectionsperhost;
public Boolean isautoconnectretry () {return autoconnectretry;
} public void Setautoconnectretry (Boolean autoconnectretry) {this.autoconnectretry = Autoconnectretry;
public int getthreadsallowed () {return threadsallowed;
The public void setthreadsallowed (int threadsallowed) {this.threadsallowed = threadsallowed;
public int Getmaxwaittime () {return maxwaittime;
The public void setmaxwaittime (int maxwaittime) {this.maxwaittime = Maxwaittime;
public int getconnecttimeout () {return connecttimeout; } public void SETconnecttimeout (int connecttimeout) {this.connecttimeout = ConnectTimeout;
Public Mongoclientoptions Build () {Mongoclientoptions.builder build = new Mongoclientoptions.builder ();
The maximum number of links that can be established with the target database Build.connectionsperhost (connectionsperhost);
Build.autoconnectretry (Autoconnectretry);
When all the connection are in use, each connection can have threadsallowed threads waiting in line
Build.threadsallowedtoblockforconnectionmultiplier (threadsallowed);
Build.maxwaittime (Maxwaittime);
Build.connecttimeout (ConnectTimeout);
return Build.build ();
}
}
4.
Package Cn.nubia.apps.mongo;
Import Com.mongodb.DB;
Import com.mongodb.DBCollection;
Import com.mongodb.MongoClient;
public class Mongotemplate {
private mongoclient mongoclient;
Private String dbname;
Public Mongoclient getmongoclient () {return
mongoclient;
}
public void Setmongoclient (Mongoclient mongoclient) {
this.mongoclient = mongoclient;
}
Public String Getdbname () {return
dbname;
}
public void Setdbname (String dbname) {
this.dbname = dbname;
}
Public DB Getdb () {return
mongoclient.getdb (dbname);
}
Public DB Getdb (String dbname) {return
mongoclient.getdb (dbname);
}
Public dbcollection getdbcollection (String collectionname) {return
getdb (). GetCollection (CollectionName);
} Public
dbcollection getdbcollection (String dbname,string collectionname) {return
getdb (dbname). GetCollection (CollectionName);
}
5.
Package Cn.nubia.apps.mongo;
Import Java.util.Date;
Import java.util.List;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.stereotype.Component;
Import Cn.nubia.apps.test.People;
Import Com.mongodb.AggregationOutput;
Import Com.mongodb.BasicDBObject;
Import com.mongodb.DBCollection;
Import Com.mongodb.DBCursor;
Import Com.mongodb.DBObject;
Import Com.mongodb.MapReduceCommand.OutputType;
Import Com.mongodb.MapReduceOutput; @Component public class Mongodbtemplate implements dbtemplate<dbobject>{@Autowired private Mongotemplate Mongote
Mplate;
@Override public dbobject fetchlist (String tablename) {Dbcollection db = mongotemplate.getdbcollection ("user");
dbcursor cursor = Db.find ();
while (Cursor.hasnext ()) {dbobject dbo = Cursor.next ();
System.err.println (Dbo.tostring ());
return null;
public void Insert (String tablename) {Dbcollection db = mongotemplate.getdbcollection (tablename); People p =New people ();
P.setname ("Alanlin");
P.setage (28);
P.setdate (New Date ());
Db.insert (P);
Db.ensureindex ("{\" name\ ": 1}"); } Public <T> list<t> Mr (String tablename,string map,string reduce,dbobject query) {dbcollection coll = mo
Ngotemplate.getdbcollection (tablename);
Dbcollection result = null; Synchronized (tablename) {Mapreduceoutput mapreduceoutput = coll.mapreduce (map, reduce, tablename + "_result", Outputty Pe.
Replace,query);
result = Mapreduceoutput.getoutputcollection ();
} dbcursor cur = Result.find ();
while (Cur.hasnext ()) {DBObject obj = Cur.next ();
System.err.println ("value=" + obj.get ("value"). toString ());
return null; /** * Data format is * {"_id": ObjectId ("564d7e3b178eb423a2e28928"), "name": "Alanlin", "num": {"_id": ObjectId ("564d7e42178eb423a2e28929"), "name": "Alanlin1", "num": {"_id": ObjectId ("564d7e5b178eb423a2e289 2a ")," name ":" Alanlin2 "," num ": {" _id ":ObjectId ("564d88de178eb423a2e2892b"), "name": "Alanlin2", "num": {"_id": ObjectId ("5653ba0b8cdc400b7380816 0 ")," name ":" Alanlin2 "," num ": * * * @param tablename/public void aggregate (String tablename) {Dbcolle
ction coll = mongotemplate.getdbcollection (tablename);
DBObject match = new Basicdbobject ("$match", New Basicdbobject ("name", "Alanlin2"));
DBObject sum = new Basicdbobject ();
Sum.put ("_id", "$num");
Sum.put ("Count", New Basicdbobject ("$sum", "1"));
DBObject Group = new Basicdbobject ("$group", sum);
Aggregationoutput out = Coll.aggregate (group);
String str = Out.getcommandresult (). toString ();
System.err.println ("str is" + str);
}
}
6. One of the configuration files supports stand-alone, as well as cluster methods.
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-4.1.xsd "> <bean id=" mongoclientoptionsbuilder "class=" Cn.nubia.apps.mongo.MongoClientOptionsBuilder "> <property name=" connectionsperhost "value=" ${ Mongodb.opts.connectionsPerHost} "/> <property name=" autoconnectretry "value=" ${ Mongodb.opts.autoConnectRetry} "/> <property name= threadsallowed" value= "${mongodb.opts.threadsallowed}"/ > <property name= "maxwaittime" value= "${mongodb.opts.maxwaittime}"/> <property name= "ConnectTimeout" Val Ue= "${mongodb.opts.connecttimeout}"/> </bean> <bean id= "Mongoclientoptions" Com.mongodb.MongoClientOptions "factory-bean=" Mongoclientoptionsbuilder "FActory-method= "Build" > </bean> <bean id= "mongodbtemplate" class= "Cn.nubia.apps.mongo.MongoDbTemplate" ></bean> <!--<bean id= "mongotemplate" class= "cn.nubia.apps.mongo.MongoTemplate" > <property na Me= "Mongoclient" > <bean class= "com.mongodb.MongoClient" > <constructor-arg index= "0" > <bean C lass= "Com.mongodb.ServerAddress" > <constructor-arg index= "0" value= "${mongodb.host}"/> <construct Or-arg index= "1" type= "int" value= "${mongodb.port}"/> </bean> </constructor-arg> <construc Tor-arg index= "1" ref= "mongoclientoptions"/> </bean> </property> <property name= "dbname" value= " ${mongodb.db} "/> </bean>--> <!--replica cluster environment--> <bean id=" mongotemplate "class=" Cn.nubia.apps.mon Go. Mongotemplate "> <property name=" mongoclient "> <bean class=" com.mongodb.MongoClient "> <construct Or-arg> <list&Gt
<ref bean= "serveraddress" ></ref> <ref bean= "serveraddress" ></ref> </list> </constructor-arg> </bean> </property> <property name= "dbname" value= "${mongodb.db}"/> & lt;/bean> <bean id= "serveraddress" class= "com.mongodb.ServerAddress" > <constructor-arg index= "0" value= "10.204.76.235"/> <constructor-arg index= "1" type= "int" value= "22017"/> </bean> <bean id= "Ser VerAddress1 "class=" com.mongodb.ServerAddress "> <constructor-arg index=" 0 "value=" 10.204.76.235 "/> < Constructor-arg index= "1" type= "int" value= "21017"/> </bean> </beans>
7. Related configuration file
mongodb.host=
mongodb.port=
mongodb.db=test
mongodb.opts.connectionsperhost=50
Mongodb.opts.autoconnectretry=true
mongodb.opts.threadsallowed=50
mongodb.opts.maxwaittime=120000
mongodb.opts.connecttimeout=60000