SDK驱动开发

SequoiaDB 巨杉数据库为应用提供通过SDK驱动进行数据库操作和集群操作的接口。目前支持SDK驱动开发的语言如下:

  • C 驱动
  • C++ 驱动
  • CSharp 驱动
  • JAVA 驱动
  • PHP 驱动
  • Python 驱动
  • REST 驱动

JAVA SDK驱动开发

本章节以 JAVA SDK 驱动开发实现数据库增、删、改、查功能为例,本示例程序使用 Eclipse 工具开发,数据库用户名/密码默认为 sdbadmin,192.168.81.134 是数据库的服务IP,11810 端口是数据库协调节点的服务端口。

获取 JAVA 驱动开发包

从 SequoiaDB 巨杉数据库官网下载对应操作系统版本的 JAVA 驱动开发包;解压驱动开发包,从目录中获取 sequoiadb-driver.jar 文件。

配置 Eclipse 开发环境

  1. 将 SequoiaDB 驱动开发包中的 sequoiadb-driver.jar 文件拷贝到工程文件目录下(建议将其放置在其他所有依赖库目录,如 lib 目录)

  2. 在 Eclipse 界面中,创建/打开开发工程

  3. 在 Eclipse 主窗口左侧的“Package Explore”窗口中,选择开发工程,并点击鼠标右键

  4. 在菜单中选择“properties”菜单项

  5. 在弹出的“property for project …”窗口中,选择“Java Build Path”->“Libraries”

  6. 点击“Add External JARs..”按钮,选择添加 sequoiadb.jar 到工程中;

  7. 点击“OK”完成环境配置。

连接数据库

  1. package com.sequoiadb.util;
  2. import com.sequoiadb.base.DBCursor;
  3. import com.sequoiadb.base.Sequoiadb;
  4. import com.sequoiadb.exception.BaseException;
  5. public class Sample {
  6. public static void main(String[] args) {
  7. String connString = "192.168.81.134:11810";
  8. try {
  9. // 建立 SequoiaDB 数据库连接
  10. Sequoiadb sdb = new Sequoiadb(connString, "sdbadmin", "sdbadmin");
  11. // 获取所有 Collection 信息,并打印出来
  12. DBCursor cursor = sdb.listCollections();
  13. try {
  14. while(cursor.hasNext()) {
  15. System.out.println(cursor.getNext());
  16. }
  17. } finally {
  18. cursor.close();
  19. }
  20. } catch (BaseException e) {
  21. System.out.println("Sequoiadb driver error, error description:" + e.getErrorType());
  22. }
  23. }
  24. }

插入数据

  1. package com.sequoiadb.util;
  2. import org.bson.BSONObject;
  3. import org.bson.BasicBSONObject;
  4. import com.sequoiadb.base.CollectionSpace;
  5. import com.sequoiadb.base.DBCollection;
  6. import com.sequoiadb.base.Sequoiadb;
  7. import com.sequoiadb.exception.BaseException;
  8. public class Sample {
  9. public static void main(String[] args) {
  10. String connString = "192.168.81.134:11810";
  11. try {
  12. Sequoiadb sdb = new Sequoiadb(connString, "sdbadmin", "sdbadmin");
  13. //创建school集合空间
  14. CollectionSpace cs = sdb.createCollectionSpace("school");
  15. //创建student集合
  16. DBCollection cl = cs.createCollection("student");
  17. // 创建一个插入的 bson 对象
  18. BSONObject obj = new BasicBSONObject();
  19. obj.put("id", 1);
  20. obj.put("name", "tom");
  21. obj.put("age", 24);
  22. cl.insert(obj);
  23. } catch (BaseException e) {
  24. System.out.println("Sequoiadb driver error, error description:" + e.getErrorType());
  25. }
  26. }
  27. }

查询数据

  1. package com.sequoiadb.util;
  2. import org.bson.BSONObject;
  3. import org.bson.util.JSON;
  4. import com.sequoiadb.base.CollectionSpace;
  5. import com.sequoiadb.base.DBCollection;
  6. import com.sequoiadb.base.DBCursor;
  7. import com.sequoiadb.base.Sequoiadb;
  8. import com.sequoiadb.exception.BaseException;
  9. public class Sample {
  10. public static void main(String[] args) {
  11. String connString = "192.168.81.134:11810";
  12. // 定义一个游标对象
  13. DBCursor cursor = null;
  14. try {
  15. Sequoiadb sdb = new Sequoiadb(connString, "sdbadmin", "sdbadmin");
  16. //获取school集合空间对象
  17. CollectionSpace cs = sdb.getCollectionSpace("school");
  18. //获取student集合对象
  19. DBCollection cl = cs.getCollection("student");
  20. //查询id为1的记录
  21. BSONObject queryCondition = (BSONObject) JSON.parse("{id:1}");
  22. //查询所有记录,并把查询结果放在游标对象中
  23. cursor = cl.query(queryCondition, null, null, null);
  24. //从游标中显示所有记录
  25. try {
  26. while (cursor.hasNext()) {
  27. BSONObject record = cursor.getNext();
  28. String name = (String) record.get("name");
  29. int age = (int) record.get("age");
  30. System.out.println("name = " + name);
  31. System.out.println("age = " + age);
  32. }
  33. } finally {
  34. if(cursor != null){
  35. cursor.close();
  36. }
  37. }
  38. } catch (BaseException e) {
  39. System.out.println("Sequoiadb driver error, error description:" + e.getErrorType());
  40. }
  41. }
  42. }

修改数据

  1. package com.sequoiadb.util;
  2. import org.bson.BSONObject;
  3. import org.bson.util.JSON;
  4. import com.sequoiadb.base.CollectionSpace;
  5. import com.sequoiadb.base.DBCollection;
  6. import com.sequoiadb.base.Sequoiadb;
  7. import com.sequoiadb.exception.BaseException;
  8. public class Sample {
  9. public static void main(String[] args) {
  10. String connString = "192.168.81.134:11810";
  11. try {
  12. Sequoiadb sdb = new Sequoiadb(connString, "sdbadmin", "sdbadmin");
  13. //获取school集合空间对象
  14. CollectionSpace cs = sdb.getCollectionSpace("school");
  15. //获取student集合对象
  16. DBCollection cl = cs.getCollection("student");
  17. //修改id为1记录的age值为30
  18. BSONObject queryCondition = (BSONObject) JSON.parse("{id:1}");
  19. BSONObject modifier = (BSONObject) JSON.parse("{$set:{age:30}}");
  20. cl.update(queryCondition, modifier, null);
  21. } catch (BaseException e) {
  22. System.out.println("Sequoiadb driver error, error description:" + e.getErrorType());
  23. }
  24. }
  25. }

删除数据

  1. package com.sequoiadb.util;
  2. import org.bson.BSONObject;
  3. import org.bson.util.JSON;
  4. import com.sequoiadb.base.CollectionSpace;
  5. import com.sequoiadb.base.DBCollection;
  6. import com.sequoiadb.base.Sequoiadb;
  7. import com.sequoiadb.exception.BaseException;
  8. public class Sample {
  9. public static void main(String[] args) {
  10. String connString = "192.168.81.134:11810";
  11. try {
  12. Sequoiadb sdb = new Sequoiadb(connString, "sdbadmin", "sdbadmin");
  13. //获取school集合空间对象
  14. CollectionSpace cs = sdb.getCollectionSpace("school");
  15. //获取student集合对象
  16. DBCollection cl = cs.getCollection("student");
  17. //删除id为1记录
  18. BSONObject queryCondition = (BSONObject) JSON.parse("{id:1}");
  19. cl.delete(queryCondition);
  20. } catch (BaseException e) {
  21. System.out.println("Sequoiadb driver error, error description:" + e.getErrorType());
  22. }
  23. }
  24. }