To retrieve a single row from the store:
Create a store handle and open it.
Construct a Python dictionary. primary key. Each name/value pair in the dictionary must correspond to the primary key and value for the row that you want to retrieve. In this case, the full primary key must be present in the dictionary.
Retrieve the row using Store.get()
.
This performs the store read operation.
The retrieved row is a Python dictionary. Individual items in the dictionary can be retrieved as you would for any Python dictionary.
For example, in Writing Rows to a Table in the Store we showed a trivial example of storing a table row to the store. The following trivial example shows how to retrieve that row.
from nosqldb import Factory from nosqldb import IllegalArgumentException from nosqldb import ProxyConfig from nosqldb import StoreConfig import logging import os import sys # locations where our store and proxy can be found kvlite = 'localhost:5000' proxy = 'localhost:7010' # set logging level to debug and log to stdout def setup_logging(): rootLogger = logging.getLogger() rootLogger.setLevel(logging.DEBUG) logger = logging.StreamHandler(sys.stdout) logger.setLevel(logging.DEBUG) formatter = logging.Formatter('\t%(levelname)s - %(message)s') logger.setFormatter(formatter) rootLogger.addHandler(logger) # configure and open the store def open_store(): kvstoreconfig = StoreConfig('kvstore', [kvlite]) return Factory.open(proxy, kvstoreconfig) def display_row(row): try: print "Retrieved row:" print "\tItem: %s" % row['item'] print "\tDescription: %s" % row['description'] print "\tCount: %s" % row['count'] print "\tPercentage: %s" % row['percentage'] print "\n" except KeyError, ke: logging.error("Row display failed. Bad key: %s" % ke.message) def do_store_ops(store): try: primary_key_d = {"item" : "bolts"} row = store.get("myTable", primary_key_d) if not row: logging.debug("Row retrieval failed") else: logging.debug("Row retrieval succeeded.") display_row(row) except IllegalArgumentException, iae: logging.error("Row retrieval failed.") logging.error(iae.message) return except KeyError, ke: logging.error("Row display failed. Bad key: %s" % ke.message) if __name__ == '__main__': setup_logging() store = open_store() do_store_ops(store) store.close()
In Writing Rows to a Child Table we showed how to populate a child table with data. To retrieve that data, you must specify the primary key used for the parent table row, as well as the primary key for the child table row. For example:
... def do_store_ops(store): try: primary_key_c = {"item" : "bolts", "itemSKU" : "1392610"} row = store.get("myInventory.itemDetails", primary_key_c) if not row: logging.debug("Row retrieval failed") else: logging.debug("Row retrieval succeeded.") print row except IllegalArgumentException, iae: logging.error("Row retrieval failed.") logging.error(iae.message) return except KeyError, ke: logging.error("Row display failed. Bad key: %s" % ke.message)
For information on how to iterate over nested tables, see Iterating with Nested Tables.