Intro to CouchDB

38
Intro to CouchDB Ben Aldred http://geekmade.co.uk @benaldred

description

A quick introduction to CouchDB and CouchRest done at nwrug.org

Transcript of Intro to CouchDB

Page 1: Intro to CouchDB

Intro to CouchDB

Ben Aldredhttp://geekmade.co.uk

@benaldred

Page 2: Intro to CouchDB

Disclaimer• I am not a CouchDB expert• I still quite like Relational Databases

Page 3: Intro to CouchDB

Introducing...

Page 4: Intro to CouchDB

Apache(so it must be good, right?)

Page 5: Intro to CouchDB

Document orientated Database

Page 6: Intro to CouchDB

CouchDB aims to solve problems

Page 7: Intro to CouchDB

Scaling

Page 8: Intro to CouchDB

Storing Data

Page 9: Intro to CouchDB

How?

Page 10: Intro to CouchDB

CouchDB Scalesjust like rails!

Page 11: Intro to CouchDB

Non-locking Multiversion

concurrency control (MVCC)

Page 12: Intro to CouchDB

Replication

Page 13: Intro to CouchDB

New way to model data

Page 14: Intro to CouchDB

Case Study: Invoices

Page 15: Intro to CouchDB

JSON{ "_id":"hello-world", "_rev":"AE19EBC7654", "title":"Hello World", "body":"content...", "date":"2009/12/25 15:52:20" }

Page 16: Intro to CouchDB

JSON{ "_id":"hello-world", "_rev":"AE19EBC7654", "title":"Hello World", "body":"content...", "date":"2009/12/25 15:52:20" }

Page 17: Intro to CouchDB

JSON{ "_id":"hello-world", "_rev":"AE19EBC7654", "title":"Hello World", "body":"content...", "date":"2009/12/25 15:52:20" }

Page 18: Intro to CouchDB

HTTP REST APICreate: PUT /:db Read: GET /:db/:docidUpdate: PUT /:db/:docidDelete: DELETE /:db/:docid

Page 19: Intro to CouchDB

Views

Page 20: Intro to CouchDB

Mapfunction(doc) { if(doc.date && doc.title) { emit(doc.date, doc.title); }}

key | value-------------------------------------"2009/12/25 15:52:20" | "Hello World"

Page 21: Intro to CouchDB

Query the Viewhttp://localhost:5984/mydb/_design/post/_view/foo

Page 22: Intro to CouchDB

Query the Viewhttp://localhost:5984/mydb/_design/post/_view/foo

Page 23: Intro to CouchDB

Demo

Page 24: Intro to CouchDB

When to use CouchDB

Page 25: Intro to CouchDB

When you need to scale

Page 26: Intro to CouchDB

When availibility is more important than

consistency

Page 27: Intro to CouchDB

When not to use it

Page 28: Intro to CouchDB

You want to leaverage the power of an ORM

Page 29: Intro to CouchDB

CouchDB + Ruby

Page 30: Intro to CouchDB

CouchRest

Page 31: Intro to CouchDB

Corecouch = CouchRest.new("http://127.0.0.1:5984")db = couch.database!('NWRUG')db.save_doc({ 'title' => 'Intro to CouchDB', 'speaker' => 'Ben Aldred', 'location' => 'BBC', 'tags' => ['couchdb', 'ruby']})

Page 32: Intro to CouchDB

Moreclass Presentation < CouchRest::ExtendedDocument include ::CouchRest::Validation

property :title property :speaker property :location

validates_present :titleend

Page 33: Intro to CouchDB

More

Presentation.firstPresentation.allPresentation.by_title(:key => 'Ben Aldred')Presentation.get('intro_to_couchdb')

Page 34: Intro to CouchDB

More

property :questions, :cast_as => ['Question']property :speaker, :cast_as => 'Person'property :started_at, :cast_as => 'Time'

Page 35: Intro to CouchDB

More

view_by :started_at, :descending => trueview_by :speaker, :title

Page 36: Intro to CouchDB

Moreview_by :name, :map => "function(doc) { if(doc.date && doc.title) { emit(doc.date, doc.title); } }"

Page 37: Intro to CouchDB

Loads More

but I have not got time

Page 38: Intro to CouchDB

Resources•http://books.couchdb.org/relax/•http://github.com/jchris/couchrest•http://couch.io•http://labs.mudynamics.com/wp-content/uploads/2009/04/icouch.html •http://geekmade.co.uk