Sequoiadb 測試體驗系列之四 – Java 開發

上一篇中嘗試了一下SequoiaDB的 shell控制檯的使用,研究了一下控制檯中匹配符、更新符和聚集符的使用。今天嘗試一下SequoiaDB官方提供的Java 驅動。

首先要從官方下載驅動程序,按照http://www.sequoiadb.com/document/1.8/developement/application/java/topics/java.html給出的信息搭建開發環境,也就是將jar包加入到工程中。

今天主要嘗試了一下Sequoiadb,CollectionSpace,DBCollection(爲毛CollectionSpace類名字前面就不加DB...)這幾個類給出的基本接口。實現了數據庫實例的創建,集合空間的創建,查詢,集合的創建,數據的插入,和數據集的插入等一系列操作。完整代碼如下:


[java] view plaincopy
  1. <span style="font-size:18px;">import java.util.ArrayList;  
  2. import java.util.List;  
  3.   
  4. import org.bson.BSONObject;  
  5. import org.bson.BasicBSONObject;  
  6.   
  7. import com.sequoiadb.base.CollectionSpace;  
  8. import com.sequoiadb.base.DBCollection;  
  9. import com.sequoiadb.base.DBCursor;  
  10. import com.sequoiadb.base.Sequoiadb;  
  11. import com.sequoiadb.exception.BaseException;  
  12.   
  13. public class BlogCollectionSpace {  
  14.   
  15.     static String CS_NAME = "test_cs";  
  16.     static String CL_NAME = "test_cl";  
  17.       
  18.     public static void main(String[] args){  
  19.         String host = "192.168.20.46";  
  20.         String port = "11810";  
  21.         String usr = "admin";  
  22.         String password = "admin";  
  23.           
  24.         Sequoiadb sdb = null;  
  25.           
  26.         //創建數據庫實例  
  27.         try{  
  28.             sdb = new Sequoiadb(host+ ":" + port, usr, password);  
  29.         } catch (BaseException e) {  
  30.             e.printStackTrace();  
  31.             System.exit(1);  
  32.         }  
  33.           
  34.         if(sdb.isCollectionSpaceExist(CS_NAME)){  
  35.             sdb.dropCollectionSpace(CS_NAME);  
  36.         }  
  37.           
  38.         //創建集合空間  
  39.         CollectionSpace cs = sdb.createCollectionSpace(CS_NAME);  
  40.           
  41.         if(sdb.isCollectionSpaceExist(CS_NAME) && cs.getName() == CS_NAME){  
  42.             System.out.println("The CS " + CS_NAME + "is created");  
  43.         }else {  
  44.             System.exit(2);  
  45.         }  
  46.           
  47.         //查詢數據庫中的集合空間  
  48.         DBCursor CSList = sdb.listCollectionSpaces();  
  49.         while (CSList.hasNext()){  
  50.             String name = (String) CSList.getNext().get("Name");  
  51.             System.out.println("Collection Space: " + name);  
  52.         }  
  53.           
  54.         if(cs.isCollectionExist(CL_NAME)){  
  55.             cs.dropCollection(CL_NAME);  
  56.         }  
  57.           
  58.         //創建集合  
  59.         DBCollection cl = cs.createCollection(CL_NAME);  
  60.           
  61.         if(cs.isCollectionExist(CL_NAME) && cl.getName() == CL_NAME){  
  62.             System.out.println("The Collection " + CL_NAME + " is created");  
  63.         }else {  
  64.             System.exit(3);  
  65.         }  
  66.           
  67.         System.out.println("Before inserting one record");  
  68.         System.out.println("There are " + cl.getCount() + " record(s) in the collection");  
  69.           
  70.         BSONObject insertor = null;  
  71.         insertor = new BasicBSONObject();  
  72.         BSONObject phone = new BasicBSONObject();  
  73.         insertor.put("Name""foo");  
  74.         insertor.put("Age"10);  
  75.         phone.put("home""123456789");  
  76.         phone.put("mobile""987654321");  
  77.         insertor.put("Phone", phone);  
  78.   
  79.         //插入包含記錄的BSONObject  
  80.         cl.insert(insertor);  
  81.         System.out.println("After inserting one record");  
  82.         System.out.println("There are " + cl.getCount() + " record(s) in the collection");  
  83.           
  84.           
  85.         System.out.println("Before inserting 5 records");  
  86.         System.out.println("There are " + cl.getCount() + " record(s) in the collection");  
  87.           
  88.           
  89.         List<BSONObject> list = null;  
  90.   
  91.         //創建一個包含五條記錄的數據集  
  92.         try {  
  93.             list = new ArrayList<BSONObject>(5);  
  94.             for (int i = 0; i < 5; i++) {  
  95.                 BSONObject obj = new BasicBSONObject();  
  96.                 BSONObject addressObj = new BasicBSONObject();  
  97.                 BSONObject phoneObj = new BasicBSONObject();  
  98.   
  99.                 addressObj.put("city""foo");  
  100.                 addressObj.put("province""bar");  
  101.   
  102.                 phoneObj.put("Type""Office");  
  103.                 phoneObj.put("Number""88888888");  
  104.   
  105.                 obj.put("name""test");  
  106.                 obj.put("Id", i);  
  107.                 obj.put("Phonenumber", phoneObj);  
  108.                 obj.put("Address", addressObj);  
  109.   
  110.                 list.add(obj);  
  111.             }  
  112.         } catch (Exception e) {  
  113.             System.out.println("Failed to create name list record.");  
  114.             e.printStackTrace();  
  115.         }  
  116.           
  117.         //在集合中插入數據集  
  118.         cl.bulkInsert(list, 1);  
  119.           
  120.         System.out.println("After inserting 5 record");  
  121.         System.out.println("There are " + cl.getCount() + " record(s) in the collection");  
  122.     }  
  123. }  
  124. </span>  


在運行上面的代碼前,通過控制檯shell查看一下數據庫的狀態:

[plain] view plaincopy
  1. <span style="font-size:18px;">> db.listCollectionSpaces()  
  2. Return 0 row(s).  
  3. Takes 0.1100s.  
  4. > db.listCollections()  
  5. Return 0 row(s).  
  6. Takes 0.1139s.</span>  

可以看出這是一個空數據庫,沒有任何集合空間和集合。運行代碼,程序的輸出爲:

[java] view plaincopy
  1. <span style="font-size:18px;">The CS test_csis created  
  2. Collection Space: test_cs  
  3. The Collection test_cl is created  
  4. Before inserting one record  
  5. There are 0 record(s) in the collection  
  6. After inserting one record  
  7. There are 1 record(s) in the collection  
  8. Before inserting 5 records  
  9. There are 1 record(s) in the collection  
  10. After inserting 5 record  
  11. There are 6 record(s) in the collection  
  12. </span>  

可以看出,上面的代碼在空數據庫中創建了一個名爲test_cs的集合空間,一個名爲test_cl的集合,並分兩次在集合中分別插入了1條和5條記錄,總共6條記錄。

最後,利用控制檯shell查詢一下數據庫的情況:

[plain] view plaincopy
  1. <span style="font-size:18px;">> db.listCollectionSpaces()  
  2. {  
  3.   "Name": "test_cs"  
  4. }  
  5. Return 1 row(s).  
  6. Takes 0.1291s.  
  7. > db.listCollections()  
  8. {  
  9.   "Name": "test_cs.test_cl"  
  10. }  
  11. Return 1 row(s).  
  12. Takes 0.1441s.  
  13. > testcs = db.getCS("test_cs")  
  14. localhost:11810.test_cs  
  15. Takes 0.1385s.  
  16. > testcl = testcs.getCL("test_cl")  
  17. localhost:11810.test_cs.test_cl  
  18. Takes 0.8262s.  
  19. > testcl.find()  
  20. {  
  21.   "_id": {  
  22.     "$oid": "53c621d0c5d00bea55f5a959"  
  23.   },  
  24.   "Age": 10,  
  25.   "Name": "foo",  
  26.   "Phone": {  
  27.     "home": "123456789",  
  28.     "mobile": "987654321"  
  29.   }  
  30. }  
  31. {  
  32.   "_id": {  
  33.     "$oid": "53c621939c74ef081d3e7d83"  
  34.   },  
  35.   "Address": {  
  36.     "city": "foo",  
  37.     "province": "bar"  
  38.   },  
  39.   "Id": 3,  
  40.   "Phonenumber": {  
  41.     "Number": "88888888",  
  42.     "Type": "Office"  
  43.   },  
  44.   "name": "test"  
  45. }  
  46. {  
  47.   "_id": {  
  48.     "$oid": "53c621939c74ef081d3e7d81"  
  49.   },  
  50.   "Address": {  
  51.     "city": "foo",  
  52.     "province": "bar"  
  53.   },  
  54.   "Id": 1,  
  55.   "Phonenumber": {  
  56.     "Number": "88888888",  
  57.     "Type": "Office"  
  58.   },  
  59.   "name": "test"  
  60. }  
  61. {  
  62.   "_id": {  
  63.     "$oid": "53c621939c74ef081d3e7d84"  
  64.   },  
  65.   "Address": {  
  66.     "city": "foo",  
  67.     "province": "bar"  
  68.   },  
  69.   "Id": 4,  
  70.   "Phonenumber": {  
  71.     "Number": "88888888",  
  72.     "Type": "Office"  
  73.   },  
  74.   "name": "test"  
  75. }  
  76. {  
  77.   "_id": {  
  78.     "$oid": "53c621939c74ef081d3e7d82"  
  79.   },  
  80.   "Address": {  
  81.     "city": "foo",  
  82.     "province": "bar"  
  83.   },  
  84.   "Id": 2,  
  85.   "Phonenumber": {  
  86.     "Number": "88888888",  
  87.     "Type": "Office"  
  88.   },  
  89.   "name": "test"  
  90. }  
  91. {  
  92.   "_id": {  
  93.     "$oid": "53c621939c74ef081d3e7d80"  
  94.   },  
  95.   "Address": {  
  96.     "city": "foo",  
  97.     "province": "bar"  
  98.   },  
  99.   "Id": 0,  
  100.   "Phonenumber": {  
  101.     "Number": "88888888",  
  102.     "Type": "Office"  
  103.   },  
  104.   "name": "test"  
  105. }  
  106. Return 6 row(s).  
  107. Takes 0.3921s.</span>  

現在的數據空中有了剛剛插入的test_cs集合空間,test_cl集合,和剛剛分兩次插入的6條數據。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章