TableAPI.delete()メソッドを使用して、1つの行をストアから削除します。行は、Table.createPrimaryKey()メソッドを使用して取得されるPrimaryKeyに基づいて削除されます。削除される前に、行が指定されたバージョンに一致することを求めることもできます。これを行うには、TableAPI.deleteIfVersion()メソッドを使用します。バージョンについては、「行バージョンの使用」で説明します。
行を削除する場合、ストアへの書込み操作で発生するのと同じ例外に対処する必要があります。これらの例外の概要は、「書込みの例外」を参照してください。
package kvstore.basicExample;
import oracle.kv.KVStore;
import oracle.kv.table.PrimaryKey;
import oracle.kv.table.Table;
import oracle.kv.table.TableAPI;
...
// KVStore handle creation is omitted for brevity
...
TableAPI tableH = kvstore.getTableAPI();
// The name you give to getTable() must be identical
// to the name that you gave the table when you created
// the table using the CREATE TABLE DDL statement.
Table myTable = tableH.getTable("myTable");
// Get the primary key for the row that we want to delete
PrimaryKey primaryKey = myTable.createPrimaryKey();
primaryKey.put("item", "Bolts");
// Delete the row
// This performs a store write operation
tableH.delete(primaryKey, null, null);
複数の行でがすべてシャード・キー値を共有している場合、1回の原子的操作で一度に複数の行を削除できます。シャード・キーが少なくとも主キーのサブセットではあることを思い出してください。その結果として、複数削除を実行するために、(シャード・キーである)部分主キーを使用します。
複数の行を一度に削除するには、TableAPI.multiDelete()メソッドを使用します。
たとえば、次のような表を作成したとします。
CREATE TABLE myTable (
itemType STRING,
itemCategory STRING,
itemClass STRING,
itemColor STRING,
itemSize STRING,
price FLOAT,
inventoryCount INTEGER,
PRIMARY KEY (SHARD(itemType, itemCategory, itemClass), itemColor,
itemSize)
)
次のようなデータを含む表:
行1:
| itemType: Hats |
| itemCategory: baseball |
| itemClass: longbill |
| itemColor: red |
| itemSize: small |
| price: 12.07 |
| inventoryCount: 127 |
行2:
| itemType: Hats |
| itemCategory: baseball |
| itemClass: longbill |
| itemColor: red |
| itemSize: medium |
| price: 13.07 |
| inventoryCount: 201 |
行3:
| itemType: Hats |
| itemCategory: baseball |
| itemClass: longbill |
| itemColor: red |
| itemSize: large |
| price: 14.07 |
| inventoryCount: 39 |
この場合、次のように、部分主キーHats、baseball、longbillを共有するすべての行を削除できます。
package kvstore.basicExample;
import oracle.kv.KVStore;
import oracle.kv.table.PrimaryKey;
import oracle.kv.table.Table;
import oracle.kv.table.TableAPI;
...
// KVStore handle creation is omitted for brevity
...
TableAPI tableH = kvstore.getTableAPI();
// The name you give to getTable() must be identical
// to the name that you gave the table when you created
// the table using the CREATE TABLE DDL statement.
Table myTable = tableH.getTable("myTable");
// Get the primary key for the row that we want to delete
PrimaryKey primaryKey = myTable.createPrimaryKey();
primaryKey.put("itemType", "Hats");
primaryKey.put("itemCategory", "baseball");
primaryKey.put("itemClass", "longbill");
// Exception handling omitted
tableH.multiDelete(primaryKey, null, null);