Retrieving a Single Row

To retrieve a single row from the store:

  1. Construct a handle for the table from which you want to read. You do this by retrieving a TableAPI class instance using KVStore.getTableAPI(). You then use that instance to retrieve the desired table handle using TableAPI.getTable(). This returns a Table class instance.

    Note:

    TableAPI.getTable() is an expensive call that requires server side access. From a performance point of view, it is a mistake to call this method whenever you need a table handle. Instead, call this method for all relevant tables in the set up section of your code, and then reuse those handles throughout your application.

  2. Use the Table instance retrieved in the previous step to create a PrimaryKey class instance. In this case, the key you create must be the entire primary key.

  3. Retrieve the row using TableAPI.get(). This performs a store read operation.

  4. Retrieve individual fields from the row using the Row.get() method.

For example, in Writing Rows to a Table in the Store we showed a trivial example of storing a table row to the store. The following trivial example shows how to retrieve that row.

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(); 

Retrieve a Child Table

In Writing Rows to a Child Table we showed how to populate a child table with data. To retrieve that data, you must specify the primary key used for the parent table row, as well as the primary key for the child table row. For example:

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("myInventory.itemDetails");

// 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(); 

For information on how to iterate over nested tables, see Iterating with Nested Tables.