上一篇中嘗試了一下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);
//插入包含記錄的BSONObject
cl.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 created
Collection Space: test_cs
The Collection test_cl is created
Before inserting one record
There are 0 record(s) in the collection
After inserting one record
There are 1 record(s) in the collection
Before inserting 5 records
There are 1 record(s) in the collection
After inserting 5 record
There 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_cs
Takes 0.1385s.
> testcl = testcs.getCL("test_cl")
localhost:11810.test_cs.test_cl
Takes 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條數據。