Was this page helpful?
ScyllaDB Python Driver is available under the Apache v2 License. ScyllaDB Python Driver is a fork of DataStax Python Driver. See Copyright here.
Caution
You're viewing documentation for an unstable version of Scylla Python Driver. Switch to the latest stable version.
Connections aim to ease the use of multiple sessions with cqlengine. Connections can be set on a model class, per query or using a context manager.
To use cqlengine, you need at least a default connection. If you initialize cqlengine’s connections with with connection.setup
, a connection will be created automatically. If you want to use another cluster/session, you need to register a new cqlengine connection. You register a connection with register_connection()
:
from cassandra.cqlengine import connection
connection.setup(['127.0.0.1')
connection.register_connection('cluster2', ['127.0.0.2'])
register_connection()
can take a list of hosts, as shown above, in which case it will create a connection with a new session. It can also take a session argument if you’ve already created a session:
from cassandra.cqlengine import connection
from cassandra.cluster import Cluster
session = Cluster(['127.0.0.1']).connect()
connection.register_connection('cluster3', session=session)
You can change the default cqlengine connection on registration:
from cassandra.cqlengine import connection
connection.register_connection('cluster2', ['127.0.0.2'] default=True)
or on the fly using set_default_connection()
connection.set_default_connection('cluster2')
You can unregister a connection using unregister_connection()
:
connection.unregister_connection('cluster2')
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
keyspaces = ['ks1', 'ks2']
conns = ['cluster1', 'cluster2']
# registers your connections
# ...
# create all keyspaces on all connections
for ks in keyspaces:
management.create_simple_keyspace(ks, connections=conns)
# define your Automobile model
# ...
# sync your models
management.sync_table(Automobile, keyspaces=keyspaces, connections=conns)
cqlengine will select the default connection, unless your specify a connection using one of the following methods.
You can specify a default connection per model:
class Automobile(Model):
__keyspace__ = 'test'
__connection__ = 'cluster2'
manufacturer = columns.Text(primary_key=True)
year = columns.Integer(primary_key=True)
model = columns.Text(primary_key=True)
print(len(Automobile.objects.all())) # executed on the connection 'cluster2'
You can use the using()
method to select a connection (or keyspace):
Automobile.objects.using(connection='cluster1').create(manufacturer='honda', year=2010, model='civic')
q = Automobile.objects.filter(manufacturer='Tesla')
autos = q.using(keyspace='ks2', connection='cluster2').all()
for auto in autos:
auto.using(connection='cluster1').save()
You can use the ContextQuery as well to select a connection:
with ContextQuery(Automobile, connection='cluster1') as A:
A.objects.filter(manufacturer='honda').all() # executed on 'cluster1'
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:
Automobile.objects.batch(b).create(manufacturer='honda', year=2010, model='civic')
Was this page helpful?
ScyllaDB Python Driver is available under the Apache v2 License. ScyllaDB Python Driver is a fork of DataStax Python Driver. See Copyright here.
cassandra
- Exceptions and Enumscassandra.cluster
- Clusters and Sessionscassandra.policies
- Load balancing and Failure Handling Policiescassandra.auth
- Authenticationcassandra.metadata
- Schema and Ring Topologycassandra.metrics
- Performance Metricscassandra.query
- Prepared Statements, Batch Statements, Tracing, and Row Factoriescassandra.pool
- Hosts and Connection Poolscassandra.protocol
- Protocol Featurescassandra.encoder
- Encoders for non-prepared Statementscassandra.decoder
- Data Return Formatscassandra.concurrent
- Utilities for Concurrent Statement Executioncassandra.connection
- Low Level Connection Infocassandra.util
- Utilitiescassandra.timestamps
- Timestamp Generationcassandra.io.asyncioreactor
- asyncio
Event Loopcassandra.io.asyncorereactor
- asyncore
Event Loopcassandra.io.eventletreactor
- eventlet
-compatible Connectioncassandra.io.libevreactor
- libev
Event Loopcassandra.io.geventreactor
- gevent
-compatible Event Loopcassandra.io.twistedreactor
- Twisted Event Loopcassandra.cqlengine.models
- Table models for object mappingcassandra.cqlengine.columns
- Column types for object mapping modelscassandra.cqlengine.query
- Query and filter model objectscassandra.cqlengine.connection
- Connection management for cqlenginecassandra.cqlengine.management
- Schema management for cqlenginecassandra.cqlengine.usertype
- Model classes for User Defined Types