The OBKV-Table Java client supports single-row and batch deletion. Choose the appropriate API based on your requirements.
Delete APIs
The following ObTableClient APIs support deletion:
API |
Description |
|---|---|
Delete delete(String tableName) |
Deletes a row. You can set deletion conditions. |
Delete examples
Prerequisites
Before you delete data, make sure that:
Your ObTableClient is initialized.
You have created a relational table in your database. The examples in this topic use the following table:
CREATE TABLE `employees` ( `employeeId` int(11) NOT NULL, `name` varchar(50) DEFAULT NULL, `department` varchar(50) DEFAULT NULL, `position` varchar(50) DEFAULT NULL, `workYear` int(11) DEFAULT NULL, `comment` varchar(50) DEFAULT NULL, PRIMARY KEY (`employeeId`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Single-row deletion
delete
The following example deletes the row whose primary key employeeId is 1001.
// Assume the following data already exists in the database
/*
+------------+----------+------------+----------+----------+---------+
| employeeId | name | department | position | workYear | comment |
+------------+----------+------------+----------+----------+---------+
| 1001 | zhangsan | R&D | dev | 10 | NULL |
+------------+----------+------------+----------+----------+---------+
*/
private static void deleteRow(ObTableClient obkvHandle) throws Exception {
Row rowKey = row().add(colVal("employeeId", 1001));
obkvHandle.delete("employees")
.setRowKey(rowKey)
.execute();
}
The following example deletes the row whose primary key employeeId is 1001 only if workYear is greater than or equal to 3.
// Assume the following data already exists in the database
/*
+------------+----------+------------+----------+----------+---------+
| employeeId | name | department | position | workYear | comment |
+------------+----------+------------+----------+----------+---------+
| 1001 | zhangsan | R&D | dev | 10 | NULL |
+------------+----------+------------+----------+----------+---------+
*/
private static void deleteRowWithFilter(ObTableClient obkvHandle) throws Exception {
Row rowKey = row().add(colVal("employeeId", 1001));
ObTableValueFilter filter = compareVal(ObCompareOp.GE, "workYear", 3);
obkvHandle.delete("employees")
.setRowKey(rowKey)
.setFilter(filter)
.execute();
}
Batch deletion
batchDelete
The following example deletes two rows whose primary keys employeeId are 1001 and 1002.
// Assume the following data already exists in the database
/*
+------------+----------+------------+----------+----------+---------+
| employeeId | name | department | position | workYear | comment |
+------------+----------+------------+----------+----------+---------+
| 1001 | zhangsan | R&D | dev | 10 | NULL |
| 1002 | lisi | R&D | dev | 10 | NULL |
+------------+----------+------------+----------+----------+---------+
*/
private static void batchDeleteRow(ObTableClient obkvHandle) throws Exception {
Row rowKey1 = row().add(colVal("employeeId", 1001));
Row rowKey2 = row().add(colVal("employeeId", 1002));
Delete deleteOp1 = delete().setRowKey(rowKey1);
Delete deleteOp2 = delete().setRowKey(rowKey2);
BatchOperationResult res = obkvHandle.batchOperation("employees")
.addOperation(deleteOp1,deleteOp2)
.execute();
System.out.println(res.hasError());
}
