When performing multi-key operations in the store, you can
specify a range of rows to operate upon. You do this using
the FieldRange
class,
which is accepted by any of the methods which perform bulk
reads. This
class
is used to restrict the selected rows to
those matching a range of field values.
For example, suppose you defined a table like this:
CREATE TABLE myTable ( surname STRING, familiarName STRING, userID STRING, phonenumber STRING, address STRING, email STRING, dateOfBirth STRING, PRIMARY KEY (SHARD(surname, familiarName), userID) )
The surname
contains a person's family name,
such as Smith
. The familiarName
contains their common name, such as Bob
,
Patricia
, Robert
, and so
forth.
Given this, you could perform operations for all the rows
related to users with a surname of Smith
,
but we can limit the result set to just those users with
familiar names that fall alphabetically between
Bob
and Patricia
by specifying a field range.
A FieldRange
is created using
the FieldRange
class, which you
provide to the method you are using to perform the
multi-read operation using the MultiRowOptions
class. This class requires the name of the primary key
field for which you want to set the range, as well the
range values, including whether they are inclusive.
In this case, we will define the start of the key range using the string "Bob" and the end of the key range to be "Patricia". Both ends of the key range will be inclusive.
In this example, we use TableIterator
,
but we could just as easily use this range on any multi-row
read operation, such as the
Store.multi_get()
method.
def display_row(row): try: print "Retrieved row:" print "\tSurname: %s" % row['surname'] print "\tFamiliar Name: %s" % row['familiarName'] print "\tUser ID: %s" % row['userID'] print "\tPhone: %s" % row['phonenumber'] print "\tAddress: %s" % row['address'] print "\tEmail: %s" % row['email'] print "\tDate of Birth: %s" % row['dateOfBirth'] print "\n" except KeyError, ke: logging.error("Row display failed. Bad key: %s" % ke.message) def do_store_ops(store): key_d = {'surname' : 'Smith'} field_range = FieldRange({ ONDB_FIELD : "familiarName", ONDB_START_VALUE : "Bob", ONDB_END_VALUE : "Patricia", # These next two are the default values, # so are not really needed. ONDB_START_INCLUSIVE : True, ONDB_END_INCLUSIVE : True }) mro = MultiRowOptions({ONDB_FIELD_RANGE : field_range}) try: row_list = store.table_iterator("myTable", key_d, False, mro) 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)