Sequoiadb 测试体验系列之四 – Java 开发

来源:互联网 发布:大麦盒子看电影软件 编辑:程序博客网 时间:2024/05/24 07:04

上一篇中尝试了一下SequoiaDB的 shell控制台的使用,研究了一下控制台中匹配符、更新符和聚集符的使用。今天尝试一下SequoiaDB官方提供的Java 驱动。

首先要从官方下载驱动程序,按照http://www.sequoiadb.com/document/1.8/developement/application/java/topics/java.html给出的信息搭建开发环境,也就是将jar包加入到工程中。

今天主要尝试了一下Sequoiadb,CollectionSpace,DBCollection(为毛CollectionSpace类名字前面就不加DB...)这几个类给出的基本接口。实现了数据库实例的创建,集合空间的创建,查询,集合的创建,数据的插入,和数据集的插入等一系列操作。完整代码如下:


<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;//创建数据库实例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);}//创建集合空间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();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);}//创建集合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);//插入包含记录的BSONObjectcl.insert(insertor);System.out.println("After 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;//创建一个包含五条记录的数据集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();}//在集合中插入数据集cl.bulkInsert(list, 1);System.out.println("After inserting 5 record");System.out.println("There are " + cl.getCount() + " record(s) in the collection");}}</span>


在运行上面的代码前,通过控制台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>

可以看出这是一个空数据库,没有任何集合空间和集合。运行代码,程序的输出为:

<span style="font-size:18px;">The CS test_csis createdCollection Space: test_csThe Collection test_cl is createdBefore inserting one recordThere are 0 record(s) in the collectionAfter inserting one recordThere are 1 record(s) in the collectionBefore inserting 5 recordsThere are 1 record(s) in the collectionAfter inserting 5 recordThere are 6 record(s) in the collection</span>

可以看出,上面的代码在空数据库中创建了一个名为test_cs的集合空间,一个名为test_cl的集合,并分两次在集合中分别插入了1条和5条记录,总共6条记录。

最后,利用控制台shell查询一下数据库的情况:

<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_csTakes 0.1385s.> testcl = testcs.getCL("test_cl")localhost:11810.test_cs.test_clTakes 0.8262s.> testcl.find(){  "_id": {    "$oid": "53c621d0c5d00bea55f5a959"  },  "Age": 10,  "Name": "foo",  "Phone": {    "home": "123456789",    "mobile": "987654321"  }}{  "_id": {    "$oid": "53c621939c74ef081d3e7d83"  },  "Address": {    "city": "foo",    "province": "bar"  },  "Id": 3,  "Phonenumber": {    "Number": "88888888",    "Type": "Office"  },  "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,  "Phonenumber": {    "Number": "88888888",    "Type": "Office"  },  "name": "test"}{  "_id": {    "$oid": "53c621939c74ef081d3e7d82"  },  "Address": {    "city": "foo",    "province": "bar"  },  "Id": 2,  "Phonenumber": {    "Number": "88888888",    "Type": "Office"  },  "name": "test"}{  "_id": {    "$oid": "53c621939c74ef081d3e7d80"  },  "Address": {    "city": "foo",    "province": "bar"  },  "Id": 0,  "Phonenumber": {    "Number": "88888888",    "Type": "Office"  },  "name": "test"}Return 6 row(s).Takes 0.3921s.</span>

现在的数据空中有了刚刚插入的test_cs集合空间,test_cl集合,和刚刚分两次插入的6条数据。

0 0