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:
key - the unique identifier for the document being created in the database
0 - CAS identifier, optionally used for checked updates (only if data hasn't changed), default 0 ignores this feature
0 - User custom data flags, default 0 ignores this feature
str(val) - the main value for this document key. This takes the iterated integer counter and converts it to a string before passing it along. You may submit other strings, without needing to convert.
The set function returns three attributes, stored into the setstatus variable:
0 - is the cache timeout for this document
1 - CAS identifier, optionally used for future updates
2 - User custom data flags
These values can be accessed as subscripts to the setstatus variable (e.g. setstatus, setstatus, setstatus). 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:
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.