0
votes

I'm using Cassandra for the first time, but have experience with setting up ORMS in libraries like SQLAlchemy. Following this page as an example I've set up my tables using the cassandra.cqlengine.models.Model class as a base class and use sync_table to set it up, e.g.:

from cassandra.cqlengine import columns
from cassandra.cqlengine.models import Model
from cassandra.cqlengine.management import sync_table

class Message(Model):
    id = columns.Text(primary_key=True)
    message = columns.Text()

sync_table(Message)

But most sample code Python/Cassandra I've seen uses a cassandra.cluster.Cluster object to define their session, as described here.

It seems like using the Cluster provides better management of the session, but restricts you to using raw text queries. Is there a nice way to use the Cluster object with the ORM I've defined?

I think I am able to do something like this:

import cfg

from cassandra.cqlengine import connection
from cassandra.cluster import Cluster

cluster = Cluster(cfg.cassandra_dbs)
session = cluster.connect(cfg.cassandra_keyspace)
connection.set_session(session)

Since I only have one db/keyspace/etc I'm worried about, maybe it's okay if it's set globally across the app if my models will use this as the connection. Is this the suggested method, or is there a better way of doing this?

Thanks!

1

1 Answers

1
votes

If you only have one cluster and keyspace, the global session configuration is the simplest way to go.

For more complex use cases, use the registered connection feature introduced in driver 3.7.0: http://www.datastax.com/dev/blog/python-driver-3-7-0-released https://datastax.github.io/python-driver/cqlengine/connections.html

This allows for use of single Models across differing keyspaces and sessions.