Skip to main content

Python Client for Couchbase

Project description

Client for Couchbase.

Building and Installing

This only applies to building from source. If you are using a Windows installer then everything (other than the server) is already included. See below for windows snapshot releases.

Also note that these instructions apply to building from source. You can always get the latest supported release version from pypi.

If you have a recent version of pip, you may use the latest development version by issuing the following incantation

pip install git+git://github.com/couchbase/couchbase-python-client

Prerequisites

  • Couchbase Server (http://couchbase.com/download)

  • libcouchbase. version 2.10.0 or greater (Bundled in Windows installer)

  • libcouchbase development files.

  • Python development files

  • A C compiler (except on Windows)

Building

The following will compile the module locally; you can then test basic functionality including running the examples.

python setup.py build_ext --inplace

If your libcouchbase install is in an alternate location (for example, /opt/local/libcouchbase), you may add extra directives, like so

python setup.py build_ext --inplace \
    --library-dir /opt/local/libcouchbase/lib \
    --include-dir /opt/local/libcouchbase/include

Or you can modify the environment CFLAGS and LDFLAGS variables.

Installing

python setup.py install

Using

Authentication is handled differently depending on what version of Couchbase Server you are using:

Couchbase Server < 5.0

Each bucket can optionally have a password. You may omit the authenticator if you are only working with password-less buckets.

>>> from couchbase.cluster import Cluster, ClassicAuthenticator
>>> cluster = Cluster('couchbase://localhost')
>>> cluster.authenticate(ClassicAuthenticator(buckets={'bucket-name': 'password'}))
>>> bucket = cluster.open_bucket('bucket-name')

Couchbase Server >= 5.0

Role-Based Access Control (RBAC) provides discrete username and passwords for an application that allow fine-grained control. The authenticator is always required.

>>> from couchbase.cluster import Cluster, PasswordAuthenticator
>>> cluster = Cluster('couchbase://localhost')
>>> cluster.authenticate(PasswordAuthenticator('username', 'password'))
>>> bucket = cluster.open_bucket('bucket-name')

Here’s an example code snippet which sets a key and then reads it

>>> bucket.upsert("key", "value")
OperationResult<RC=0x0, Key=key, CAS=0x31c0e3f3fc4b0000>
>>> res = bucket.get("key")
>>> res
ValueResult<RC=0x0, Key=key, Value=u'value', CAS=0x31c0e3f3fc4b0000, Flags=0x0>
>>> res.value
u'value'
>>>

You can also use views

>>> resultset = bucket.query("beer", "brewery_beers", limit=5)
>>> resultset
View<Design=beer, View=brewery_beers, Query=Query:'limit=5', Rows Fetched=0>
>>> for row in resultset: print row.key
...
[u'21st_amendment_brewery_cafe']
[u'21st_amendment_brewery_cafe', u'21st_amendment_brewery_cafe-21a_ipa']
[u'21st_amendment_brewery_cafe', u'21st_amendment_brewery_cafe-563_stout']
[u'21st_amendment_brewery_cafe', u'21st_amendment_brewery_cafe-amendment_pale_ale']
[u'21st_amendment_brewery_cafe', u'21st_amendment_brewery_cafe-bitter_american']

Twisted API

The Python client now has support for the Twisted async network framework. To use with Twisted, simply import txcouchbase.connection instead of couchbase.bucket

from twisted.internet import reactor
from txcouchbase.bucket import Bucket

cb = Bucket('couchbase://localhost/default')
def on_upsert(ret):
    print "Set key. Result", ret

def on_get(ret):
    print "Got key. Result", ret
    reactor.stop()

cb.upsert("key", "value").addCallback(on_upsert)
cb.get("key").addCallback(on_get)
reactor.run()

# Output:
# Set key. Result OperationResult<RC=0x0, Key=key, CAS=0x9a78cf56c08c0500>
# Got key. Result ValueResult<RC=0x0, Key=key, Value=u'value', CAS=0x9a78cf56c08c0500, Flags=0x0>

The txcouchbase API is identical to the couchbase API, except that where the synchronous API will block until it receives a result, the async API will return a Deferred which will be called later with the result or an appropriate error.

GEvent API

from gcouchbase.bucket import Bucket

conn = Bucket('couchbase://localhost/default')
print conn.upsert("foo", "bar")
print conn.get("foo")

The API functions exactly like the normal Bucket API, except that the implementation is significantly different.

Asynchronous (Tulip) API

This module also supports Python 3.4/3.5 asynchronous I/O. To use this functionality, import the couchbase.experimental module (since this functionality is considered experimental) and then import the acouchbase module. The acouchbase module offers an API similar to the synchronous client:

import asyncio

import couchbase.experimental
couchbase.experimental.enable()
from acouchbase.bucket import Bucket


async def write_and_read(key, value):
    cb = Bucket('couchbase://10.0.0.31/default')
    await cb.connect()
    await cb.upsert(key, value)
    return await cb.get(key)

loop = asyncio.get_event_loop()
rv = loop.run_until_complete(write_and_read('foo', 'bar'))
print(rv.value)

PyPy

PyPy is an alternative high performance Python implementation. Since PyPy does not work well with C extension modules, this module will not work directly. You may refer to the alternate implementation based on the cffi module: https://github.com/couchbaselabs/couchbase-python-cffi

Other Examples

There are other examples in the examples directory. To run them from the source tree, do something like

PYTHONPATH=$PWD ./examples/bench.py -U couchbase://localhost/default

Building documentation

The documentation is using Sphinx and also needs the numpydoc Sphinx extension. In order for the documentation to build properly, the C extension must have been built, since there are embedded docstrings in there as well.

To build the documentation, go into the docs directory and run

make html

The HTML output can be found in docs/build/html/.

Alternatively, you can also build the documentation (after building the module itself) from the top-level directory:

python setup.py build_sphinx

Once built, the docs will be in in build/sphinx/html

Testing

For running the tests, you need the standard unittest module, shipped with Python. Additionally, the testresources package is required.

To run them, use either py.test, unittest or trial.

The tests need a running Couchbase instance. For this, a tests.ini file must be present, containing various connection parameters. An example of this file may be found in tests.ini.sample. You may copy this file to tests.ini and modify the values as needed.

The simplest way to run the tests is to declare a bucket_prefix in the tests.ini file and run the setup_tests.py script to create them for you.

python setup_tests.py

To run the tests:

nosetests

Support & Additional Resources

If you found an issue, please file it in our JIRA. You can ask questions in our forums or in the #libcouchbase channel on freenode.

The official documentation can be consulted as well for general Couchbase concepts and offers a more didactic approach to using the SDK.

License

The Couchbase Python SDK is licensed under the Apache License 2.0.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

couchbase-2.5.12.tar.gz (665.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

couchbase-2.5.12-cp38-cp38-win_amd64.whl (974.7 kB view details)

Uploaded CPython 3.8Windows x86-64

couchbase-2.5.12-cp38-cp38-win32.whl (893.7 kB view details)

Uploaded CPython 3.8Windows x86

couchbase-2.5.12-cp37-cp37m-win_amd64.whl (958.7 kB view details)

Uploaded CPython 3.7mWindows x86-64

couchbase-2.5.12-cp37-cp37m-win32.whl (875.2 kB view details)

Uploaded CPython 3.7mWindows x86

File details

Details for the file couchbase-2.5.12.tar.gz.

File metadata

  • Download URL: couchbase-2.5.12.tar.gz
  • Upload date:
  • Size: 665.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.0

File hashes

Hashes for couchbase-2.5.12.tar.gz
Algorithm Hash digest
SHA256 a215d17b680103dea6a31b122895e24cd0ffff42ac5885c1d60f70767cbce886
MD5 b9af9c3c6751598fc06bd4f9c9246e3e
BLAKE2b-256 02f8480bfab18a33b8aedd5e5cffa08aa12b105981c1671d7794cd4c5f188c8b

See more details on using hashes here.

File details

Details for the file couchbase-2.5.12-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: couchbase-2.5.12-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 974.7 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.0

File hashes

Hashes for couchbase-2.5.12-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 9409596aac317e7da7e49019c4bd1271de8007651e731b515c6b81fb66e4d474
MD5 9f55d0ba1abf85ae28e0a0bef2e9191e
BLAKE2b-256 3f7d9f6e79d4761e1afe611646f5da135a1d762905fd482dbc51e70973d3bb9e

See more details on using hashes here.

File details

Details for the file couchbase-2.5.12-cp38-cp38-win32.whl.

File metadata

  • Download URL: couchbase-2.5.12-cp38-cp38-win32.whl
  • Upload date:
  • Size: 893.7 kB
  • Tags: CPython 3.8, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.0

File hashes

Hashes for couchbase-2.5.12-cp38-cp38-win32.whl
Algorithm Hash digest
SHA256 200772f59a0ef243382d16f6438f8ae344086251a10ef7e45f3b1ac5bfcc8be3
MD5 72adef25a00fea55736fa49df4d25be5
BLAKE2b-256 eb1569e5d88c363e1a85b34a4a9f9e4e1cf5baa817f66a4a2f1171fb0b2268e4

See more details on using hashes here.

File details

Details for the file couchbase-2.5.12-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: couchbase-2.5.12-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 958.7 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.0

File hashes

Hashes for couchbase-2.5.12-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 f98977e97c446665aec93be1f143cedd60e0dcba8213ec3b8bd3f86162866b51
MD5 c63a0a79ecd9d00979b20ada648731d9
BLAKE2b-256 43e0ee4f731b09476cc4dc30074bb1cbf9c46906284acd2b183575fee8b31622

See more details on using hashes here.

File details

Details for the file couchbase-2.5.12-cp37-cp37m-win32.whl.

File metadata

  • Download URL: couchbase-2.5.12-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 875.2 kB
  • Tags: CPython 3.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.0

File hashes

Hashes for couchbase-2.5.12-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 7baab2e4917cc62bd3d3731ad6b1524e07a7db934110753566232d08af758d72
MD5 353a0e9595fe37f9627e406de41c4717
BLAKE2b-256 da42b2321bd5a2cd791c0e7cf38f5c934666b33b5362f53fcd8749717b95cae4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page