Scylla Documentation Logo Documentation
  • Server
    • Scylla Open Source
    • Scylla Enterprise
    • Scylla Alternator
  • Cloud
    • Scylla Cloud
    • Scylla Cloud Docs
  • Tools
    • Scylla Manager
    • Scylla Monitoring Stack
    • Scylla Operator
  • Drivers
    • CQL Drivers
    • DynamoDB Drivers
Download
Menu

Caution

You're viewing documentation for a previous version of Scylla Python Driver. Switch to the latest stable version.

Scylla Python Driver Object Mapper Connections

Connections¶

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.

Register a new connection¶

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)

Change the default connection¶

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')

Unregister a connection¶

You can unregister a connection using unregister_connection():

connection.unregister_connection('cluster2')

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

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)

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):
    __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'

QuerySet and model instance¶

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()

Context Manager¶

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'

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:
    Automobile.objects.batch(b).create(manufacturer='honda', year=2010, model='civic')
PREVIOUS
Batch Queries
NEXT
Third party integrations
  • 3.24.8
    • 3.25.4
    • 3.24.8
    • 3.22.3
    • 3.21.0
  • API Documentation
    • cassandra - Exceptions and Enums
    • cassandra.cluster - Clusters and Sessions
    • cassandra.policies - Load balancing and Failure Handling Policies
    • cassandra.auth - Authentication
    • cassandra.graph - Graph Statements, Options, and Row Factories
    • cassandra.metadata - Schema and Ring Topology
    • cassandra.metrics - Performance Metrics
    • cassandra.query - Prepared Statements, Batch Statements, Tracing, and Row Factories
    • cassandra.pool - Hosts and Connection Pools
    • cassandra.protocol - Protocol Features
    • cassandra.encoder - Encoders for non-prepared Statements
    • cassandra.decoder - Data Return Formats
    • cassandra.concurrent - Utilities for Concurrent Statement Execution
    • cassandra.connection - Low Level Connection Info
    • cassandra.util - Utilities
    • cassandra.timestamps - Timestamp Generation
    • cassandra.io.asyncioreactor - asyncio Event Loop
    • cassandra.io.asyncorereactor - asyncore Event Loop
    • cassandra.io.eventletreactor - eventlet-compatible Connection
    • cassandra.io.libevreactor - libev Event Loop
    • cassandra.io.geventreactor - gevent-compatible Event Loop
    • cassandra.io.twistedreactor - Twisted Event Loop
    • cassandra.cqlengine.models - Table models for object mapping
    • cassandra.cqlengine.columns - Column types for object mapping models
    • cassandra.cqlengine.query - Query and filter model objects
    • cassandra.cqlengine.connection - Connection management for cqlengine
    • cassandra.cqlengine.management - Schema management for cqlengine
    • cassandra.cqlengine.usertype - Model classes for User Defined Types
    • cassandra.datastax.graph - Graph Statements, Options, and Row Factories
    • cassandra.datastax.graph.fluent
    • cassandra.datastax.graph.fluent.query
    • cassandra.datastax.graph.fluent.predicates
  • Installation
  • Getting Started
  • Scylla Specific Features
  • Upgrading
  • Execution Profiles
  • Performance Notes
  • Paging Large Queries
  • Lightweight Transactions (Compare-and-set)
  • Security
  • User Defined Types
  • Object Mapper
    • Upgrade Guide
    • Models
    • Making Queries
    • Batch Queries
    • Connections
    • Third party integrations
    • Frequently Asked Questions
  • Working with Dates and Times
  • Scylla Cloud
  • Frequently Asked Questions
  • Create an issue
  • Edit this page

On this page

  • Connections
    • Register a new connection
    • Change the default connection
    • Unregister a connection
    • Management
    • Connection Selection
      • Default Model Connection
      • QuerySet and model instance
      • Context Manager
      • BatchQuery
Logo
Docs Contact Us About Us
Mail List Icon Slack Icon
© ScyllaDB 2021 and © DataStax 2013-2017
Powered by Sphinx 4.3.2 & ScyllaDB Theme 1.2.2