Using multiGet()
TableAPI.multiGet()
allows you to retrieve multiple rows at once, so long as they all share the same shard keys. You must specify a full set of shard keys to this method.
Use TableAPI.multiGet()
only if your retrieval set will fit entirely in memory.
For example, suppose you have a table that stores information about products, which is designed like this:
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)
)
With tables containing data like this:
-
Row 1:
-
itemType: Hats
-
itemCategory: baseball
-
itemClass: longbill
-
itemColor: red
-
itemSize: small
-
price: 12.07
-
inventoryCount: 127
-
-
Row 2:
-
itemType: Hats
-
itemCategory: baseball
-
itemClass: longbill
-
itemColor: red
-
itemSize: medium
-
price: 13.07
-
inventoryCount: 201
-
-
Row 3:
-
itemType: Hats
-
itemCategory: baseball
-
itemClass: longbill
-
itemColor: red
-
itemSize: large
-
price: 14.07
-
inventoryCount: 39
-
In this case, you can retrieve all of the rows with their itemType
field set to Hats
and their itemCategory
field set to baseball
. Notice that this represents a partial primary key, because itemClass
, itemColor
and itemSize
are not used for this query.
package kvstore.basicExample;
...
import java.util.List;
import java.util.Iterator;
import oracle.kv.ConsistencyException;
import oracle.kv.KVStore;
import oracle.kv.RequestTimeoutException;
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. In this case, we are
// using a partial primary key.
PrimaryKey key = myTable.createPrimaryKey();
key.put("itemType", "Hats");
key.put("itemCategory", "baseball");
key.put("itemClass", "longbill");
List<Row> myRows = null;
try {
myRows = tableH.multiGet(key, null, null);
} catch (ConsistencyException ce) {
// The consistency guarantee was not met
} catch (RequestTimeoutException re) {
// The operation was not completed within the
// timeout value
}
You can then iterate over the resulting list as follows:
for (Row theRow: myRows) {
String itemType = theRow.get("itemType").asString().get();
String itemCategory = theRow.get("itemCategory").asString().get();
String itemClass = theRow.get("itemClass").asString().get();
String itemColor = theRow.get("itemColor").asString().get();
String itemSize = theRow.get("itemSize").asString().get();
Float price = theRow.get("price").asFloat().get();
Integer price = theRow.get("itemCount").asInteger().get();
}