Store.multi_get()
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
Store.multi_get()
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.
...
def display_row(row):
try:
print "Retrieved row:"
print "\tItem Type: %s" % row['itemType']
print "\tCategory: %s" % row['itemCategory']
print "\tClass: %s" % row['itemClass']
print "\tSize: %s" % row['itemSize']
print "\tColor: %s" % row['itemColor']
print "\tPrice: %s" % row['price']
print "\tInventory Count: %s" % row['inventoryCount']
except KeyError, ke:
logging.error("Row display failed. Bad key: %s" % ke.message)
def do_store_ops(store):
try:
shard_key_d = {"itemType" : "Hats",
"itemCategory" : "baseball",
"itemClass" : "longbill"}
row_list =
store.multi_get("myTable", # table name
False, # Retrieve only keys?
shard_key_d) # partial primary key
if not row_list:
logging.debug("Table retrieval failed")
else:
logging.debug("Table retrieval succeeded.")
for r in row_list:
display_row(r)
except IllegalArgumentException, iae:
logging.error("Table retrieval failed.")
logging.error(iae.message)
Notice in the previous example that
Store.multi_get()
returns the table rows in a simple Python list. To display the
rows, you simply iterate over the list in the same way you
would any Python list.