3.9 KiB
Connections (experimental)
Connections are experimental and aimed to ease the use of multiple sessions with cqlengine. Connections can be set on a model class, per query or using a context manager.
Register a new connection
To use cqlengine, you need at least a default connection. This is
currently done automatically under the hood with connection.setup <.connection.setup>
. If you
want to use another cluster/session, you need to register a new
cqlengine connection. You register a connection with ~.connection.register_connection
from cassandra.cqlengine import connection '127.0.0.1') connection.setup(['cluster2', ['127.0.0.2']) connection.register_connection(
Change the default connection
You can change the default cqlengine connection on registration:
from cassandra.cqlengine import connection 'cluster2', ['127.0.0.2'] default=True) connection.register_connection(
or on the fly using ~.connection.set_default_connection
'cluster2') connection.set_default_connection(
Unregister a connection
You can unregister a connection using ~.connection.unregister_connection
:
'cluster2') connection.unregister_connection(
Management
When using multiples connections, you also need to sync your models on all connections (and keyspaces) that you need operate on. Management commands have been improved to ease this part. Here is an example:
from cassandra.cqlengine import management = ['ks1', 'ks2'] keyspaces = ['cluster1', 'cluster2'] conns # registers your connections # ... # create all keyspaces on all connections for ks in keyspaces: =conns) management.create_simple_keyspace(ks, connections # define your Automobile model # ... # sync your models =keyspaces, connections=conns) management.sync_table(Automobile, keyspaces
Connection Selection
cqlengine will select the default connection, unless your specify a connection using one of the following methods.
Default Model Connection
You can specify a default connection per model:
class Automobile(Model): = 'test' __keyspace__ = 'cluster2' __connection__ = columns.Text(primary_key=True) manufacturer = columns.Integer(primary_key=True) year = columns.Text(primary_key=True) model print len(Automobile.objects.all()) # executed on the connection 'cluster2'
QuerySet and model instance
You can use the using() <.query.ModelQuerySet.using>
method to
select a connection (or keyspace):
='cluster1').create(manufacturer='honda', year=2010, model='civic') Automobile.objects.using(connection= Automobile.objects.filter(manufacturer='Tesla') q = q.using(keyspace='ks2, connection='cluster2').all() autos for auto in autos: auto.using(connection='cluster1').save()
Context Manager
You can use the ContextQuery as well to select a connection:
with ContextQuery(Automobile, connection='cluster1') as A: filter(manufacturer='honda').all() # executed on 'cluster1' A.objects.
BatchQuery
With a BatchQuery, you can select the connection with the context manager. Note that all operations in the batch need to use the same connection.
with BatchQuery(connection='cluster1') as b: ='honda', year=2010, model='civic') Automobile.objects.batch(b).create(manufacturer