The OBKV-Table Java client supports single-row and batch data deletion. You can choose the appropriate interface based on your requirements.
Supported deletion interfaces
The following interfaces of ObTableClient support deletion:
API |
Description |
|---|---|
Delete delete(String tableName) |
Deletes a row of data. You can set deletion conditions. |
Deletion examples
Prerequisites
Before you delete data, make sure the following prerequisites are met:
- Your ObTableClient has been initialized.
- You have created a relational table in your database. The following example uses this 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 sample code deletes a row of data by specifying the primary key employeeId as 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 sample code deletes the row whose employeeId is 1001 if its 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 sample code deletes two rows of data by specifying employeeId as 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());
}