TableAPI.multiGet()
allows you to retrieve multiple rows at once, so long as they all
share the same shard keys. You can specify a full set of
shard keys, or a partial set.
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:
## Enter into table creation mode table create -name myTable ## Now add the fields add-field -type STRING -name itemType add-field -type STRING -name itemCategory add-field -type STRING -name itemClass add-field -type STRING -name itemColor add-field -type STRING -name itemSize add-field -type FLOAT -name price add-field -type INTEGER -name inventoryCount primary-key -field itemType -field itemCategory -field itemClass -field itemColor -field itemSize shard-key -field itemType -field itemCategory -field itemClass ## Exit table creation mode exit
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 CLI's 'table create' command.
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");
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 (Rows 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();
}