Class Mapping with Column Family MapΒΆ

You can map existing classes to column families using ColumnFamilyMap.

To specify the fields to be persisted, use any of the subclasses of pycassa.types.CassandraType available in pycassa.types.

>>> from pycassa.types import *
>>> class User(object):
...     key = LexicalUUIDType()
...     name = Utf8Type()
...     age = IntegerType()
...     height = FloatType()
...     score = DoubleType(default=0.0)
...     joined = DateType()

The defaults will be filled in whenever you retrieve instances from the Cassandra server and the column doesn’t exist. If you want to add a column in the future, you can simply add the relevant attribute to the class and the default value will be used when you get old instances.

>>> from pycassa.pool import ConnectionPool
>>> from pycassa.columnfamilymap import ColumnFamilyMap
>>>
>>> pool = ConnectionPool('Keyspace1')
>>> cfmap = ColumnFamilyMap(User, pool, 'users')

All the functions are exactly the same as for ColumnFamily, except that they return instances of the supplied class when possible.

>>> from datetime import datetime
>>> import uuid
>>>
>>> key = uuid.uuid4()
>>>
>>> user = User()
>>> user.key = key
>>> user.name = 'John'
>>> user.age = 18
>>> user.height = 5.9
>>> user.joined = datetime.now()
>>> cfmap.insert(user)
1261395560186855
>>> user = cfmap.get(key)
>>> user.name
"John"
>>> user.age
18
>>> users = cfmap.multiget([key1, key2])
>>> print users[0].name
"John"
>>> for user in cfmap.get_range():
...    print user.name
"John"
"Bob"
"Alex"
>>> cfmap.remove(user)
1261395603906864
>>> cfmap.get(user.key)
Traceback (most recent call last):
...
cassandra.ttypes.NotFoundException: NotFoundException()

Previous topic

Composite Types

Next topic

Version 1 UUIDs (TimeUUIDType)

This Page