39

Click here to load reader

Couch db and_the_web

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Couch db and_the_web

CouchDB and the web

Page 2: Couch db and_the_web

Knut O. Hellan

twitter.com/knuthellanknuthellan.com

Page 3: Couch db and_the_web

NoSQL

Page 4: Couch db and_the_web

NoSQL

Freedom of choice

Right tool for the job

Use relational databases when appropriate

Page 5: Couch db and_the_web

Flavor of the week

Lightweight key-value:

Graph:

Document stores:

Server spanning:  More

Page 6: Couch db and_the_web

CouchDB Overview

Page 7: Couch db and_the_web

Pros and cons

RESTful JSON API

Schema free

Incremental map reduce views

Incremental bi-directional replication

Page 8: Couch db and_the_web

Pros and cons

Robust append-only

File backup with cp and tar

Scalable?

Page 9: Couch db and_the_web

CouchDB views

Page 10: Couch db and_the_web

Our database

Page 11: Couch db and_the_web

Our database

{"_id": "123bc00725fc9f831209440793001bb6","_rev": "3-f30fca1f7fb4f387207e4756795aac74","author": "jchris","tweet": "This weekend just happened to be a good time for me to hack. Node.js CouchDB and Twitter, makes me feel like I'm part of #nodeko but I'm not","type": "tweet"}

Page 12: Couch db and_the_web

View Design

Page 13: Couch db and_the_web

Default mapper

function(doc) {  emit(null, doc);}

Page 14: Couch db and_the_web

Indexer

function(doc) {  if (!doc.tweet) return;

  var clean_text = doc.tweet.replace(/\W/g, ' ').replace(/  /g, ' ');  var lower_text = clean_text.toLowerCase();  var text_vector = lower_text.split(/ /);  for (var i in text_vector) {    var word = text_vector[i];    if (word.length > 2) emit(word, doc._id);  }}

Page 15: Couch db and_the_web

The mapper output

http://localhost:5984/news/_design/parser/_view/index{"total_rows":50,"offset":0,"rows":[{"id":"123bc00725fc9f83120944079300093e","key":"9xmyxw","value":1},{"id":"123bc00725fc9f831209440793000f46","key":"a6exod","value":1},{"id":"123bc00725fc9f831209440793000f46","key":"and","value":1},{"id":"123bc00725fc9f831209440793001bb6","key":"and","value":1},{"id":"123bc00725fc9f83120944079300093e","key":"are","value":1},

http://localhost:5984/news/_design/parser/_view/index?key=%22couchdb%22{"total_rows":50,"offset":11,"rows":[{"id":"123bc00725fc9f83120944079300093e","key":"couchdb","value":1},{"id":"123bc00725fc9f831209440793000f46","key":"couchdb","value":1},{"id":"123bc00725fc9f831209440793001bb6","key":"couchdb","value":1}]}

Page 16: Couch db and_the_web

Add reducer

function(keys, values, rereduce) {  return sum(values);} http://localhost:5984/news/_design/parser/_view/index{"rows":[{"key":null,"value":50}]} http://localhost:5984/news/_design/parser/_view/index?key=%22couchdb%22{"rows":[{"key":null,"value":3}]}

Page 17: Couch db and_the_web

Grouping

http://localhost:5984/news/_design/parser/_view/index?group_level=1{"rows":[{"key":"9xmyxw","value":1},{"key":"a6exod","value":1},{"key":"and","value":2},{"key":"are","value":1},

Page 18: Couch db and_the_web

CouchDB replication

Page 19: Couch db and_the_web

Futon Replication

Page 20: Couch db and_the_web

Replication Modes

- Copy or replicate once - Continuous replication- Filtered replication

Page 21: Couch db and_the_web

Multi-Couch

Page 22: Couch db and_the_web

Offline replication

- Mobile phone

- Notebook or tablet and the cloud

Page 23: Couch db and_the_web

Conflicts

Multi-master => possible conflicts

All masters will agree

Conflict information kept in revision history

Page 24: Couch db and_the_web

Filtered Replication

function(doc, req) {if (doc.type && doc.type == "foo") {return true;} else {return false;}}

Page 25: Couch db and_the_web

Filtered Replication{"_id":"myddoc","filters": {"myfilter": "function goes here"}}

Page 26: Couch db and_the_web

Filtered Replication{  "source":"http://example.org/example-database",  "target":"http://admin:[email protected]:5984/example-database",   "filter":"myddoc/myfilter"}

Page 27: Couch db and_the_web

CouchDB scaling

Page 28: Couch db and_the_web

Alternatives

- CouchDB lounge  -  - Pillow

Page 29: Couch db and_the_web

Pillow

- Reuses CouchDB functionality - Transparent for users - Automatic resharding

Page 30: Couch db and_the_web

Pillow

Page 31: Couch db and_the_web

Pillow

Page 32: Couch db and_the_web

Pillow

Page 33: Couch db and_the_web

Pillow

Page 34: Couch db and_the_web

Pillow

Page 35: Couch db and_the_web

CouchDB lessons learned

Page 36: Couch db and_the_web

CouchDB Experience

Map Reduce based views are powerful

View experimentation in Futon

Three-way multi-master replication

Replication monitor

Page 37: Couch db and_the_web

CouchDB Experience

EC2

External middleware still useful

Duck typing matches CouchDB's JSON

Page 38: Couch db and_the_web

Now what?

Install and play with CouchDB

Visit couchdb.apache.org

Visit knuthellan.com

Page 39: Couch db and_the_web

relax