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

来源:互联网 发布:dnftgp角色数据不更新 编辑:程序博客网 时间:2024/05/23 18:13

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


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



[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.         try {  
  27.             sdb = new Sequoiadb(host + ":" + port, usr, password);  
  28.         } catch (BaseException e) {  
  29.             e.printStackTrace();  
  30.             System.exit(1);  
  31.         }  
  32.   
  33.         if (sdb.isCollectionSpaceExist(CS_NAME)) {  
  34.             sdb.dropCollectionSpace(CS_NAME);  
  35.         }  
  36.   
  37.         CollectionSpace cs = sdb.createCollectionSpace(CS_NAME);  
  38.   
  39.         if (sdb.isCollectionSpaceExist(CS_NAME) && cs.getName() == CS_NAME) {  
  40.             System.out.println("The CS " + CS_NAME + " is created");  
  41.         } else {  
  42.             System.exit(2);  
  43.         }  
  44.   
  45.         DBCursor CSList = sdb.listCollectionSpaces();  
  46.         while (CSList.hasNext()) {  
  47.             String name = (String) CSList.getNext().get("Name");  
  48.             System.out.println("Collection Space: " + name);  
  49.         }  
  50.   
  51.         if (cs.isCollectionExist(CL_NAME)) {  
  52.             cs.dropCollection(CL_NAME);  
  53.         }  
  54.   
  55.         DBCollection cl = cs.createCollection(CL_NAME);  
  56.   
  57.         if (cs.isCollectionExist(CL_NAME) && cl.getName() == CL_NAME) {  
  58.             System.out.println("The Collection " + CL_NAME + " is created");  
  59.         } else {  
  60.             System.exit(3);  
  61.         }  
  62.   
  63.         System.out.println("There are " + cl.getCount() + " record(s) in the collection");  
  64.         System.out.println("Inserting one record...");  
  65.           
  66.         insertOneRecord(cl);  
  67.           
  68.         System.out.println("There are " + cl.getCount() + " record(s) in the collection");  
  69.         System.out.println("Inserting 5 records...");  
  70.   
  71.         insertSomeRecords(cl);  
  72.           
  73.         System.out.println("There are " + cl.getCount() + " record(s) in the collection");  
  74.         System.out.println("Deleting one record...");  
  75.           
  76.         deleteOneRecord(cl);  
  77.           
  78.         System.out.println("There are " + cl.getCount() + " record(s) in the collection");  
  79.         System.out.println("Deleting 5 records...");  
  80.           
  81.         deleteSomeRecords(cl);  
  82.           
  83.         System.out.println("There are " + cl.getCount() + " record(s) in the collection");  
  84.         System.out.println("Inserting 6 records again...");  
  85.           
  86.         insertOneRecord(cl);  
  87.         insertSomeRecords(cl);  
  88.           
  89.         System.out.println("There are " + cl.getCount() + " record(s) in the collection");  
  90.           
  91.         System.out.println("Querying  all records with the value of 'Id' from 0 to 8...");  
  92.         queryId(cl);  
  93.           
  94.         System.out.println("Updateing all the records, add 10 to all the 'Id'...");  
  95.         updateId(cl);  
  96.           
  97.         System.out.println("Querying  all records with the value of 'Id' from 0 to 8...");  
  98.         queryId(cl);  
  99.           
  100.   
  101.     }  
  102.       
  103.       
  104.     static private void insertOneRecord(DBCollection cl){  
  105.         BSONObject insertor = null;  
  106.         insertor = new BasicBSONObject();  
  107.         BSONObject phone = new BasicBSONObject();  
  108.         insertor.put("Name""foo");  
  109.         insertor.put("Age"10);  
  110.         phone.put("home""123456789");  
  111.         phone.put("mobile""987654321");  
  112.         insertor.put("Phone", phone);  
  113.   
  114.         cl.insert(insertor);  
  115.     }  
  116.   
  117.       
  118.       
  119.     static private void insertSomeRecords(DBCollection cl) {  
  120.   
  121.         List<BSONObject> list = null;  
  122.   
  123.         try {  
  124.             list = new ArrayList<BSONObject>(5);  
  125.             for (int i = 0; i < 5; i++) {  
  126.                 BSONObject obj = new BasicBSONObject();  
  127.                 BSONObject addressObj = new BasicBSONObject();  
  128.                 BSONObject phoneObj = new BasicBSONObject();  
  129.   
  130.                 addressObj.put("city""foo");  
  131.                 addressObj.put("province""bar");  
  132.   
  133.                 phoneObj.put("Type""Office");  
  134.                 phoneObj.put("Number""88888888");  
  135.   
  136.                 obj.put("name""test");  
  137.                 obj.put("Id", i);  
  138.                 obj.put("Phonenumber", phoneObj);  
  139.                 obj.put("Address", addressObj);  
  140.   
  141.                 list.add(obj);  
  142.             }  
  143.         } catch (Exception e) {  
  144.             System.out.println("Failed to create name list record.");  
  145.             e.printStackTrace();  
  146.         }  
  147.   
  148.         cl.bulkInsert(list, 1);  
  149.     }  
  150.       
  151.     static private void deleteOneRecord(DBCollection cl) {  
  152.         BSONObject matcher = new BasicBSONObject();  
  153.         matcher.put("Name","foo");  
  154.         cl.delete(matcher);  
  155.     }  
  156.       
  157.     static private void deleteSomeRecords(DBCollection cl) {  
  158.         BSONObject matcher = new BasicBSONObject();  
  159.         matcher.put("name""test");  
  160.         cl.delete(matcher);  
  161.     }  
  162.       
  163.     static private void queryId(DBCollection cl) {  
  164.         BSONObject matcher = new BasicBSONObject();  
  165.         BSONObject condition = new BasicBSONObject();  
  166.         condition.put("$gte"0);  
  167.         condition.put("$lte"8);  
  168.         matcher.put("Id",condition);  
  169.         DBCursor records = cl.query(matcher,null,null,null);  
  170.         System.out.println("Query Results:");  
  171.         int count = 0;  
  172.         while(records.hasNext()){  
  173.             count++;  
  174.             BSONObject record = records.getNext();  
  175.             System.out.println("Record with Id from 0 to 8:" + record.get("Id").toString());  
  176.               
  177.         }  
  178.         System.out.println("There are " + count + " records in the result set");  
  179.     }  
  180.       
  181.     static private void updateId(DBCollection cl) {  
  182.         BSONObject matcher = new BasicBSONObject();  
  183.         BSONObject modifier = new BasicBSONObject();  
  184.         BSONObject condition = new BasicBSONObject();  
  185.         BSONObject operation = new BasicBSONObject();  
  186.         condition.put("$gte"0);  
  187.         condition.put("$lte"8);  
  188.         matcher.put("Id",condition);  
  189.         operation.put("Id",10);  
  190.         modifier.put("$inc", operation);  
  191.         cl.update(matcher,modifier,null);  
  192.     }  
  193.       
  194.       
  195.       
  196. }  
  197. </span>  

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

[plain] view plaincopy
  1. The CS test_cs is created  
  2. Collection Space: test_cs  
  3. The Collection test_cl is created  
  4. There are 0 record(s) in the collection  
  5. Inserting one record...  
  6. There are 1 record(s) in the collection  
  7. Inserting 5 records...  
  8. There are 6 record(s) in the collection  
  9. Deleting one record...  
  10. There are 5 record(s) in the collection  
  11. Deleting 5 records...  
  12. There are 0 record(s) in the collection  
  13. Inserting 6 records again...  
  14. There are 6 record(s) in the collection  
  15. Querying  all records with the value of 'Id' from 0 to 8...  
  16. Query Results:  
  17. Record with Id from 0 to 8:0  
  18. Record with Id from 0 to 8:1  
  19. Record with Id from 0 to 8:2  
  20. Record with Id from 0 to 8:3  
  21. Record with Id from 0 to 8:4  
  22. There are 5 records in the result set  
  23. Updateing all the records, add 10 to all the 'Id'...  
  24. Querying  all records with the value of 'Id' from 0 to 8...  
  25. Query Results:  
  26. 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条记录。

0 0
原创粉丝点击