0

I'm a Django beginner and have developed 1 app using mysql as primary DB, but in my next project I have to use Cassandra db using https://github.com/cqlengine/cqlengine but do not use https://github.com/r4fek/django-cassandra-engine (which is a wrapper over cqlengine?).

I dont have any clue How do I start? I mean how and where should I create db connection and then create models in models.py file?

Should I create connection in init.py file?in views.py? what would be the most efficient way?

would be great(for future readers too) if someone provide a simple configuration and a model.

xyz
  • 197
  • 3
  • 16

2 Answers2

3

The twissandra demo should be a good example of how to build an app using Cassandra and Django.

In this implementation there is no models.py and the connection is maintained in the file cass.py.

You'll see cass.py also hosts all the functions required to return data from the C* database and make objects which are used by the system. This is where you would swap out the api requests with your CqlEngine code.

I hope these resources get you pointed in the right direction

RussS
  • 16,476
  • 1
  • 34
  • 62
  • Thanks! but I'm looking for only cqlengine source and using models.py. e.g. `from cqlengine import connection` `connection.setup(['127.0.0.1:9160'])` can you please provide a source for that. – xyz Dec 17 '14 at 06:00
  • Twissandra has long been a project that time forgot. You can look over my answer for the appropriate CQLEngine response. A good example project to take a look at is: [Meat Bot by rustyrazorblade, creator of CQLEngine](https://github.com/rustyrazorblade/meatbot/) – mbeacom Apr 30 '15 at 18:43
0

Rustyrazorblade shows an easy way to accomplish this via his CQLEngine tutorial branch HERE.

You can easily setup the connection by doing something like this in your_app_project/models/connection.py:

from cqlengine import management
from cqlengine.connection import setup

def connect():
    setup(["127.0.0.1", "127.0.1.1", "127.0.1.2"], "tutorial", retry_connect=True)
    management.create_keyspace("tutorial", replication_factor=1, strategy_class="SimpleStrategy")

In this example: "tutorial" is the keyspace we are using, strategy_class is the replication strategy your C* instance is using, replication_factor is the amount of replications that will be stored throughout the ring, 127.0.0.1 is a Cassandra cluster node IP address (you can pass this a list or a string) and retry_connect specifies whether or not you would like it to attempt to reconnect if there is a connection failure.

From here, it is very easy for new C* users to get confused. You can call this anytime Before syncing the C* tables or using a C* query.

So, you'll want to do something like:

from cqlengine.management import sync_table
from models.connection import connect
from models.somemodels import MyCassandraModel

# This will fire off our previously setup 'connect' method
connect()
# This will setup the Model as a table in your C* DB
sync_table(MyCassandraModel)

You can even drop this into manage.py, just as long as that CQLEngine setup() is properly executed.

mbeacom
  • 1,466
  • 15
  • 25