Cassorm (Cassandra ORM) is an object-relational mapping built on top of the Pycassa client library and designed for use with Django and Apache Cassandra (.7+). It features:
- Automatic schema generation/installation
- Secondary index support
- Model, List, and Dictionary types
If easy_install is available, you can use:
easy_install https://github.com/wehriam/Cassorm/tarball/develop
Add the following lines to your Django settings.py file:
import pycassa
CASSANDRA_SERVERS = ["127.0.0.1:9160"]
CASSANDRA_KEYSPACE = "SampleKeyspace"
CASSANDRA_POOL = pycassa.connect(CASSANDRA_KEYSPACE, CASSANDRA_SERVERS)
Cassorm offers three main data types. The first is a model based on Pycassa's ColumnFamilyMap:
class TestModel(cassorm.CassandraModel): # Column Family will be 'testmodel'
a = cassorm.DateTime()
b = cassorm.DateTimeString()
c = cassorm.Float64()
d = cassorm.FloatString()
e = cassorm.Int64()
f = cassorm.IntString()
g = cassorm.String()
To create the schema, use the "sync" method. This only needs to be done once per column family.
TestModel.sync()
Instantiate the model to use it:
test = TestModel()
test.a = datetime.now()
test.save()
TestModel.objects[test.key].a # Returns a datetime object.
class TestList(CassandraList): # Column Family will be 'testlist'
pass
To create the schema, use the "sync" method. This only needs to be done once per column family.
TestList.sync()
Instantiate the list to use it:
sample = TestList("SampleKey")
sample.append("a")
sample.append("b")
len(sample) # 2
sample[0] # 'a'
sample.pop() # 'b'
class TestDict(CassandraDict): # Column Family will be 'testdict'
pass
To create the schema, use the "sync" method. This only needs to be done once per column family.
TestDict.sync()
Instantiate the dictionary to use it:
sample = TestDict("SampleKey")
sample['a'] = 'Alice'
sample['b'] = 'Bob'
sample.keys() # ['a', 'b']
sample.values() # ['Alice', 'Bob']
del sample['a']
sample.keys() # ['b']
'b' in sample # True
Add the following lines to your app's tests.py file:
from cassorm import CassandraListTest
from cassorm import CassandraModelTest
from cassorm import CassandraDict