Skip to content

Commit 3efd249

Browse files
authored
Merge pull request apache#662 from datastax/613-docs
Connections docs
2 parents 7cf9cbd + bc534c4 commit 3efd249

File tree

11 files changed

+158
-15
lines changed

11 files changed

+158
-15
lines changed

cassandra/cqlengine/query.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ class ContextQuery(object):
274274
A Context manager to allow a Model to switch context easily. Presently, the context only
275275
specifies a keyspace for model IO.
276276
277+
:param *args: One or more models. A model should be a class type, not an instance.
278+
:param **kwargs: (optional) Context parameters: can be *keyspace* or *connection*
279+
277280
For example:
278281
279282
.. code-block:: python
@@ -293,10 +296,6 @@ class ContextQuery(object):
293296
"""
294297

295298
def __init__(self, *args, **kwargs):
296-
"""
297-
:param *args: One or more models. A model should be a class type, not an instance.
298-
:param **kwargs: (optional) Context parameters: can be keyspace or connection
299-
"""
300299
from cassandra.cqlengine import models
301300

302301
self.models = []
@@ -1007,7 +1006,7 @@ def timeout(self, timeout):
10071006

10081007
def using(self, keyspace=None, connection=None):
10091008
"""
1010-
Change the context on-the-fly of the Model class (connection, keyspace)
1009+
Change the context on-the-fly of the Model class (keyspace, connection)
10111010
"""
10121011

10131012
if connection and self._batch:

docs/api/cassandra/cqlengine/connection.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@
88
.. autofunction:: set_session
99

1010
.. autofunction:: setup
11+
12+
.. autofunction:: register_connection
13+
14+
.. autofunction:: unregister_connection
15+
16+
.. autofunction:: set_default_connection

docs/api/cassandra/cqlengine/models.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Model
3232

3333
.. autoattribute:: __keyspace__
3434

35+
.. autoattribute:: __connection__
36+
3537
.. attribute:: __default_ttl__
3638
:annotation: = None
3739

@@ -169,6 +171,10 @@ Model
169171

170172
Sets the ttl values to run instance updates and inserts queries with.
171173

174+
.. method:: using(keyspace=None, connection=None)
175+
176+
Change the context on the fly of the model instance (keyspace, connection)
177+
172178
.. automethod:: column_family_name
173179

174180
Models also support dict-like access:

docs/api/cassandra/cqlengine/query.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ The methods here are used to filter, order, and constrain results.
5050

5151
.. automethod:: ttl
5252

53+
.. automethod:: using
54+
5355
.. _blind_updates:
5456

5557
.. automethod:: update

docs/cqlengine/batches.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Batch Query General Use Pattern
1212

1313
.. code-block:: python
1414
15-
from cqlengine import BatchQuery
15+
from cassandra.cqlengine import BatchQuery
1616
1717
#using a context manager
1818
with BatchQuery() as b:
@@ -102,7 +102,7 @@ Logged vs Unlogged Batches
102102

103103
.. code-block:: python
104104
105-
from cqlengine.query import BatchType
105+
from cassandra.cqlengine.query import BatchType
106106
with BatchQuery(batch_type=BatchType.Unlogged) as b:
107107
LogEntry.batch(b).create(k=1, v=1)
108108
LogEntry.batch(b).create(k=1, v=2)

docs/cqlengine/connections.rst

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
==========================
2+
Connections (experimental)
3+
==========================
4+
5+
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.
6+
7+
8+
Register a new connection
9+
=========================
10+
11+
To use cqlengine, you need at least a default connection. This is currently done automatically under the hood with :func:`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 :func:`~.connection.register_connection`
12+
13+
.. code-block:: python
14+
15+
from cassandra.cqlengine import connection
16+
17+
connection.setup(['127.0.0.1')
18+
connection.register_connection('cluster2', ['127.0.0.2'])
19+
20+
Change the default connection
21+
=============================
22+
23+
You can change the default cqlengine connection on registration:
24+
25+
.. code-block:: python
26+
27+
from cassandra.cqlengine import connection
28+
29+
connection.register_connection('cluster2', ['127.0.0.2'] default=True)
30+
31+
or on the fly using :func:`~.connection.set_default_connection`
32+
33+
.. code-block:: python
34+
35+
connection.set_default_connection('cluster2')
36+
37+
Unregister a connection
38+
=======================
39+
40+
You can unregister a connection using :func:`~.connection.unregister_connection`:
41+
42+
.. code-block:: python
43+
44+
connection.unregister_connection('cluster2')
45+
46+
Management
47+
==========
48+
49+
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:
50+
51+
.. code-block:: python
52+
53+
from cassandra.cqlengine import management
54+
55+
keyspaces = ['ks1', 'ks2']
56+
conns = ['cluster1', 'cluster2']
57+
58+
# registers your connections
59+
# ...
60+
61+
# create all keyspaces on all connections
62+
for ks in keyspaces:
63+
management.create_simple_keyspace(ks, connections=conns)
64+
65+
# define your Automobile model
66+
# ...
67+
68+
# sync your models
69+
management.sync_table(Automobile, keyspaces=keyspaces, connections=conns)
70+
71+
72+
Connection Selection
73+
====================
74+
75+
cqlengine will select the default connection, unless your specify a connection using one of the following methods.
76+
77+
Default Model Connection
78+
------------------------
79+
80+
You can specify a default connection per model:
81+
82+
.. code-block:: python
83+
84+
class Automobile(Model):
85+
__keyspace__ = 'test'
86+
__connection__ = 'cluster2'
87+
manufacturer = columns.Text(primary_key=True)
88+
year = columns.Integer(primary_key=True)
89+
model = columns.Text(primary_key=True)
90+
91+
print len(Automobile.objects.all()) # executed on the connection 'cluster2'
92+
93+
QuerySet and model instance
94+
---------------------------
95+
96+
You can use the :attr:`using() <.query.ModelQuerySet.using>` method to select a connection (or keyspace):
97+
98+
.. code-block:: python
99+
100+
Automobile.objects.using(connection='cluster1').create(manufacturer='honda', year=2010, model='civic')
101+
q = Automobile.objects.filter(manufacturer='Tesla')
102+
autos = q.using(keyspace='ks2, connection='cluster2').all()
103+
104+
for auto in autos:
105+
auto.using(connection='cluster1').save()
106+
107+
Context Manager
108+
---------------
109+
110+
You can use the ContextQuery as well to select a connection:
111+
112+
.. code-block:: python
113+
114+
with ContextQuery(Automobile, connection='cluster1') as A:
115+
A.objects.filter(manufacturer='honda').all() # executed on 'cluster1'
116+
117+
118+
BatchQuery
119+
----------
120+
121+
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.
122+
123+
.. code-block:: python
124+
125+
with BatchQuery(connection='cluster1') as b:
126+
Automobile.objects.batch(b).create(manufacturer='honda', year=2010, model='civic')

docs/cqlengine/models.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ extend the model's validation method:
119119
if self.name == 'jon':
120120
raise ValidationError('no jon\'s allowed')
121121
122-
*Note*: while not required, the convention is to raise a ``ValidationError`` (``from cqlengine import ValidationError``)
122+
*Note*: while not required, the convention is to raise a ``ValidationError`` (``from cassandra.cqlengine import ValidationError``)
123123
if validation fails.
124124

125125
.. _model_inheritance:

docs/cqlengine/queryset.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,10 @@ Named tables are a way of querying a table without creating an class. They're u
387387

388388
.. code-block:: python
389389
390-
from cqlengine.connection import setup
390+
from cassandra.cqlengine.connection import setup
391391
setup("127.0.0.1", "cqlengine_test")
392392
393-
from cqlengine.named import NamedTable
393+
from cassandra.cqlengine.named import NamedTable
394394
user = NamedTable("cqlengine_test", "user")
395395
user.objects()
396396
user.objects()[0]

docs/cqlengine/third_party.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ Here's how, in substance, CQLengine can be plugged to `Celery
1313
1414
from celery import Celery
1515
from celery.signals import worker_process_init, beat_init
16-
from cqlengine import connection
17-
from cqlengine.connection import (
16+
from cassandra.cqlengine import connection
17+
from cassandra.cqlengine.connection import (
1818
cluster as cql_cluster, session as cql_session)
1919
2020
def cassandra_init(**kwargs):
@@ -40,8 +40,8 @@ This is the code required for proper connection handling of CQLengine for a
4040

4141
.. code-block:: python
4242
43-
from cqlengine import connection
44-
from cqlengine.connection import (
43+
from cassandra.cqlengine import connection
44+
from cassandra.cqlengine.connection import (
4545
cluster as cql_cluster, session as cql_session)
4646
4747
try:

docs/cqlengine/upgrade_guide.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Imports
4040
cqlengine is now integrated as a sub-package of the driver base package 'cassandra'.
4141
Upgrading will require adjusting imports to cqlengine. For example::
4242

43-
from cqlengine import columns
43+
from cassandra.cqlengine import columns
4444

4545
is now::
4646

0 commit comments

Comments
 (0)