您的位置:首页 > 编程语言 > Java开发

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

2014-07-28 15:15 417 查看
上一篇笔记中主要尝试了一下SequoiaDB 的 Sequoiadb,CollectionSpace,DBCollection 这几个类给出的基本接口。实现了数据库实例的创建,集合空间的创建,查询,集合的创建,数据的插入,和数据集的插入等一系列操作。这次尝试一下在集合中的删除,更新,查询等操作。

前半部分代码是将上次的代码稍作了修改,实现的功能还是一样,连接database,创建collection space,创建collection,插入两种格式的6条记录。

[java] view
plaincopy

<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("There are " + cl.getCount() + " record(s) in the collection");

System.out.println("Inserting one record...");

insertOneRecord(cl);

System.out.println("There are " + cl.getCount() + " record(s) in the collection");

System.out.println("Inserting 5 records...");

insertSomeRecords(cl);

System.out.println("There are " + cl.getCount() + " record(s) in the collection");

System.out.println("Deleting one record...");

deleteOneRecord(cl);

System.out.println("There are " + cl.getCount() + " record(s) in the collection");

System.out.println("Deleting 5 records...");

deleteSomeRecords(cl);

System.out.println("There are " + cl.getCount() + " record(s) in the collection");

System.out.println("Inserting 6 records again...");

insertOneRecord(cl);

insertSomeRecords(cl);

System.out.println("There are " + cl.getCount() + " record(s) in the collection");

System.out.println("Querying all records with the value of 'Id' from 0 to 8...");

queryId(cl);

System.out.println("Updateing all the records, add 10 to all the 'Id'...");

updateId(cl);

System.out.println("Querying all records with the value of 'Id' from 0 to 8...");

queryId(cl);

}

static private void insertOneRecord(DBCollection cl){

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);

cl.insert(insertor);

}

static private void insertSomeRecords(DBCollection cl) {

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);

}

static private void deleteOneRecord(DBCollection cl) {

BSONObject matcher = new BasicBSONObject();

matcher.put("Name","foo");

cl.delete(matcher);

}

static private void deleteSomeRecords(DBCollection cl) {

BSONObject matcher = new BasicBSONObject();

matcher.put("name", "test");

cl.delete(matcher);

}

static private void queryId(DBCollection cl) {

BSONObject matcher = new BasicBSONObject();

BSONObject condition = new BasicBSONObject();

condition.put("$gte", 0);

condition.put("$lte", 8);

matcher.put("Id",condition);

DBCursor records = cl.query(matcher,null,null,null);

System.out.println("Query Results:");

int count = 0;

while(records.hasNext()){

count++;

BSONObject record = records.getNext();

System.out.println("Record with Id from 0 to 8:" + record.get("Id").toString());

}

System.out.println("There are " + count + " records in the result set");

}

static private void updateId(DBCollection cl) {

BSONObject matcher = new BasicBSONObject();

BSONObject modifier = new BasicBSONObject();

BSONObject condition = new BasicBSONObject();

BSONObject operation = new BasicBSONObject();

condition.put("$gte", 0);

condition.put("$lte", 8);

matcher.put("Id",condition);

operation.put("Id",10);

modifier.put("$inc", operation);

cl.update(matcher,modifier,null);

}

}

</span>

运行代码,程序的输出为:

[plain] view
plaincopy

The CS test_cs is created

Collection Space: test_cs

The Collection test_cl is created

There are 0 record(s) in the collection

Inserting one record...

There are 1 record(s) in the collection

Inserting 5 records...

There are 6 record(s) in the collection

Deleting one record...

There are 5 record(s) in the collection

Deleting 5 records...

There are 0 record(s) in the collection

Inserting 6 records again...

There are 6 record(s) in the collection

Querying all records with the value of 'Id' from 0 to 8...

Query Results:

Record with Id from 0 to 8:0

Record with Id from 0 to 8:1

Record with Id from 0 to 8:2

Record with Id from 0 to 8:3

Record with Id from 0 to 8:4

There are 5 records in the result set

Updateing all the records, add 10 to all the 'Id'...

Querying all records with the value of 'Id' from 0 to 8...

Query Results:

There are 0 records in the result set

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

之后删除了包含“Name“域的值为”foo“的一条记录。

然后又删除了”Id“域的值为0-8的5条记录。

此时,集合里所有的记录都被删除。

所以又分两次重新插入了6条记录。

查询所有”Id“域的值为0-8的记录,返回5条记录,Id的值分别为0,1,2,3,4

将所有”Id”的值增加10,增加后的Id的值应为10,11,12,13,14

再次查询所有”Id“域的值为0-8的记录,返回0条记录。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: