• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Intro To Couch Db
 

Intro To Couch Db

on

  • 17,394 views

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

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

Statistics

Views

Total Views
17,394
Views on SlideShare
16,342
Embed Views
1,052

Actions

Likes
26
Downloads
353
Comments
0

12 Embeds 1,052

http://www.techgig.com 828
http://blog.nosqlfan.com 163
http://www.slideshare.net 41
http://188.40.186.163 9
http://www.techgig.timesjobs.com 2
http://dschool.co 2
http://static.slidesharecdn.com 2
http://livebuzz.com.br 1
http://www.16kan.com 1
http://www.uplook.cn 1
http://xue.uplook.cn 1
http://115.112.206.222 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Intro To Couch Db Intro To Couch Db Presentation Transcript

    • Introduction to using CouchDB with PHP By Shahahr Evron Zend Technologies
    • 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
    • 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
    • What is Apache CouchDB? 4
    • 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
    • A Document Oriented Database Data is stored in “documents” ...and not in “relations” like in an RDBMS 6
    • 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 dscrute@dundermifflin.com 002 001 Michael Scott mscott@dundermifflin.com 003 002 Maurice Moss moss.m@reynholm.co.uk ID Account ID From Subject Body 001 001 001 Defending from bear attack [CLOB] 002 002 003 FIRE! [CLOB] 7
    • Document Oriented Storage { "Name": "Reynholm Industries" "Region": "UK" "Owner": "Bob" "Contacts": [ { "Name": "Maurice Moss" "Email": "moss.m@reynholm.co.uk" } { "Name": "Denholm Reynholm" "Email": "theboss@reynholm.co.uk" }] } { "From": "Maurice Moss" "Subject": "FIRE!" "Message": "Dear Sir / Madam, .... ...." } 8
    • 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
    • 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
    • Hello, CouchDB time to relax! 11
    • 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
    • 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
    • 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
    • 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
    • The Futon Administration Interface 16
    • 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
    • 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
    • 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
    • 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
    • Introducing the Sopha client library... http://github.com/shevron/sopha 21
    • 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
    • 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
    • Introducing the Stoa sample application http://github.com/shevron/stoa 24
    • Views and Map/Reduce Functions 25
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • Epilogue 33
    • 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
    • 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
    • Want to learn more? Google: http://www.google.com ;) Docs & Wiki: http://couchdb.apache.org IRC: #couchdb @ FreeNode Mailing List : user-subscribe@couchdb.apache.org Upcoming Book: http://books.couchdb.org/relax/ 36
    • Thank You! Feedback: shahar.e@zend.com, http://joind.in/890, @shevron Slides will be available at http://arr.gr/ This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. 37