Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

130 lines (90 sloc) 3.824 kb

Note

If you are using the 0.6.x series of Cassandra then get pycassa 0.3 from the Downloads section and read the documentation contained within. This README applies to the current state of pycassa which tracks Cassandra's development (work in-progress toward Cassandra 0.7).

pycassa

pycassa is a python client library for Apache Cassandra with the following features:

  1. Auto-failover single or thread-local connections
  2. Connection pooling
  3. A batch interface
  4. Simplified version of the Thrift interface
  5. A method to map an existing class to a Cassandra column family

Documentation

Documentation can be found here:

http://pycassa.github.com/pycassa/

It includes installation instructions, a tutorial, API documentation, and a change log.

Getting Help

IRC:

Mailing List:

Requirements

thrift: http://incubator.apache.org/thrift/
Cassandra: http://cassandra.apache.org

To install thrift's python bindings:

easy_install thrift05

Installation

If easy_install is available, you can use:

easy_install pycassa

The simplest way to install manually is to copy the pycassa directories to your program. If you want to install, run setup.py as a superuser.

python setup.py install

Connecting

All functions are documented with docstrings. To read usage documentation, you can use help:

>>> import pycassa
>>> help(pycassa.ColumnFamily.get)

To get a connection pool, pass a Keyspace and an optional list of servers:

>>> pool = pycassa.connect('Keyspace1') # Defaults to connecting to the server at 'localhost:9160'
>>> pool = pycassa.connect('Keyspace1', ['192.168.2.10:9160'])

See the tutorial for more details.

Basic Usage

To use the standard interface, create a ColumnFamily instance.

>>> pool = pycassa.connect('Keyspace1')
>>> cf = pycassa.ColumnFamily(pool, 'Standard1')
>>> cf.insert('foo', {'column1': 'val1'})
1261349837816957
>>> cf.get('foo')
{'column1': 'val1'}

insert() also acts to update values:

>>> cf.insert('foo', {'column1': 'val2'})
1261349910511572
>>> cf.get('foo')
{'column1': 'val2'}

You may insert multiple columns at once:

>>> cf.insert('bar', {'column1': 'val3', 'column2': 'val4'})
1261350013606860
>>> cf.multiget(['foo', 'bar'])
{'foo': {'column1': 'val2'}, 'bar': {'column1': 'val3', 'column2': 'val4'}}
>>> cf.get_count('bar')
2

get_range() returns an iterable. Call it with list() to convert it to a list.

>>> list(cf.get_range())
[('bar', {'column1': 'val3', 'column2': 'val4'}), ('foo', {'column1': 'val2'})]
>>> list(cf.get_range(row_count=1))
[('bar', {'column1': 'val3', 'column2': 'val4'})]

You can remove entire keys or just a certain column.

>>> cf.remove('bar', columns=['column1'])
1261350220106863
>>> cf.get('bar')
{'column2': 'val4'}
>>> cf.remove('bar')
1261350226926859
>>> cf.get('bar')
Traceback (most recent call last):
...
cassandra.ttypes.NotFoundException: NotFoundException()
Jump to Line
Something went wrong with that request. Please try again.