TableAPI.multiGet()
を使用すると、複数の行で同じ主キーが共有されている場合、それらの行を一度に取得できます。主キー一式または主キーの一部を指定できます。
取得セットがメモリーに完全に収まる場合にのみTableAPI.multiGet()
を使用します。
たとえば、次のように設計されている製品に関する情報を格納する表があるとします。
## 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
次のようなデータを含む表:
行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.ArrayList; 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"); ArrayList<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 (Iterator<Row> iter = myRows.iterator(); iter.hasNext();) { Row theRow = iter.next(); 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(); }