pycassa.index – Secondary Index Tools

Tools for using Cassandra’s secondary indexes.

Example Usage:

>>> from pycassa.columnfamily import ColumnFamily
>>> from pycassa.pool import ConnectionPool
>>> from pycassa.index import *
>>>
>>> pool = ConnectionPool('Keyspace1')
>>> users = ColumnFamily(pool, 'Users')
>>> state_expr = create_index_expression('state', 'Utah')
>>> bday_expr = create_index_expression('birthdate', 1970, GT)
>>> clause = create_index_clause([state_expr, bday_expr], count=20)
>>> for key, user in users.get_indexed_slices(clause):
...     print user['name'] + ",", user['state'], user['birthdate']
John Smith, Utah 1971
Mike Scott, Utah 1980
Jeff Bird, Utah 1973

This gives you all of the rows (up to 20) which have a ‘birthdate’ value above 1970 and a state value of ‘Utah’.

pycassa.index.EQ = 0

Equality (==) operator for index expressions

pycassa.index.GT = 2

Greater-than (>) operator for index expressions

pycassa.index.GTE = 1

Greater-than-or-equal (>=) operator for index expressions

pycassa.index.LT = 4

Less-than (<) operator for index expressions

pycassa.index.LTE = 3

Less-than-or-equal (<=) operator for index expressions

static index.create_index_expression(column_name, value[, op=EQ])

Constructs an IndexExpression to use in an IndexClause

The expression will be applied to the column with name column_name. A match will only occur if the operator specified with op returns True when used on the actual column value and the value parameter.

The default operator is EQ, which tests for equality.

static index.create_index_clause(expr_list[, start_key][, count])

Constructs an IndexClause for use with get_indexed_slices()

expr_list should be a list of IndexExpression objects that must be matched for a row to be returned. At least one of these expressions must be on an indexed column.

Cassandra will only return matching rows with keys after start_key. If this is the empty string, all rows will be considered. Keep in mind that this is not as meaningful unless an OrderPreservingPartitioner is used.

The number of rows to return is limited by count, which defaults to 100.

Previous topic

pycassa.system_manager – Manage Schema Definitions

Next topic

pycassa.batch – Batch Operations

This Page