SDK使用手册
SDK是为开发人员创建的一组软件工具程序,通过特定API接口对数据库进行操作,开发者能够更加灵活且高效地执行数据库操作,降低学习成本,极大提升开发效率。
l 支持执行Connect集群操作
l 支持表操作(创建/删除)
l 支持单条记录操作(查看/插入/删除/修改)
l 支持批量记录操作(查看/插入/删除/修改)
l 支持聚合操作
n Sum
n Sum0
n Count
n Max
n Min
n Countwithnull
可以直接使用pom从maven central下载dingo-sdk artifactory。
<dependency>
<groupId>io.dingodb</groupId>
<artifactId>dingo-client</artifactId>
<version>0.6.0-SNAPSHOT</version>
</dependency>
以下将提供两种使用方式供大家参考。
方式一
1. 定义Pojo类
import io.dingodb.client.annotation.DingoKey;
import io.dingodb.client.annotation.DingoRecord;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@DingoRecord(table = "acct_deal")
@Getter
@Setter
@ToString
public class AcctDeal {
@DingoKey
private String acct_no;
@DingoKey
private String deal_date;
private double acc_amount;
private double acc_count;
private double deb_amount;
private double deb_count;
private double cre_amount;
private double cre_count;
}
2. 创建表
String remoteHost = "coordinator:19181";
DingoClient dingoClient = new DingoClient(remoteHost);
dingoClient.open();
DingoOpCli dingoOpCli = new DingoOpCli.Builder(dingoClient).build();
boolean isOK = dingoOpCli.createTable(AcctDeal.class);
System.out.println("Create table Status: " + isOK);
3. 向表中插入数据
AcctDeal acctDeal = new AcctDeal();
acctDeal.setAcct_no("1001");
acctDeal.setDeal_date("2022-08-08");
acctDeal.setAcc_amount(100);
acctDeal.setAcc_count(10);
acctDeal.setDeb_amount(50);
acctDeal.setDeb_count(3);
acctDeal.setCre_amount(30);
acctDeal.setCre_count(3);
dingoOpCli.save(acctDeal);
4. 查询
l 查询数据
AcctDeal pojoAcctDeal = dingoOpCli.read(AcctDeal.class, new Object[]{"1001","2022-08-08"});
System.out.println(pojoAcctDeal );
5. 样本数据
mysql> select * from acc_deal limit 5;
+--------+------------+------------+-----------+------------+-----------+------------+-----------+
| ACC_NO | DEAL_DATE | ACC_AMOUNT | ACC_COUNT | DEB_AMOUNT | DEB_COUNT | CRE_AMOUNT | CRE_COUNT |
+--------+------------+------------+-----------+------------+-----------+------------+-----------+
| 1001 | 2022-08-08 | 100 | 5 | 50 | 2 | 20 | 2 |
| 1002 | 2022-08-09 | 150 | 6 | 60 | 3 | 30 | 3 |
| 1001 | 2022-08-15 | 200 | 6 | 50 | 4 | 40 | 2 |
| 1004 | 2022-08-20 | 140 | 4 | 55 | 3 | 70 | 2 |
| 1001 | 2022-08-25 | 180 | 8 | 60 | 3 | 80 | 4 |
+--------+------------+------------+-----------+------------+-----------+------------+-----------+
ACCO_NO: Account No--> primary key
DEAL_DATE: Deal Date--> primary key
ACC_AMOUNT: Trade amount about account
ACC_COUNT: Trade count about account
DEB_AMOUNT: Amount of debit
DEB_COUNT: Count of debit
CRE_AMOUNT: Amount of lender
CRE_COUNT: Count of lender
6. 删除表并关闭连接
isOK = dingoOpCli.dropTable(AcctDeal.class);
System.out.println("drop table Status:" + isOK + ".............");
dingoClient.close();
方式 二
1.创建表
String tableName = "testThread";
ColumnDefinition c1 = ColumnDefinition.builder().name("id").type("integer").precision(0).scale(0).nullable(false).primary(0).build();
ColumnDefinition c2 = ColumnDefinition.builder().name("name").type("varchar").precision(0).scale(0).nullable(false).primary(-1).build();
ColumnDefinition c3 = ColumnDefinition.builder().name("amount").type("double").precision(0).scale(0).nullable(false).primary(-2).build();
PartitionDetailDefinition detailDefinition = new PartitionDetailDefinition(null, null, Arrays.asList(new Object[]{30}));
PartitionRule partitionRule = new PartitionRule(null, null, Arrays.asList(detailDefinition));
TableDefinition tableDefinition = TableDefinition.builder()
.name(tableName)
.columns(Arrays.asList(c1, c2, c3))
.version(1)
.ttl(0)
.partition(partitionRule)
.engine(Common.Engine.ENG_ROCKSDB.name())
.build();
boolean isSuccess = dingoClient.createTable(tableDefinition);
System.out.println("create is success: " + isSuccess);
2. 插入数据
l 插入单条数据
Record record = new Record(tableDefinition.getColumns(), new Value[]{Value.get(1), Value.get("col1"), Value.get(1234.0)});
boolean upsertStatus = dingoClient.upsert(tableName, record);
l 插入多条数据
List<Record> records = new ArrayList<>();
for (int i = 0; i < 100; i++) {
records.add(new Record(tableDefinition.getColumns(), new Value[]{Value.get(i), Value.get("col" + i), Value.get(123.0 * i)}));
}
List<Boolean> upsertStatusList = dingoClient.upsert(tableName, records);
3. 查询
l 查询单条数据
Record record = dingoClient.get(tableName, new Key(Value.get(1)));
l 批量查询数据 1
List<Record> recordList = dingoClient.get(tableName, Arrays.asList(new Key(Value.get(1)), new Key(Value.get(2)), new Key(Value.get(3))));
l 批量查询数据 2
Iterator<Record> iterator = dingoClient.scan(tableName, new Key(Value.get(1)), new Key(Value.get(100)), true, true);
4. 聚合操作
KeyRangeCoprocessor.Aggregation o1 = KeyRangeCoprocessor.Aggregation.builder().operation(KeyRangeCoprocessor.AggType.SUM).columnName("amount").alias("amount_sum").build();
KeyRangeCoprocessor.Aggregation o2 = KeyRangeCoprocessor.Aggregation.builder().operation(KeyRangeCoprocessor.AggType.MAX).columnName("amount").alias("amount_max").build();
KeyRangeCoprocessor.Aggregation o3 = KeyRangeCoprocessor.Aggregation.builder().operation(KeyRangeCoprocessor.AggType.COUNT).columnName("amount").alias("amount_count").build();
l 带groupBy
Iterator<Record> scan = dingoClient.scan(tableName, new Key(Collections.emptyList()), new Key(Collections.emptyList()), true, true, Arrays.asList(o1,o2,o3), Arrays.asList("name"));
l 不带groupBy
Iterator<Record> scan = dingoClient.scan(tableName, new Key(Collections.emptyList()), new Key(Collections.emptyList()), true, true, Arrays.asList(o1,o2,o3));
5. 删除数据
l 删除单条数据
Boolean deleteStatus = dingoClient.delete(tableName, new Key(Value.get(1)));
l 批量删除数据 1
List<Boolean> deleteStatusList = dingoClient.delete(tableName, Arrays.asList(new Key(Value.get(1)), new Key(Value.get(2)), new Key(Value.get(3))));
l 批量删除数据 2
Long deleteNum = dingoClient.delete(tableName, new Key(Value.get(1)), new Key(Value.get(100)), true, true).getCount();
6. 删除表并关闭连接
isOK = dingoClient.dropTable(tableName);
System.out.println("drop table Status:" + isOK + ".............");
dingoClient.close();
DingoDB提供了一个全面强大的Java API,但在将数据从Java POJO映射到数据库时需要编写一定量的样板代码。为了降低映射POJO与DingoDB之间所需的代码量,并减少代码的脆弱性,引入了注解。
l Key
DingoKey是一个注解,用于将实体类的某个字段标记为键。具体来说,您可以在该字段上添加@DingoKey注解,以便在与实体相关联的数据库中使用它作为主键。这样做可以简化数据访问中的一些操作,例如获取、更新、删除特定实体。
@DingoKey
private String acct_no;
@DingoKey
private String deal_date;
l Column
在Java中,字段可以映射到数据库,而与数据库中的列是否可见无关。要实现这一点,您可以使用@DingoColumn注解来指定要映射到的列。通过这样做,您可以控制实体类的哪些字段与数据库进行交互,并且可以更灵活地控制数据的访问和操作。
@DingoColumn(name = "vrsn")
private int version;
这将把版本字段的值映射到 DingoDB 数据库中的 vrsn 列。如果数据库列的名称与 Java 字段的名称相同,则可以省略该列的名称,例如:
@DingoColumn
private int age;
这将在 DingoDB 数据库中生成 age 列。默认情况下,所有字段都会被映射到数据库中。您可以使用 @DingoExclude 注解来排除某些字段,也可以使用 @DingoColumn 注解来对字段进行重命名。例如:
@DingoRecord(table = "testSet")
public static class Test {
public int a;
public int b;
public int c;
public int d;
}
这将保存包含4列的记录,即 a、b、c 和 d。如果要仅保存字段 a、b、c,可以使用以下两种方式之一:
@DingoRecord(table = "testSet")
public static class Test {
public int a;
public int b;
public int c;
@DingoExclude
public int d;
}
如果字段同时使用了 @DingoExclude 注解,则该字段将不会被映射到数据库列中。您可以通过在 @DingoColumn 中指定列名来强制指定特定字段的列名:
@DingoRecord(table = "testSet")
public static class Test {
public int a;
public int b;
@DingoColumn(name = "longCname")
public int c;
public int d;
}
这将保存4个字段到数据库中,即 a、b、longCname 和 d。其中,longCname 字段被指定为对应的列名。