Intro To Couch Db


Published on

An introductory presentation to Document oriented databases, CouchDB, and using CouchDB from within PHP

Published in: Technology
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Intro To Couch Db

  1. Introduction to using CouchDB with PHP By Shahahr Evron Zend Technologies
  2. Welcome! I am: ▶ A PHP programmer since 2002 ▶ At Zend since 2005 ▶ Technical Product Manager for Zend Server Yes, I have a difficult name (at least for English speakers) ▶ Shachar (German, Dutch) ▶ Shajar (Spanish) ▶ Шахар (Russian) ▶ ‫( ﺷﺨﺮ‬Arabic) ▶ ‫( שחר‬Hebrew) 2
  3. Agenda ▶ What is CouchDB? ● What is a Document-Oriented Database? ▶ Basic Concepts & the REST API ● Databases ● Documents ▶ Views ● Map/Reduce functions ● Understanding the view collation ▶ Where to go next? 3
  4. What is Apache CouchDB? 4
  5. CouchDB Is... ▶ An Open-Source document-oriented database ▶ An Apache project ▶ HTTP based API, uses JSON for data representation ▶ Built-in replication / synchronization support ▶ Written in Erlang ▶ Started in 2005 by Damien Katz ▶ Became an Apache project in 2008 ▶ Currently in version 0.10 ▶ API is still changing! 5
  6. A Document Oriented Database Data is stored in “documents” ...and not in “relations” like in an RDBMS 6
  7. Relational Storage ID Name Region Owner 001 Reynholm Industries UK Bob 002 Dunder Mifflin Inc. US Sally 003 MomCorp NNY Sally ID Account ID Name Email 001 001 Dwight Schrute 002 001 Michael Scott 003 002 Maurice Moss ID Account ID From Subject Body 001 001 001 Defending from bear attack [CLOB] 002 002 003 FIRE! [CLOB] 7
  8. Document Oriented Storage { "Name": "Reynholm Industries" "Region": "UK" "Owner": "Bob" "Contacts": [ { "Name": "Maurice Moss" "Email": "" } { "Name": "Denholm Reynholm" "Email": "" }] } { "From": "Maurice Moss" "Subject": "FIRE!" "Message": "Dear Sir / Madam, .... ...." } 8
  9. Data is indexed with Map/Reduce functions No SQL! ▶ Querying is done using “views” ▶ Views are defined using map/reduce functions ▶ Data is indexed using these views as it is stored in the DB Map/Reduce functions are: ▶ Map functions emit (key, value) interpretation of the data ▶ Reduce functions aggregate data emitted by map functions Map/Reduce functions are written in JavaScript 9
  10. What is it good for? You should use it for... ▶ Storing documents – or any data which is document-like ▶ Hosting entire applications! Why? ▶ Very easy to program with – no SQL, no schema, no ORM ▶ Schema-less means very easy to deploy changes ▶ Easy to administer, backup, replicate, can work offline You should not use it for... ▶ Storing data which is relational in nature ▶ Be careful with data that needs to have unique values 10
  11. Hello, CouchDB time to relax! 11
  12. Accessing CouchDB from PHP CouchDB is accessed using an HTTP based API ▶ You can use any good HTTP client from PHP to access CouchDB ● PHP HTTP stream, cURL, pecl_http, Zend_Http_Client … ● We will use Zend_Http_Client in our examples (sorry, I'm biased!) ▶ You can create or use an existing dedicated client library ● Wraps the HTTP calls with CouchDB-specific API Data sent to / from CouchDB is serialized using JSON ▶ You can use PHP's ext/json to easily work with it ▶ You can use ZF's Zend_Json if you need a portable solution 12
  13. Server API Check the server status: echo $client->setUri('http://localhost:5984/') ->request('GET') ->getBody(); {"couchdb":"Welcome","version":"0.9.1"} Get list of all databases: echo $client->setUri('http://localhost:5984/_all_dbs') ->request('GET') ->getBody(); ["my_db","stoa","test_suite_db","test_suite_db_a"] 13
  14. Database API Create a new database: $resp = $client->setUri('http://localhost:5984/mydb') ->request('PUT'); echo $resp->getStatus(); // Expected status is 201 Delete an existing database: $resp = $client->setUri('http://localhost:5984/mydb') ->request('DELETE'); echo $resp->getStatus(); // Expected status is 200 14
  15. Creating Documents Creating a new document with a server-generated ID: $doc = json_encode(array( 'title' => 'Speaking at ZendCon09!', 'tags' => array('speaking', 'php', 'zendcon', 'zend'), 'created_at' => 1255977324, 'published' => true, 'content' => "Yey! I'm speaking at ZendCon!" )); $resp = $client->setUri('http://localhost:5984/mydb') ->setRawData($doc, 'text/json') ->request('POST'); // Response code should be 201 echo $resp->getBody(); // {"ok":true,"id":"b82d17579b9c901f6911727167a39987","rev":"1-190672822"} 15
  16. The Futon Administration Interface 16
  17. Creating Documents Creating a new document with a user defined ID: $doc = json_encode(array( 'title' => 'Speaking at ZendCon09!', 'tags' => array('speaking', 'php', 'zendcon', 'zend'), 'created_at' => 1255977324, 'published' => true, 'content' => "Yey! I'm speaking at ZendCon!" )); $resp = $client->setUri('http://localhost:5984/mydb/speaking-at-zendcon') ->setRawData($doc, 'text/json') ->request('PUT'); // Response code should be 201 echo $resp->getBody(); // {"ok":true,"id":"speaking-at-zendcon","rev":"1-2035733428"} 17
  18. Accessing Documents Access the current revision of a document: $docId = 'speaking-at-zendcon'; $resp = $client->setUri('http://localhost:5984/mydb/' . urlencode($docId)) ->request('GET'); var_export(json_decode($resp->getBody(), true)); ▶ Expected output: array ( '_id' => 'speaking-at-zendcon', '_rev' => '1-2035733428', 'title' => 'Speaking at ZendCon09!', 'tags' => array ('speaking', 'php', 'zendcon', 'zend'), 'created_at' => 1255977324, 'published' => true, 'content' => 'Yey! I'm speaking at ZendCon!', ); ▶ You can access older revisions of the same document: 'http://localhost:5984/mydb/speaking-at-zendcon?rev=' . $rev 18
  19. Updating Documents Documents are updated as a whole ▶ You can't “update” a single value inside the document ▶ You must specify the current revision number when updating $doc = json_encode(array( '_rev' => '1-2035733428', 'title' => 'Speaking at ZendCon!', 'tags' => array('speaking', 'php', 'zendcon', 'zend'), 'created_at' => 1255977324, 'published' => false, 'content' => "Yey! I'm speaking at ZendCon!" )); $resp = $client->setUri('http://localhost:5984/mydb/speaking-at-zendcon') ->setRawData($doc, 'text/json') ->request('PUT'); // Expected code is 201, 409 means revision conflict // Expected body: {"ok":true,"id":"speaking-at-zendcon","rev":"2-2571024485"} 19
  20. Deleting Documents Deleting is easy! ▶ Again, you must specify the revision number $docId = 'speaking-at-zendcon'; $rev = '1-2035733428'; $url = "http://localhost:5984/mydb/$docId?rev=$rev"; $resp = $client->setUri($url) ->request('DELETE'); echo $resp->getStatus(); // Expected status is 200 20
  21. Introducing the Sopha client library... 21
  22. Sopha is... A CouchDB Client Library (being) written in PHP 5.2 ▶ …except for the ViewServer component which is 5.3 Wraps the HTTP/JSON work in a simple API ▶ Sopha_Http ▶ Sopha_Json Provides access to main CouchDB features ▶ Sopha_Db ▶ Sopha_Document ▶ Sopha_View 22
  23. Some Sopha API: Sopha_Db::createDb('mydb', 'localhost', Sopha_Db::COUCH_PORT); $db = new Sopha_Db('mydb'); // when opening an existing DB $doc = $db->retrieve($docId, 'MyDocumentClass', $revision); $doc->myparam = 'some new value'; $doc->save(); $doc->delete(); $phpValue = array( 'kak' => 'dila', 'ma' => 'nishma' ); $doc = $db->create($phpValue, 'myDocuemtnId'); 23
  24. Introducing the Stoa sample application 24
  25. Views and Map/Reduce Functions 25
  26. What are views? A convenient way to query your documents ▶ Provide a way to access data based on more than the doc ID ▶ Provide a way to aggregate data from several docs Views are defined in design documents ▶ Special URL: /<dbname>/_design/<viewname> ▶ Each design document can define several views ▶ Each view defines a map function, and can define a reduce function ▶ Documents inserted or updated are indexed using these functions 26
  27. Creating a design document Creating a design document is like creating a regular document, with special content and URL: $mapFunc = 'function(doc) { emit(doc.from, doc); }'; $designDoc = json_encode(array( 'language' => 'javascript', 'views' => array( 'bycontact' => array( 'map' => $mapFunc ) ) )); $resp = $client->setUri('http://localhost:5984/mydb/_design/email') ->setRawData($designDoc, 'text/json') ->request('PUT'); 27
  28. Map Functions Map functions emit interpretations of documents passed through them ▶ Can emit the entire document or a part of it ▶ Can emit multiple results for a single document Each emitted document has a key ▶ The key can be any valid JSON value ▶ Key is used for sorting and limiting the query scope ● You can query a view, and specify a single key or a key range 28
  29. Understanding View Collation View results are ordered by key, according to the view collection View collation order: ▶ null ▶ Booleans: false, true ▶ Numbers ▶ Strings, case sensitive (lower case, upper case) ▶ Arrays ● Internal sorting by values ▶ Objects ● Internal sorting by keys 29
  30. Reduce Functions Reduce functions reduce mapped values down to a single aggregated value ▶ Reduce is optional – a view can have a map function only ▶ Reduce functions receive a set of keys and values ● Can be a set of values emitted by the map function ● Can be an already-reduced value returned by a previous run of the reduce function (rereduce) ▶ Reduce functions can group results according to their key ● When not grouped, reduce will return a single value ● Example: show count of all contacts vs. count of all contacts per account 30
  31. Calling views Views are accessed like regular documents, with a special URL: $url = "_design/post/_view/by-tag"; $resp = $client->setUri("http://localhost:5984/$url") ->request('GET'); {"total_rows":6,"offset":0,"rows":[ {"id":"e0a21a071103585d1c2e3e168b2cfe6b", "key":["buzz",1256058429],"value":{...}}, {"id":"f03ddef93502092218cb39c25be47937", "key":["buzz",1256058469],"value":{...}}, {"id":"e0a21a071103585d1c2e3e168b2cfe6b", "key":["leveraging",1256058429],"value":{...}}, {"id":"f03ddef93502092218cb39c25be47937", "key":["meta",1256058469],"value":"value":{...}}, {"id":"f03ddef93502092218cb39c25be47937", "key":["post",1256058469],"value":"value":{...}}, {"id":"e0a21a071103585d1c2e3e168b2cfe6b", "key":["stuff",1256058429],"value":"value":{...}}, ]} 31
  32. Query Parameters You can add the following parameters to the query when calling a view: ▶ key=keyvalue ▶ startkey=keyvalue ▶ endkey=keyvalue ▶ limit=... ▶ descending=true ▶ skip=... ▶ group=true (when calling a reduce view) ▶ grouplevel=... (when calling a reduce view) 32
  33. Epilogue 33
  34. Last Advice... ▶ Forget what you know about relational databases! ▶ …but don't try to force data that should not be in CouchDB in to it – you can use both an RDBMS and a document DB! ▶ Understand Map/Reduce ▶ Understand the view collation, and use it creatively ▶ Ask questions! 34
  35. Some things not covered here ...But you should probably know about: ▶ Bulk queries ▶ Document attachments ▶ Authentication & access control model ▶ Utilizing HTTP-level caching ▶ Replication model & conflict resolution ▶ In-DB applications ▶ Lucene Integration ▶ Replacing the view server (yes, you can use PHP!) 35
  36. Want to learn more? Google: ;) Docs & Wiki: IRC: #couchdb @ FreeNode Mailing List : Upcoming Book: 36
  37. Thank You! Feedback:,, @shevron Slides will be available at This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License. To view a copy of this license, visit or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. 37