Search:

Search all manuals
Search this manual
Manual
Membase and Python Tutorial
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
4 Minimal Examples
Chapter Sections
Chapters

4.2. Saving Data

The following is a minimal script connecting to Membase and inserting some sample data. A unique key is auto generated and a sequential integer is stored as the value. 100 inserts are made into the default data bucket. Remember, Python cares about whitespace indents for creating its loops and recognizing longs lines broken into shorter ones.

from couchbase.couchbaseclient import VBucketAwareCouchbaseClient as client
import uuid

server = "http://localhost:8091/pools/default"
v = client(server,"default","")

for val in range(0,100):
  key = uuid.uuid4().bytes.encode('hex')
  setstatus = v.set(key, 0, 0, str(val))

print setstatus
v.done()

The first two lines described above enable two modules:

from couchbase.couchbaseclient import VBucketAwareCouchbaseClient as client
import uuid

The first import statement gives you access to the couchbase module API, while also using a simple alias to make things easier to read further down. The second import statement enables the uuid module which provides unique ID that will be used for the key value in the bucket. Hex values and special UUIDs are not required for a key, but are used as an example.

The next line sets the server connection string for to a node. The "default" bucket is used which does not require authentication.

server = "http://localhost:8091/pools/default"

Next comes the important step - connecting to the node and passing along all the connection information:

v = client(server,"default","")

The v object is a connection to the node as described in the server variable from the previous step. The second value passed to the connection is the name of the bucket you wish to connect to. In this case default is used to keep the example simple.

Only a few more lines to go. The next line is simply for generating a basic Python loop so you can generate a number of test entries.

for val in range(0,100):

The loop is set to iterate through 101 times and then stop. Change the 100 in (0,100) to how many loops you would like to have.

The next line sets the key to be a unique ID number, encoded into hex. This is only used for this example script. Your unique keys may be customized as needed.

key = uuid.uuid4().bytes.encode('hex')

Onto the next line, where all the previous setup delivers the goods to the database:

setstatus = v.set(key, 0, 0, str(val))

The set function requires four specific properties to be passed to it:

The set function returns three attributes, stored into the setstatus variable:

These values can be accessed as subscripts to the setstatus variable (e.g. setstatus[0], setstatus[1], setstatus[2]). Or you can print them all at once, as shown:

print setstatus
(715927478, 100, '')

Finally, when all finished sending the data, you must disconnect by using the done function:

v.done()

This releases all open connections and prints a notice showing they are all closed up. If you do not issue the v.done() call your Python script may never finish and most likely hang until the process is manually killed.