The previous note updated the delete, update, and query operations in the collection. Try the SEQUOIADB cluster operation this time. This includes creating and deleting replication groups, starting and stopping replication groups, adding, deleting, starting, stopping nodes in a replication group , getting master and slave nodes in a replication group, and so on.
Import Java.util.arraylist;import java.util.list;import Org.bson.bsonobject;import Org.bson.basicbsonobject;import Com.sequoiadb.base.node.nodestatus;import Com.sequoiadb.base.dbcursor;import Com.sequoiadb.base.Node;import Com.sequoiadb.base.replicagroup;import Com.sequoiadb.base.sequoiadb;import com.sequoiadb.exception.BaseException ;p Ublic class Blogrg {static string rgname = "TESTRG"; static string hostName = "Sdbserver1";p ublic static void Main (String [] args) {//Connect to database String host = "192.168.20.46"; String port = "11810"; String usr = "admin"; String Password = "admin"; Sequoiadb SDB = null;try {sdb = new sequoiadb (host + ":" + port, usr, password);} catch (Baseexception e) {e.printstacktrace (); System.exit (1);} Print information about the replication group (name, number) Printgroupinfo (SDB);//clear the Environment, delete duplicate replication group if (Isgroupexist (Sdb,rgname)) {System.out.println ("removing The old replica group ... "); Sdb.removereplicagroup (RgName);} Printgroupinfo (SDB);//Add a new replication group SYSTEM.OUT.PRINTLN ("Adding the new replica group ..."); Replicagroup RG = Sdb.createreplIcagroup (RgName);p rintgroupinfo (SDB);//Print Replication group node information System.out.println ("Tere is" + rg.getnodenum (nodestatus.sdb_ Node_all) + "nodes in the group."); /Add three new nodes node Node1 = AddNode (rg,50000); Node Node2 = AddNode (rg,50010); Node Node3 = AddNode (rg,50020);//Print replication group information System.out.println ("Tere is" + rg.getnodenum (nodestatus.sdb_node_all) + " Nodes in the group. "); /Get replication group master/slave Node master = rg.getmaster (); System.out.println ("The Master node is" +master.getport ()); System.out.println ("The Slave node is" + Rg.getslave (). Getport ());//Stop the Master node System.out.println ("Stoping The master Nodes ... "); Master.stop ();//wait for the primary node to stop while (Rg.getmaster (). Getport () = = Master.getport ()) {try{thread.sleep ();} catch ( Exception e) {}}//View the primary node of the new election System.out.println ("re-selecting The Master Nodes ..."); System.out.println ("The master node is" + Rg.getmaster (). Getport ());} private static void Printgroupinfo (Sequoiadb sdb) {ArrayList names = Sdb.getreplicagroupnames (); int count = 0; System.out.print ("The replica Groups is"); for (OBject name:names) {count++; System.out.print (String) name + ",");} System.out.println ("\nthere is" + Count + "replica groups in total."); private static Boolean isgroupexist (Sequoiadb sdb, String rgname) {ArrayList names = Sdb.getreplicagroupnames (); for ( Object name:names) {if (Rgname.equals (String) name) return true;} return false;} private static Node AddNode (Replicagroup RG, int port) {if (Rg.getnode (hostname,port)! = null) Rg.removenode (HostName, port, NULL); Node node = Rg.createnode (Hostname,port, "/opt/sequoiadb/database/test/" + port,null); System.out.println ("Starting the node" + Port + "..."); Node.start (); return node;}}
The above code adds a new replication group to the database and adds three primary nodes to the new replication group, the data group automatically elects the new master node, and after the primary node is stopped, the new master node is re-elected within the replication group.
The result of running the above code is:
The replica groups is Syscataloggroup, Datagroup, TESTRG, there is 3 replica groups in total. Removing the old replica group ... The replica groups is Syscataloggroup, Datagroup, there is 2 replica groups in total. Adding the new replica group ... The replica groups is Syscataloggroup, Datagroup, TESTRG, there is 3 replica groups in total. Tere is 0 nodes in the group.starting the node 50000...starting the node 50010...starting the node 50020...Tere is 3 nod Es in the group. The master node is 50000The slave node is 50010stoping the master node...re-selecting the master node ... The master node is 50020
As you can see, when the program starts running, there are three replication groups in the database, where TESTRG is a useless replication group that was last run, and the other two are the default two replication groups for the database. Remove redundant TESTRG replication groups through the Removereplicagroup () method, and then add new TESTRG replication groups through Createreplicagroup (), and within the newly created group through CreateNode () and start () Add and start 50000,50010,50020 three new nodes. by Getmaster () and the Getslave () method to obtain the master and slave nodes within the group, and using stop () to stop the primary node 50000, the primary node is completely stopped, the group will automatically re-elect the new master node 50020.
After the run is complete, view the details of the database TESTRG replication group through the shell console:
>rg.getdetail () {"Group": [{"HostName": "Sdbserver1", "DBPath": "/opt/sequoiadb/database/test/50000", "Service": [{"type": 0, "Name": "50000"}, {"Type": 1, "Na Me ":" 50001 "}, {" Type ": 2," Name ":" 50002 "}]," NodeID ": 1053}, {"HostName": "Sdbserver1", "DBPath": "/opt/sequoiadb/database/test/50010", "Service": [{ "Type": 0, "name": "50010"}, {"Type": 1, "name": "50011"}, { "Type": 2, "Name": "50012"}], "NodeID": 1054}, {"HostName": "Sdbserver1", "DBPath": "/opt/sequoiadb/database/test/50020", "Service": [{"Type": 0, "Name": "5002 0 "}, {" Type ": 1," name ":" 50021 "}, {" Type ": 2," name ": "50022"}], "NodeID": 1055}], "GroupID": 1023, "GroupName": "Testrg", "Primarynode": 1055, "Role": 0, "Status": 0, "Version": 4, "_id": {"$oid": "53d9d38e14a63a88c621edd8"}}return 1 row (s). Takes 0.4716s.You can see that there are three nodes in the group, and the Master node (Primarynode) is 1055, which is 50020.