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
- Sequoiadb 测试体验系列之四 – Java 开发
- Sequoiadb 测试体验系列之四 – Java 开发
- Sequoiadb 测试体验系列之五 – Java 开发2
- Sequoiadb 测试体验系列之五 – Java 开发2
- Sequoiadb 测试体验系列之六 – Java 开发3
- Sequoiadb 测试体验系列之二 – shell 控制台初探
- Sequoiadb 测试体验系列之二 – shell 控制台初探
- Sequoiadb 测试体验系列之三 – shell 控制台再探
- Sequoiadb 测试体验系列之三 – shell 控制台再探
- Sequoiadb 测试体验系列之一 – 安装和配置
- Sequoiadb 测试体验系列之一 – 安装和配置
- 【入门教程】SequoiaDB Java开发入门
- 测试驱动开发之体验--开篇
- JAVA ProgressBar系列之四
- 敏捷开发团队管理系列之四:程序与测试团队III
- 测试驱动开发系列之四--代码中的坏味道以及如何改进他们
- 建行E商贸通支付开发系列之四(测试环境搭建)
- 建行E商贸通支付开发系列之四(测试环境搭建)
- Android搭建VPN截获用户网络数据-非root
- hibernate中identity和increment区别
- java项目部署到linux系统上出现java.lang.OutOfMemoryError: PermGen space错误
- IOS-时间与字符串互相转换
- 启动与关闭Oracle rac数据库集群
- Sequoiadb 测试体验系列之四 – Java 开发
- android编程
- python 进阶学习之4
- 使用 MVVMLight 消息通知
- 调整oracle sga的大小导致 ORA-00851和ORA-00844错误
- 在浏览器中通过bartender,调用条码打印机的active控件代码的实现
- linux 下端口close_wait 过多
- FFmpeg-Android
- hdu2544 最短路