mongodb read write separation and java side


mongod.exe --bind_ip 192.168.1.202 --port 50001 --logpath E:\MongoDB\logs\log.txt --logappend --dbpath E:\MongoDB\data\db --replSet zhangwei
mongod.exe --bind_ip 192.168.1.202 --port 50002 --logpath E:\MongoDB\logs\log1.txt --logappend --dbpath E:\MongoDB\data\db1 --replSet zhangwei
mongod.exe --bind_ip 192.168.1.202 --port 50003 --logpath E:\MongoDB\logs\log2.txt --logappend --dbpath E:\MongoDB\data\db2  --replSet zhangwei

#Connect to the main server, where 192.168.1.202:50001 is set as the main server
mongo --host 192.168.1.202 --port 50001
 
#Initialization, which server is the primary server first
rs.initiate()


#View current status
rs.status()

#Add replica set
rs.add('192.168.1.202:50002')
rs.add('192.168.1.202:50003')


#Connect from server
mongo --host 192.168.1.202 --port 50002
mongo --host 192.168.1.202 --port 50003
 
#Set readable
rs.slaveOk()


Next is the configuration of java client

package com.system;

import java.util.ArrayList;
import java.util.List;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ReadPreference;
import com.mongodb.ServerAddress;

/*
 * mongodb Database link pool
 */
public class MongoDBDaoImpl
{
	private MongoClient mongoClient = null;
	private static final MongoDBDaoImpl mongoDBDaoImpl = new MongoDBDaoImpl();// Starved Han style single case model

	private MongoDBDaoImpl()
	{
		if (mongoClient == null)
		{
			ReadPreference preference = ReadPreference.secondary();  
			MongoClientOptions.Builder buide = new MongoClientOptions.Builder();
			buide.connectionsPerHost(100);// The maximum number of links that can be established with the target database
			buide.connectTimeout(1000 * 60 * 20);// Timeout for establishing link with database
			buide.maxWaitTime(100 * 60 * 5);// Maximum waiting time before a thread successfully obtains an available database
			buide.threadsAllowedToBlockForConnectionMultiplier(100);
			buide.maxConnectionIdleTime(0);
			buide.maxConnectionLifeTime(0);
			buide.socketTimeout(0);
			buide.socketKeepAlive(true);
			MongoClientOptions myOptions = buide.readPreference(preference).build();
			try
			{
				List<ServerAddress> addresses = new ArrayList<ServerAddress>();  
				ServerAddress address1 = new ServerAddress("192.168.1.202" , 50001); 
				ServerAddress address2 = new ServerAddress("192.168.1.202" , 50002); 
                ServerAddress address2 = new ServerAddress("192.168.1.202" , 50003); 
				addresses.add(address1);  
				addresses.add(address2); 
				mongoClient = new MongoClient(addresses,myOptions); 
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}

	public static MongoDBDaoImpl getMongoDBDaoImpl()
	{
		return mongoDBDaoImpl;
	}



	public DBCollection getCollection(String dbName, String collectionName)
	{
		DB db = mongoClient.getDB(dbName);
		return db.getCollection(collectionName);
	}

}

It needs to be implemented as mongodb voltage sharing. Our reading is placed in the secondary node and our writing is placed in the primary node

ReadPreference preference = ReadPreference.secondary();  

MongoClientOptions myOptions = buide.readPreference(preference).build();

 

These two sentences are the most important. These two sentences realize reading data only to the secondary node.

 

Next is the operation of java api on data

package com.system;
import java.util.List;

import org.json.JSONObject;

import util.CommEnum;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.ReadPreference;


public class MongoDB {

	//  private static final String HOST = PublicParameter.mongo_ip;
	//  private static final int PORT = PublicParameter.mongo_port;
	//    private static final String HOST = "218.5.26.21";
	//    private static final int PORT = 27017;
	//    private static Mongo mongo;
	//    private static DB db;
	//    
	//    static {
	//        //Connect to MongoDB
	//        mongo = new Mongo(HOST, PORT);
	//        //Open database testDB
	//       
	//    }
	//    


	// ====================================Query start==============================================
	/**
	 * @Title: queryOne
	 * @Description: TODO Query a record whose name is Zhang San 212122143214324322324
	 * @param dbCollection
	 * @return: void
	 */
	public static DBObject queryOne(String dbName, String collName, DBObject documents){
		DBCollection dbCollection = MongoDBDaoImpl.getMongoDBDaoImpl().getCollection(dbName, collName);
		//        DBObject documents = new BasicDBObject();
		//        documents.put("name", "Zhang San");
		DBObject result = dbCollection.findOne(documents);
		return result;
	}

	/**
	 * @Title: queryOne
	 * @Description: TODO Query a record whose name is Zhang San 212122143214324322324
	 * @param dbCollection
	 * @return: void
	 */
	public static DBCursor queryIn(String dbName, String collName, DBObject queryCondition){
		DBCollection dbCollection = MongoDBDaoImpl.getMongoDBDaoImpl().getCollection(dbName, collName);
		DBObject index = new BasicDBObject();
		index.put("c_phone", 1);
		index.put("c_account_id", 1);
		dbCollection.createIndex(index);
		 DBCursor limit = dbCollection.find(queryCondition).skip(0).limit(1);
		return limit;
	}

	public static long queryCount(String dbName, String collName, DBObject queryCondition){
		DBCollection dbCollection = MongoDBDaoImpl.getMongoDBDaoImpl().getCollection(dbName, collName);
		DBObject index = new BasicDBObject();
		index.put("c_phone", 1);
		index.put("black_state", 1);
		dbCollection.createIndex(index);
		long count = dbCollection.count(queryCondition);
	
		return count;
	}

	/**
	 * @Title: queryPage
	 * @Description: TODO Paging query
	 * @param dbCollection
	 * @return: void
	 */
	public static DBCursor queryPage(String dbName, String collName,int skip,int limit){
		DBCollection dbCollection = MongoDBDaoImpl.getMongoDBDaoImpl().getCollection(dbName, collName);
		DBCursor cursor = dbCollection.find().skip(skip).limit(limit);
		//        while (cursor.hasNext()) {
		//            System.out.println(cursor.next());
		//        }
		return cursor;
	}

	/** 
	 * 
	 * @Title: queryPage
	 * @Description: TODO Paging query
	 * @param dbCollection
	 * @return: void
	 */
	public static DBCursor queryPage(String dbName, String collName,int skip,int limit,DBObject where){

		DBCollection dbCollection = MongoDBDaoImpl.getMongoDBDaoImpl().getCollection(dbName, collName);
		DBCursor cursor = dbCollection.find(where).skip(skip).limit(limit);
		//        while (cursor.hasNext()) {
		//            System.out.println(cursor.next());
		//        }
		return cursor;
	}



	/**
	 * @Title: queryRange
	 * @Description: TODO Range query, query records between 3 and 5
	 * @param dbCollection
	 * @return: void
	 */
	public static DBCursor queryRange(String dbName, String collName,DBObject range,String column_name) {
		DBCollection dbCollection = MongoDBDaoImpl.getMongoDBDaoImpl().getCollection(dbName, collName);
		//        DBObject range = new BasicDBObject();
		//        range.put("$gte", 50);
		//        range.put("$lte", 52);

		DBObject dbObject = new BasicDBObject();
		dbObject.put(column_name, range);
		DBCursor cursor = dbCollection.find(dbObject);
		//        while (cursor.hasNext()) {
		//            System.out.println(cursor.next());
		//        }
		return cursor;
	}

	/**'
	 * @Title: queryList
	 * @Description: TODO Find all records
	 * @param dbCollection
	 * @return: void
	 */
	public static DBCursor queryList(String dbName, String collName) {
		DBCollection dbCollection = MongoDBDaoImpl.getMongoDBDaoImpl().getCollection(dbName, collName);
		DBCursor cursor = dbCollection.find();
		//        DBObject dbObject = null;
		//        while(cursor.hasNext()){
		//            dbObject = cursor.next();
		//            System.out.println(dbObject);
		//        }
		return cursor;
	}

	// ====================================Add start==============================================
	/**
	 * @Title: addOne
	 * @Description: TODO Add a record
	 * @param dbCollection
	 * @return: void
	 */
	public static void addOne(String dbName, String collName,DBObject documents){
		DBCollection dbCollection = MongoDBDaoImpl.getMongoDBDaoImpl().getCollection(dbName, collName);
		//        DBObject documents = new BasicDBObject();
		//        documents.put("xxx", 1);
		dbCollection.insert(documents);
	
	}

	/**
	 * @Title: addList 
	 * @Description: TODO Batch add records. Various data types can be used in the added records
	 * @param dbCollection
	 * @return: void
	 */
	public static void addList(String dbName, String collName, List<DBObject> listdbo){
		DBCollection dbCollection = MongoDBDaoImpl.getMongoDBDaoImpl().getCollection(dbName, collName);
		//        DBObject dbObject = new BasicDBObject();
		//        dbObject.put("name", "Lao Zhang");
		//        dbObject.put("age", 52);
		//        dbObject.put("job", "guard the old king");
		//        dbObject.put("remark", new BasicDBObject("address", "Shenzhen City, Guangdong Province"). append("street", "888 Shennan Avenue");
		//        listdbo.add(dbObject);

		dbCollection.insert(listdbo);
	}

	//    /**
	//     * @Title: addByJson
	//     * @Description: TODO json After object transfer, add
	//     * @param dbCollection
	//     * @return: void
	//     */
	//    public static void addByJson(DBCollection dbCollection){
	//        String JSON = "{\" name \ ": \" Wang Wu \ ", \" age \ ": 66," job \ ": \" guard old Wang \ ", \" remark \ ": {\" address \ ": \" Shenzhen City, Guangdong Province \ ", \" street \ ": \" 888 Shennan Avenue \ "}}";
	//        DBObject dbObject = (DBObject) JSON.parse(json);
	//        dbCollection.insert(dbObject);
	//    }

	// ====================================Modify start==============================================


	/**
	 * @Title: updateMulti
	 * @Description: TODO Modification record
	 * @param dbCollection
	 * @return: void
	 */
	public static void updateMulti(String dbName, String collName,DBObject newdbObject, DBObject olddbObject,boolean is_insert) {
		DBCollection dbCollection = MongoDBDaoImpl.getMongoDBDaoImpl().getCollection(dbName, collName);
		//        DBObject newdbObject = new BasicDBObject();
		//        newdbObject.put("name", "Zhang San");
		//        
		//        DBObject olddbObject = new BasicDBObject();
		//        olddbObject.put("name", "yisheng");
		// Need to add this
		DBObject upsertValue = new BasicDBObject("$set", newdbObject);  
		// The next two parameters are: 1. Insert if the updated data is not available; 2. Update multiple eligible documents at the same time 
		dbCollection.update(olddbObject, upsertValue, is_insert, false);
	}

	// ====================================Delete start==============================================
	/**
	 * @Title: deleteFirst
	 * @Description: TODO Delete first
	 * @param 
	 * @return: void
	 */
	public static void deleteFirst(String dbName, String collName){
		DBCollection dbCollection = MongoDBDaoImpl.getMongoDBDaoImpl().getCollection(dbName, collName);
		DBObject dbObject = dbCollection.findOne();
		dbCollection.remove(dbObject);
	}

	/**
	 * @Title: deleteOne
	 * @Description: TODO Delete a specified record
	 * @param dbCollection
	 * @return: void
	 */
	public static void deleteOne(String dbName, String collName,String key,String value){
		DBCollection dbCollection = MongoDBDaoImpl.getMongoDBDaoImpl().getCollection(dbName, collName);
		DBObject dbObject = new BasicDBObject();
		dbObject.put(key, value);
		dbCollection.remove(dbObject);
	}

	/**
	 * @Title: deleteByIn
	 * @Description: TODO Delete multiple records, for example: select * from tb where name in('12','34 ')
	 * @param dbCollection
	 * @return: void
	 */
	public static void deleteByIn(String dbName, String collName,List<String> list,String column_name) {
		DBCollection dbCollection = MongoDBDaoImpl.getMongoDBDaoImpl().getCollection(dbName, collName);
		//        List<String> list = new ArrayList<String>();
		//        list.add("Lao Zhang");
		//        list.add("Lao Wang");
		//        list.add("Zhang San");
		DBObject dbObject = new BasicDBObject();
		dbObject.put("$in", list);
		DBObject delObject = new BasicDBObject();
		delObject.put(column_name, dbObject);
		dbCollection.remove(delObject);
	}

	/**
	 * @Title: deleteByIn
	 * @Description: TODO Delete table
	 * @param dbCollection
	 * @return: void
	 */
	public static void deleteByTable(String dbName, String collName) {
		DBCollection dbCollection = MongoDBDaoImpl.getMongoDBDaoImpl().getCollection(dbName, collName);
		dbCollection.drop();
	}


	/**
	 * @Title: deleteAll
	 * @Description: TODO Delete all records
	 * @param dbCollection
	 * @return: void
	 */
	public static void deleteAll(String dbName, String collName){
		DBCollection dbCollection = MongoDBDaoImpl.getMongoDBDaoImpl().getCollection(dbName, collName);
		DBCursor cursor = dbCollection.find();
		while(cursor.hasNext()){
			dbCollection.remove(cursor.next());
		}
	}

	/**
	 * @Title: deleteAll
	 * @Description: TODO Delete some records
	 * @param dbCollection
	 * @return: void
	 */
	public static void deleteAll(String dbName, String collName,DBObject filter){
		DBCollection dbCollection = MongoDBDaoImpl.getMongoDBDaoImpl().getCollection(dbName, collName);
		DBCursor cursor = dbCollection.find(filter).skip(1).limit(10);
		while(cursor.hasNext()){
			dbCollection.remove(cursor.next());
		}
	}



}

 

Keywords: MongoDB Java Database JSON

Added by borabora12 on Thu, 02 Jan 2020 10:55:07 +0200