from Python though couchdbkit – Key-value Store Functionality – MapReduce Queries – HTTP API • When is CouchDB useful and when not? – Multi-Master Replication – Scaling up and down • Pointers to other resources, CouchDB ecosystem
code – Small Scripts or REPL • Learning-by-Watching – Example Application at https://github.com/stefankoegl/python-couchdb-examples – Slides at https://slideshare.net/skoegl/couch-db-pythonpyconpl2012 – Use example scripts and see what happens – Submit Pull-Requests!
• DB Initialization • Key-Value Store • Simple MapReduce Queries • The _changes Feed • Complex MapReduce Queries • Replication • Additional Features and the Couch Ecosystem
anymore after the training) • http://couch.skoegl.net:5984/<yourname> • Authentication: username training, password training • Setup your DB_URL in settings.py • If you want to install your own – Tutorials: https://wiki.apache.org/couchdb/Installation – Ubuntu: https://launchpad.net/~longsleep/+archive/couchdb – Mac, Windows: https://couchdb.apache.org/#download
# install with pip pip install couchdbkit # or from source git clone git://github.com/benoitc/couchdbkit.git cd couchdbkit sudo python setup.py install # and then you should be able to import import couchdbkit
and its URL eg http://127.0.0.1:5984 • What we want: a database there eg http://127.0.0.1:5984/myname • http://wiki.apache.org/couchdb/HTTP_database_API
• You can store any dict – Wrapped in couchdbkit's Document classes for convenience • Documents can be serialized to JSON … mydict = mydoc.to_json() • … and deserialized from JSON mydoc = DocClass.wrap(mydict)
StringProperty class Thing(Document): owner = StringProperty(required=True) name = StringProperty(required=True) db = Database(DB_URL) Thing.set_db(db)
by Database (bad) – Provided when storing the database (good) – Think about lost responses – couchdbkit does that for us • couchdbkit adds property doc_type with value „Thing“
Simple MapReduce Queries – Create a View – Query the View • Complex MapReduce Queries • The _changes Feed • Replication • Additional Features and the Couch Ecosystem
– Has to be side-effect free – Emits zero or more intermediate key-value pairs • Reduce Function (optional) – Aggregates intermediate pairs • View Results stored in B+-Tree – Incrementally pre-computed at query-time – Queries are just a O(log n)
_design/<ddoc name> • couchdbkit syncs ddocs from filesystem • We'll need this a few more times – Put the following in its own script – or run $ ./ldbsyncddocs.py
they were last modified • Possibility to – React on changes – Process all documents without skipping any – Continue at some point with since parameter • CouchDB as a distributed, persistent MQ • http://guide.couchdb.org/draft/notifications.html • http://wiki.apache.org/couchdb/HTTP_database_API#Changes
Simple MapReduce Queries • The _changes Feed • Advanced MapReduce Queries • Replication – Setting up filters – Find Friends and Replicate from them – Eventual Consistency and Conflicts • Additional Features and the Couch Ecosystem
and store them locally • There's also Push replication, but we won't use it • Goes through the source's _changes feed • Compares with local documents, updates or creates conflicts
function that takes – a document – a request object • and returns – true, if the document passes the filter – false otherwise • A filter is evaluated at the source
„forcing“ a document update • _rev calculated based on – Previous _rev – document content • Conflict when two documents have – The same _id – Distinct _rev
do this for you • Automatic strategy selects a (temporary) winner – Deterministic: always the same winner on each node – leaves them in conflict state • View that contains all conflicts • Resolve conflict programmatically • http://guide.couchdb.org/draft/conflicts.html • http://wiki.apache.org/couchdb/Replication_and_conflicts
Simple MapReduce Queries • The _changes Feed • Advanced MapReduce Queries • Replication • Additional Features and the Couch Ecosystem – Scaling and related Projects – Fulltext Search – Further Reading
nodes that appears as a single server – http://bigcouch.cloudant.com/ – will be merged into CouchDB soon • refuge – Fully decentralized data platform based on CouchDB – Includes fork of GeoCouch for spatial indexing – http://refuge.io/
update in the database server • External Processes: use CouchDB as a proxy to other processes (eg search engines) • Attachments: attach binary files to documents • Update Validation: JavaScript code to validate doc updates • CouchApps: Web-Apps served directly by CouchDB • Bulk APIs: Several Updates in one Request • List and Show Functions: Transforming responses before serving them
JSON for documents, JavaScript for MapReduce queries, and regular HTTP for an API • couchdbkit is a a Python library providing access to Apache CouchDB