Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Django ORM for use with Apache Cassandra

branch: develop

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 cassorm
Octocat-spinner-32 .gitignore
Octocat-spinner-32 AUTHORS
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.mkd
Octocat-spinner-32 ez_setup.py
Octocat-spinner-32 setup.py
README.mkd

Cassorm

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

Installation

If easy_install is available, you can use:

easy_install https://github.com/wehriam/Cassorm/tarball/develop

Connecting

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)

Basic Usage, Models

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.

Basic Usage, Lists

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'

Basic Usage, Dictionaries

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

Tests

Add the following lines to your app's tests.py file:

from cassorm import CassandraListTest
from cassorm import CassandraModelTest
from cassorm import CassandraDict
Something went wrong with that request. Please try again.