The primary CRUD API used by the Python Client is that of a standard key/value store. You create and update documents by supplying a key and value. You retrieve or remove documents by supplying a key. For example, consider the JSON document that you'll find in the "beer-sample" bucket that's available when you install Couchbase Server and setup your cluster. The key "110fc0f765" is associated with the beer document below.
{ "name": "Sundog", "abv": 5.25, "ibu": 0, "srm": 0, "upc": 0, "type": "beer", "brewery_id": "110f1bb0ee", "updated": "2010-07-22 20:00:20", "description": "Sundog is an amber ale as deep as the copper glow of a Lake Michigan sunset. Its biscuit malt give Sundog a toasty character and a subtle malty sweetness. Sundog brings out the best in grilled foods, caramelized onions, nutty cheese, barbecue, or your favorite pizza.", "style": "American-Style Amber/Red Ale", "category": "North American Ale" }
Note that credentials and bucket selection would vary slightly for this example.
couchbase = Couchbase("http://192.168.56.2:8091/pools/default", "beer-sample", ""); bucket = couchbase["beer-sample"]
To retrieve this document, you simply call the Get method of the client.
var savedBeer = client.Get("110fc0f765");To retrieve this document, you simply call the get method of the bucket. The get method returns a tuple, where the third element is the value. The first is the status code and the second the CAS value for the key.
savedBeer = bucket.get("110fc0f765")[2]If you add a line to print the savedBeer to the console, you should see a JSON string that contains the data above.
savedBeer = client.get("110fc0f765") print savedBeer
To add a document, first create a JSON string.
newBeer = \ """ "name": "Old Yankee Ale", "abv": 5.00, "ibu": 0, "srm": 0, "upc": 0, "type": "beer", "brewery_id": "110a45622a", "updated": "2012-08-30 20:00:20", "description": ".A medium-bodied Amber Ale", "style": "American-Style Amber", "category": "North American Ale" """
For a key, we'll simply compute a SHA-1 hash of our document and then grab the first 10 characters. The exact mechanism by which you create your keys need only be consistent. If you are going to query documents by key (not just through views) you should choose predictable keys (e.g., beer_Old_Yankee_Ale).
import hashlib key = hashlib.sha1(newBeer).hexdigest()[0:10]
With this new key, the JSON document may easily be stored.
bucket.set(key, 0, 0, newBeer)The set method will replace a key if it exists or add it if it does not. There are also individual add and replace methods. There are four arguments to provide the set method. The first is the key and the fourth the value. The second and third arguments provided are the expiry and flags for the set operation. In the case above, the key will not expire, nor will it have any flags set on the data. For more on these topics please see the Python API docs.
Removing a document simply entails calling the delete method with the key to be removed.
bucket.delete(key)