単一行の取得

子表の取得

ストアから単一行を取得するには、次の手順を実行します。

  1. 読み取る表のハンドルを作成します。そのためには、KVStore.getTableAPI()を使用してTableAPIクラス・インスタンスを取得します。次に、そのインスタンスとTableAPI.getTable()を使用して、目的の表ハンドルを取得します。これにより、Tableクラス・インスタンスが返されます。

    注意

    TableAPI.getTable()は、サーバー側アクセスを必要とする消費が多いコールです。パフォーマンスの観点では、表ハンドルが必要なたびにこのメソッドを呼び出す方法は間違っています。かわりに、コードの設定セクションで関連するすべての表に対してこのメソッドを呼び出し、それらのハンドルをアプリケーションを通して再利用します。

  2. 前の手順で取得されたTableインスタンスを使用して、PrimaryKeyクラス・インスタンスを作成します。この場合、作成するキーは主キーである必要があります。

  3. TableAPI.get()を使用して行を取得します。これにより、ストアの読取り操作が実行されます。

  4. Row.get()メソッドを使用して、行から個別のフィールドを取得します。

たとえば、「ストアへの行の書込み」では、ストアに表の行を格納する簡単な例を示しました。次の簡単な例では、行の取得方法を示します。

package kvstore.basicExample;

import oracle.kv.KVStore;
import oracle.kv.table.PrimaryKey;
import oracle.kv.table.Row;
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");

// Construct the PrimaryKey. This is driven by your table
// design, which designated one or more fields as 
// being part of the table's primary key. In this
// case, we have a single field primary key, which is the
// 'item' field. Specifically, we want to retrieve the
// row where the 'item' field contains 'Bolts'.
PrimaryKey key = myTable.createPrimaryKey();
key.put("item", "Bolts");

// Retrieve the row. This performs a store read operation.
// Exception handling is skipped for this trivial example.
Row row = tableH.get(key, null);

// Now retrieve the individual fields from the row.
String item = row.get("item").asString.get();
String description = row.get("description").asString.get();
Integer count = row.get("count").asInteger.get();
Double percentage = row.get("percentage").asDouble.get(); 

子表の取得

「子表への行の書込み」では、子表にデータを移入する方法を示しました。そのデータを取得するには、親表の行に使用される主キー、および子表の行の主キーを指定する必要があります。次に例を示します。

package kvstore.basicExample;

import oracle.kv.KVStore;
import oracle.kv.table.PrimaryKey;
import oracle.kv.table.Row;
import oracle.kv.table.Table;
import oracle.kv.table.TableAPI;

...

// KVStore handle creation is omitted for brevity

...

TableAPI tableH = kvstore.getTableAPI();

// We omit retrieval of the parent table because it is not required.
Table myChildTable = tableH.getTable("myTable.myChildTable");

// Construct the PrimaryKey. This key must contain the primary key
// from the parent table row, as well as the primary key from the 
// child table row that you want to retrieve.
PrimaryKey key = myChildTable.createPrimaryKey();
key.put("itemCategory", "Bolts");
key.put("itemSKU", "1392610");

// Retrieve the row. This performs a store read operation.
// Exception handling is skipped for this trivial example.
Row row = tableH.get(key, null);

// Now retrieve the individual fields from the row.
String description = row.get("itemDescription").asString().get();
Float price = row.get("price").asFloat().get();
Integer invCount = row.get("inventoryCount").asInteger().get(); 

ネスト表を反復する方法の詳細は、「MultiRowOptionsを使用したネスト表の取得」を参照してください。