Intro to CouchDB

  • 4,453 views
Uploaded 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, …

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.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
4,453
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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