Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit bb7cdb5

Browse files
committed
Add partitioned databases documentation.
1 parent 5f926a0 commit bb7cdb5

File tree

1 file changed

+134
-13
lines changed

1 file changed

+134
-13
lines changed

docs/getting_started.rst

Lines changed: 134 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
===============
1+
###############
22
Getting started
3-
===============
3+
###############
44

55
Now it's time to begin doing some work with Cloudant and Python. For working
66
code samples of any of the API's please go to our test suite.
@@ -32,7 +32,7 @@ a HTTP connection or a response on all requests. A timeout can be
3232
set using the ``timeout`` argument when constructing a client.
3333

3434
Connecting with a client
35-
^^^^^^^^^^^^^^^^^^^^^^^^
35+
========================
3636

3737
.. code-block:: python
3838
@@ -144,7 +144,7 @@ Note: Idle connections within the pool may be terminated by the server, so will
144144
indefinitely meaning that this will not completely remove the overhead of creating new connections.
145145

146146
Using library in app server environment
147-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147+
=======================================
148148

149149
This library can be used in an app server, and the example
150150
below shows how to use ``client`` in a ``flask`` app server.
@@ -190,7 +190,7 @@ existing database, or delete a database. The following examples assume a client
190190
connection has already been established.
191191

192192
Creating a database
193-
^^^^^^^^^^^^^^^^^^^
193+
===================
194194

195195
.. code-block:: python
196196
@@ -203,7 +203,7 @@ Creating a database
203203
print('SUCCESS!!')
204204
205205
Opening a database
206-
^^^^^^^^^^^^^^^^^^
206+
==================
207207

208208
Opening an existing database is done by supplying the name of an existing
209209
database to the client. Since the ``Cloudant`` and ``CouchDB`` classes are
@@ -216,13 +216,134 @@ sub-classes of ``dict``, this can be accomplished through standard Python
216216
my_database = client['my_database']
217217
218218
Deleting a database
219-
^^^^^^^^^^^^^^^^^^^
219+
===================
220220

221221
.. code-block:: python
222222
223223
# Delete a database using an initialized client
224224
client.delete_database('my_database')
225225
226+
227+
Partitioned Databases
228+
=====================
229+
230+
Partitioned databases introduce the ability for a user to create logical groups
231+
of documents called partitions by providing a partition key with each document.
232+
233+
.. warning:: Your Cloudant cluster must have the ``partitions`` feature enabled.
234+
A full list of enabled features can be retrieved by calling the
235+
client :func:`~cloudant.client.CouchDB.metadata` method.
236+
237+
Creating a partitioned database
238+
-------------------------------
239+
240+
.. code-block:: python
241+
242+
db = client.create_database('mydb', partitioned=True)
243+
244+
Handling documents
245+
------------------
246+
247+
The document ID contains both the partition key and document key in the form
248+
``<partitionkey>:<documentkey>`` where:
249+
250+
- Partition Key *(string)*. Must be non-empty. Must not contain colons (as this
251+
is the partition key delimiter) or begin with an underscore.
252+
- Document Key *(string)*. Must be non-empty. Must not begin with an underscore.
253+
254+
Be aware that ``_design`` documents and ``_local`` documents must not contain a
255+
partition key as they are global definitions.
256+
257+
**Create a document**
258+
259+
.. code-block:: python
260+
261+
partition_key = 'Year2'
262+
document_key = 'julia30'
263+
db.create_document({
264+
'_id': ':'.join((partition_key, document_key)),
265+
'name': 'Jules',
266+
'age': 6
267+
})
268+
269+
**Get a document**
270+
271+
.. code-block:: python
272+
273+
doc = db[':'.join((partition_key, document_key))]
274+
275+
Creating design documents
276+
-------------------------
277+
278+
To define partitioned indexes you must set the ``partitioned=True`` optional
279+
when constructing the new ``DesignDocument`` class.
280+
281+
.. code-block:: python
282+
283+
ddoc = DesignDocument(db, document_id='view', partitioned=True)
284+
ddoc.add_view('myview','function(doc) { emit(doc.foo, doc.bar); }')
285+
ddoc.save()
286+
287+
Similarly, to define a partitioned Cloudant Query index you must set the
288+
``partitioned=True`` optional.
289+
290+
.. code-block:: python
291+
292+
index = db.create_query_index(
293+
design_document_id='query',
294+
index_name='foo-index',
295+
fields=['foo'],
296+
partitioned=True
297+
)
298+
index.create()
299+
300+
Querying data
301+
-------------
302+
303+
A partition key can be specified when querying data so that results can be
304+
constrained to a specific database partition.
305+
306+
.. warning:: To run partitioned queries the database itself must be partitioned.
307+
308+
**Query**
309+
310+
.. code-block:: python
311+
312+
results = self.db.get_partitioned_query_result(
313+
partition_key, selector={'foo': {'$eq': 'bar'}})
314+
315+
for result in results:
316+
...
317+
318+
See :func:`~cloudant.database.CouchDatabase.get_partitioned_query_result` for a
319+
full list of supported parameters.
320+
321+
**Search**
322+
323+
.. code-block:: python
324+
325+
results = self.db.get_partitioned_search_result(
326+
partition_key, search_ddoc['_id'], 'search1', query='*:*')
327+
328+
for result in results['rows']:
329+
....
330+
331+
See :func:`~cloudant.database.CloudantDatabase.get_partitioned_search_result`
332+
for a full list of supported parameters.
333+
334+
**Views (MapReduce)**
335+
336+
.. code-block:: python
337+
338+
results = self.db.get_partitioned_view_result(
339+
partition_key, view_ddoc['_id'], 'view1')
340+
341+
for result in results:
342+
....
343+
344+
See :func:`~cloudant.database.CouchDatabase.get_partitioned_view_result` for a
345+
full list of supported parameters.
346+
226347
*********
227348
Documents
228349
*********
@@ -235,7 +356,7 @@ create, read, update, and delete a document. These examples assume that
235356
either a CloudantDatabase or a CouchDatabase object already exists.
236357

237358
Creating a document
238-
^^^^^^^^^^^^^^^^^^^
359+
===================
239360

240361
.. code-block:: python
241362
@@ -255,7 +376,7 @@ Creating a document
255376
print('SUCCESS!!')
256377
257378
Retrieving a document
258-
^^^^^^^^^^^^^^^^^^^^^
379+
=====================
259380

260381
Accessing a document from a database is done by supplying the document
261382
identifier of an existing document to either a ``CloudantDatabase`` or a
@@ -271,7 +392,7 @@ classes are sub-classes of ``dict``, this is accomplished through standard
271392
print(my_document)
272393
273394
Checking if a document exists
274-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
395+
=============================
275396

276397
You can check if a document exists in a database the same way you would check
277398
if a ``dict`` has a key-value pair by key.
@@ -284,7 +405,7 @@ if a ``dict`` has a key-value pair by key.
284405
print('document with _id julia30 exists')
285406
286407
Retrieve all documents
287-
^^^^^^^^^^^^^^^^^^^^^^
408+
======================
288409

289410
You can also iterate over a ``CloudantDatabase`` or a ``CouchDatabase`` object
290411
to retrieve all documents in a database.
@@ -296,7 +417,7 @@ to retrieve all documents in a database.
296417
print(document)
297418
298419
Update a document
299-
^^^^^^^^^^^^^^^^^
420+
=================
300421

301422
.. code-block:: python
302423
@@ -312,7 +433,7 @@ Update a document
312433
my_document.save()
313434
314435
Delete a document
315-
^^^^^^^^^^^^^^^^^
436+
=================
316437

317438
.. code-block:: python
318439

0 commit comments

Comments
 (0)