Secondary IndexesΒΆ

Cassandra supports secondary indexes, which allow you to efficiently get only rows which match a certain expression.

Here’s a description of secondary indexes and how to use them.

In order to use get_indexed_slices() to get data from an indexed column family using the indexed column, we need to create an IndexClause which contains a list of IndexExpression objects. The IndexExpression objects inside the clause are ANDed together, meaning every expression must match for a row to be returned.

Suppose we have a ‘Users’ column family with one row per user, and we want to get all of the users from Utah with a birthdate after 1970. We can make use of the pycassa.index module to make this easier:

>>> from pycassa.pool import ConnectionPool
>>> from pycassa.columnfamily import ColumnFamily
>>> 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

Although at least one IndexExpression in the clause must be on an indexed column, you may also have other expressions which are on non-indexed columns.

Previous topic

Assorted Cassandra and pycassa Functionality

Next topic

Super Columns

This Page