multiGet()の使用
TableAPI.multiGet()
を使用すると、複数の行で同じシャード・キーが共有されている場合、それらの行を一度に取得できます。このメソッドに、完全なセットのシャード・キーを指定する必要があります。
取得セットがメモリーに完全に収まる場合にのみTableAPI.multiGet()
を使用します。
たとえば、次のように設計されている製品に関する情報を格納する表があるとします。
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
-
この場合、itemType
フィールドがHats
に設定され、itemCategory
フィールドがbaseball
に設定されているすべての行を取得できます。itemClass
、itemColor
およびitemSize
はこの問合せには使用されていないため、これは部分主キーを表すことに注意してください。
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
}
次のようにして、結果のリストを反復できます。
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();
}