Intro to CouchDB

5,181 views
5,037 views

Published on

We will take an introductory look at CouchDB with a brief review of the NoSQL movement. We will spend most of the time in actual code and examples from real projects and the Futon web interface, giving a better feeling for what's going on. Plenty of time for Q&A.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,181
On SlideShare
0
From Embeds
0
Number of Embeds
242
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Intro to CouchDB

  1. 1. Intro to CouchDB by Sam Bisbee
  2. 2. Hi! <ul><li>CTO – Woopid.com
  3. 3. Maintain CouchDB for Debian
  4. 4. Sag (CouchDB for PHP) http://www.saggingcouch.com
  5. 5. sam@sbisbee.com || @sbisbee </li></ul>
  6. 6. Happy Birthday! CouchDB turned 1.0 last week!
  7. 7. NoSQL Explained in 1 [Snarky] Line <ul>$qry = sprintf(&quot;SELECT registration.*,company.name as companyName,address.lineOne as companyAddress,address.city as companyCity,address.state as companyState,address.zip as companyZip,mealsAttending.*,needHotel.* FROM registration,company,address,mealsAttending,needHotel WHERE registration.email='%s' AND registration.company=company.id AND company.address=address.id AND registration.mealsAttending=mealsAttending.id AND registration.needHotel=needHotel.id&quot;, self::clean($email)); </ul>
  8. 8. NoSQL Explained in 1 Word CHOICE
  9. 9. There isn't much PHP tonight <ul><li>That's a good thing!
  10. 10. But here's some: </li></ul>try { //Get the post (a StdClass)... $post = $this->sag->get('postID')->body; //...update its info... $post->views++; //..and send it back to the couch. return $this->sap->put($post->_id, $post)->body->ok; } catch(SagCouchException $e) { //The requested post doesn't exist - oh no! if($e->getCode() == &quot;404&quot;) $e = new Exception(&quot;That post doesn't exist.&quot;); throw $e; }
  11. 11. CAP Theorem <ul>You can only have 2 of these 3... <li>Consistency – everyone has the same data at the same time
  12. 12. Availability – it's okay if nodes go down
  13. 13. Partition Tolerance – message loss is okay </li></ul>
  14. 14. CAP Theorem Credit: CouchDB The Definitive Guide (http://books.couchdb.org)
  15. 15. JSON <ul>Javascript Object Notation <li>Null: null
  16. 16. String: “bwah”
  17. 17. Int/Float: 3.33
  18. 18. Booleans: true
  19. 19. Object: { “key”: “value” }
  20. 20. Array: [ “element, “element” ] </li></ul>
  21. 21. JSON & PHP <ul>json_encode(array(“hi”, 1, false)); json_decode($jsonString); $class = new UserVO(); json_encode($class); </ul>
  22. 22. CouchDB “Defined” <ul><li>Document based
  23. 23. Stores everything in JSON
  24. 24. RESTful API over HTTP
  25. 25. Query with Map/Reduce in a language of your choice (usually JavaScript) </li></ul>
  26. 26. Wait, document based? <ul><li>Everything is a document
  27. 27. Stored as JSON (a class)
  28. 28. No more schemas! YAY!
  29. 29. Documents can have attachments </li></ul>
  30. 30. Wait, document based? <ul>{ “name”: “Sam Bisbee”, “company”: “Woopid”, “twitter”: “@sbisbee”, “rsvp”: true, “talks”: [ 1, 5, 10 ] } </ul>
  31. 31. RESTful API <ul>CRUD operations <li>C reate POST /databaseName/ PUT /databaseName/docID
  32. 32. R ead GET /databaseName/docID
  33. 33. U pdate PUT /databaseName/docID
  34. 34. D elete DELETE /databaseName/docID </li></ul>
  35. 35. Get it... <ul>$response = $sag->get('/32 character hash'); $httpHeaders = $response->headers; $document = $response->body; </ul>
  36. 36. ...and set it <ul>$result = $sag->put( $document->_id, $document ); if($result->body->ok) return true; else throw new Exception('Nope!'); </ul>
  37. 37. Design Documents & Futon <ul><li>Design Documents hold your queries (Map/Reduce, list and show functions, etc.)
  38. 38. And now, Futon... </li></ul>
  39. 39. Map/Reduce <ul><li>Breaks up big problems into many smaller ones (distributable)
  40. 40. Map functions create a set of “intermediate” key/value pairs
  41. 41. Reduce functions collate those key/value pairs, but are optional
  42. 42. [Word count example in Futon] </li></ul>
  43. 43. Typing Documents <ul>The goal: keep all documents in 1 database, but discern between them <li>Duck typing – figure out type based on document's contents
  44. 44. Explicit typing – add a “type” field. Ex., “type”: “webPage” [back to Futon] </li></ul>
  45. 45. Example: Blog posts by author
  46. 46. Example: Blog posts by date range
  47. 47. Transactions <ul>The RDBMS way... <li>Control who and when
  48. 48. One write point
  49. 49. Can lock databases, tables, and rows </li></ul>
  50. 50. Transactions <ul>The CouchDB way... <li>Eventual consistency
  51. 51. Every document has a _rev
  52. 52. Think like a banker: treat transACTIONS like independent actions </li></ul>
  53. 53. Transactions Posted Amount Account Balance $100 + $100 $200 - $3,000 - $2,800 + $2,800 $0 { “ docType”: “postAmount”, “ account”: “account doc's _id”, “ amount”: 100 }
  54. 54. Example: Blog post Comments Don't store in the document!
  55. 55. Other Types of Queries <ul><li>Bulk API – get, update, and create multiple documents at once
  56. 56. List and Show functions – convert to a different output type (RSS, etc.)
  57. 57. couchdb-lucene – use map style functions to populate a lucene feed </li></ul>
  58. 58. Example Write Diagram PHP Couch Map input to value objects Validate your value objects React: send to couch, return an error, etc.
  59. 59. Example Write Diagram PHP Couch Map input to value objects Validate your value objects React: send to couch, return an error, etc.
  60. 60. Couch App Couch http://www.github.com/couchapp/couchapp/ Map input to value objects Validate your value objects React: send to couch, return an error, etc.
  61. 61. Replication Master -> Slave Master
  62. 62. Replication Master ↔ Master
  63. 63. Replication OMG COUCHES!
  64. 64. Clustering CouchDB <ul><li>Proxy – have a single point of entry (couch.example.com) that chooses a couch to handle the request based on some logic
  65. 65. Sharding – hash the document _id's (hexadecimal), and assign nodes to keyspaces </li></ul>
  66. 66. Example Cluster Hashes 0 - 7 Hashes 8 - f Request Proxy (logic)
  67. 67. Example Cluster Hashes 0 - 7 Hashes 8 - f Request Proxy (logic)
  68. 68. What I'm Interested In <ul><li>Best Practices </li><ul><li>Storing Design Docs in git (!svn)
  69. 69. Logic in the client versus server </li></ul><li>Better Language Support </li><ul><li>Utilities for handling common situations and data structure (ex., trees)
  70. 70. Standardize across languages </li></ul><li>More view server languages </li></ul>
  71. 71. Infinity & Beyond <ul><li>Did I mention Sag yet? http://www.saggingcouch.com
  72. 72. Couchio http://www.couch.io
  73. 73. CouchDB: The Definitive Guide http://books.couchdb.org/relax
  74. 74. couchdb-lucene github.com/rnewson/couchdb-lucene
  75. 75. CouchDB Lounge http://tilgovi.github.com/couchdb-lounge </li></ul>

×