SEQUOIADB Test Experience series of four –java development __java

Source: Internet
Author: User

In the previous article, I tried the use of SEQUOIADB's shell console, and studied the use of matching, updating, and aggregating characters in the console. Today, try Sequoiadb's official Java drive.

First to download the driver from the official, according to http://www.sequoiadb.com/document/1.8/developement/application/java/topics/ Java.html the information given to build the development environment, that is, the jar package into the project.

Today the main try Sequoiadb,collectionspace,dbcollection (for Mao Collectionspace class name before the db ... The basic interfaces given by these classes. The implementation of database instance creation, collection space creation, query, set creation, data insertion, and data set insertion, and so on a series of operations. The complete code is as follows:


<span style= "FONT-SIZE:18PX;"
>import java.util.ArrayList;

Import java.util.List;
Import Org.bson.BSONObject;

Import Org.bson.BasicBSONObject;
Import Com.sequoiadb.base.CollectionSpace;
Import com.sequoiadb.base.DBCollection;
Import Com.sequoiadb.base.DBCursor;
Import com.sequoiadb.base.Sequoiadb;

Import com.sequoiadb.exception.BaseException;
	public class Blogcollectionspace {static String cs_name = "Test_cs";
	
	static String Cl_Name = "TEST_CL";
		public static void Main (string[] args) {String host = "192.168.20.46";
		String port = "11810";
		String usr = "admin";
		
		String Password = "admin";
		
		SEQUOIADB SDB = null;
		Create a database instance try{SDB = new Sequoiadb (host+ ":" + port, usr, password);
			catch (Baseexception e) {e.printstacktrace ();
		System.exit (1);
		} if (Sdb.iscollectionspaceexist (Cs_name)) {sdb.dropcollectionspace (cs_name);
		
		//Create the SET space Collectionspace cs = Sdb.createcollectionspace (cs_name); if (Sdb.iscollectionspaceexist (cs_name) && cs.getname () = = Cs_name) {System.out.println ("the CS" + Cs_name + "is created");
		}else {system.exit (2);
		Dbcursor cslist = sdb.listcollectionspaces () of the aggregate space in the query database;
			while (Cslist.hasnext ()) {String name = (String) cslist.getnext (). Get ("name");
		System.out.println ("Collection space:" + name);
		} if (Cs.iscollectionexist (Cl_Name)) {cs.dropcollection (cl_name);
		
		}//Create collection Dbcollection cl = cs.createcollection (Cl_Name); if (Cs.iscollectionexist (cl_name) && cl.getname () = = Cl_Name) {System.out.println ("the Collection" + Cl_Name +
		"is created");
		}else {system.exit (3);
		} System.out.println ("Before inserting one record");
		
		System.out.println ("There are" + cl.getcount () + "record (s) in the collection");
		Bsonobject insertor = null;
		Insertor = new Basicbsonobject ();
		Bsonobject phone = new Basicbsonobject ();
		Insertor.put ("Name", "foo");
		Insertor.put ("Age", 10);
		Phone.put ("Home", "123456789"); PHone.put ("mobile", "987654321");

		Insertor.put ("Phone", phone);
		Insert the Bsonobject Cl.insert (Insertor) containing the record;
		System.out.println ("After the inserting one record");
		
		
		System.out.println ("There are" + cl.getcount () + "record (s) in the collection");
		System.out.println ("Before inserting 5 records");
		
		
		System.out.println ("There are" + cl.getcount () + "record (s) in the collection");

		list<bsonobject> list = null;
			Create a DataSet containing five records try {list = new arraylist<bsonobject> (5);
				for (int i = 0; i < 5; i++) {Bsonobject obj = new Basicbsonobject ();
				Bsonobject addressobj = new Basicbsonobject ();

				Bsonobject phoneobj = new Basicbsonobject ();
				Addressobj.put ("City", "foo");

				Addressobj.put ("Province", "bar");
				Phoneobj.put ("Type", "Office");

				Phoneobj.put ("number", "88888888");
				Obj.put ("name", "Test");
				Obj.put ("Id", I);
				Obj.put ("PhoneNumber", phoneobj);

				Obj.put ("Address", addressobj);
			List.add (obj); }} CATCH (Exception e) {System.out.println ("Failed to create Name list record.");
		E.printstacktrace ();
		
		///Insert data set in collection Cl.bulkinsert (list, 1);
		System.out.println ("After the inserting 5 record");
	System.out.println ("There are" + cl.getcount () + "record (s) in the collection"); }} </span>


Before running the above code, look at the state of the database through the console shell:

<span style= "FONT-SIZE:18PX;" >> db.listcollectionspaces () return
0 row (s).
Takes 0.1100s.
> db.listcollections () return
0 row (s).
Takes 0.1139s.</span>

You can see that this is an empty database, without any collection space or collection. Run the code, the output of the program is:

<span style= "FONT-SIZE:18PX;"  >the CS test_csis created
Collection space:test_cs the
Collection Test_cl is created
before one Record
There are 0 record (s) in the collection
inserting one record
There are 1 record (s) in the Colle Ction
before inserting 5 records
There are 1 record (s) in the collection after
inserting 5 record
ther E are 6 record (s) in the collection
</span>

As you can see, the above code creates a collection space named Test_cs in an empty database, a collection named Test_cl, and inserts 1 and 5 records in the collection, totaling 6 records, two times.

Finally, use the console shell to query the database:

<span style= "FONT-SIZE:18PX;"
>> db.listcollectionspaces () {"Name": "Test_cs"} return 1 row (s).
Takes 0.1291s.
> db.listcollections () {"Name": "Test_cs.test_cl"} return 1 row (s).
Takes 0.1441s.
> Testcs = Db.getcs ("Test_cs") Localhost:11810.test_cs takes 0.1385s.
> TESTCL = TESTCS.GETCL ("Test_cl") LOCALHOST:11810.TEST_CS.TEST_CL takes 0.8262s.
    > Testcl.find () {"_id": {"$oid": "53c621d0c5d00bea55f5a959"}, "age": Ten, "Name": "foo", "Phone": { "Home": "123456789", "mobile": "987654321"} {"_id": {"$oid": "53c621939c74ef081d3e7d83"}, "Address" : {"City": "foo", "Province": "Bar"}, "Id": 3, "PhoneNumber": {"number": "88888888", "Type": "Off
    Ice "}," name ":" Test "} {" _id ": {" $oid ":" 53c621939c74ef081d3e7d81 "}," address ": {" City ":" foo ", "Province": "Bar"}, "Id": 1, "PhoneNumber": {"number": "88888888", "Type": "Office"}, "name": "Test
  '} {' _id ': {  "$oid": "53c621939c74ef081d3e7d84"}, "address": {"City": "foo", "Province": "Bar"}, "Id": 4, "Phone Number ": {Number": "88888888", "Type": "Office"}, "name": "Test"} {"_id": {"$oid": "53c621939c74ef 081d3e7d82 "}," address ": {" City ":" foo "," Province ":" Bar "}," Id ": 2," PhoneNumber ": {" number ": ' 88888888 ', ' Type ': ' Office '}, ' name ': ' Test '} {' _id ': {' $oid ': ' 53c621939c74ef081d3e7d80 '}, ' Addre SS ": {" City ":" foo "," Province ":" Bar "}," Id ": 0," PhoneNumber ": {" number ":" 88888888 "," Type ":"
Office "}," name ":" Test "} return 6 row (s). Takes 0.3921s.</span>

Now the data is in the air with the Test_cs set space just inserted, the Test_cl collection, and the 6 data that has just been inserted two times.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.