multi_get()の使用方法

Store.multi_get()を使用すると、複数の行で同じシャード・キーが共有されている場合、それらの行を一度に取得できます。このメソッドに、完全なセットのシャード・キーを指定する必要があります。

取得セットがメモリーに完全に収まる場合にのみStore.multi_get()を使用します。

たとえば、次のように設計されている製品に関する情報を格納する表があるとします。

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に設定されているすべての行を取得できます。itemClassitemColorおよびitemSizeはこの問合せには使用されていないため、これは部分主キーを表すことに注意してください。

...

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
                            shard_key_d, # partial primary key
                            False)       # Retrieve only keys?
        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) 

前述の例では、Store.multi_get()から単純なPythonリストの表の行が返されます。行を表示するには、単純に、Pythonリストの場合と同じ方法でリストを反復処理します。