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

来源:互联网 发布:淘宝虚假交易怎么恢复 编辑:程序博客网 时间:2024/05/29 03:49

上一篇中尝试了一下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条数据。
0 0
原创粉丝点击