Esti Álvarez @esti
Conferencia Rails 2010
viernes 5 de noviembre de 2010
Relational DatabasesDocumental Databases
Schema-free Document
Oriented Database
viernes 5 de noviembre de 2010
Key Features
• Completely dynamic schema
• Key-value store in JSON format with B-
Tree indexing
• Append-only (all or noth...
Replication
• Bidirectional replication with built in
conflict resolution
• Offline by default
• Incremental replication
vie...
Database interaction
• Data is stored and returned in JSON
format
• Queried via HTTP RESTful API
• Index building language...
Database interaction
• Data is stored and returned in JSON
format
• Queried via HTTP RESTful API
• Index building language...
When should I use
CouchDB?
• Concurrent Distributed systems
• Systems where data consistency is
important
• Offline by defa...
Futon. http://localhost:5984
viernes 5 de noviembre de 2010
Learn by example
viernes 5 de noviembre de 2010
JSON data storage
{
"name": "Homer",
"last_names": ["Simpson", "Duff"],
"phone_numbers": {
"mobile": "555-666-777",
"work"...
JSON data storage
{
"name": "Homer",
"last_names": ["Simpson", "Duff"],
"phone_numbers": {
"mobile": "555-666-777",
"work"...
JSON data storage
{
"name": "Homer",
"last_names": ["Simpson", "Duff"],
"phone_numbers": {
"mobile": "555-666-777",
"work"...
Schema-less
{
"_id": "debd7e7385464f4874dd2a38043f7825",
"_rev": "3-839e43865b653a1ed73c3d21cc17c5dd",
"name": "Homer",
"l...
JSON data storage
{
"_id": "debd7e7385464f4874dd2a38043f7825",
"_rev": "3-839e43865b653a1ed73c3d21cc17c5dd",
"name": "Home...
HTTP RESTful API
$ curl http://localhost:5984/ypages
HTTP/1.1 404 Object Not Found
{"error":"not_found","reason":"no_db_fi...
Creating the Database
$ curl -X PUT http://localhost:5984/ypages
HTTP/1.1 201 Created
{"ok":true}
viernes 5 de noviembre d...
Creating a Document
$curl -v -X PUT http://localhost:5984/ypages/homer -d
'{"name": "Homer",
"last_names": ["Simpson", "Du...
Getting a Document
$ curl -X GET http://localhost:5984/ypages/homer
HTTP/1.1 200 OK
Etag: "1-4a9c28dc2e4227cac13872c89a787...
Updating a Document
$ curl -v -X PUT http://localhost:5984/ypages/homer -d
'{"_rev": "1-4a9c...",
"name": "Marge",
...}'
H...
Querying the database
• Views: the way to arrange data to answer
our questions
• Method to build views:
Incremental MapRed...
Find by name
function(doc) {
if (doc.name) {
emit(doc.name, null);
}
}
viernes 5 de noviembre de 2010
Find by name
$ curl '/ypages/_design/app/_view/by_name'
{"total_rows":4,"offset":0,
"rows":[
{"key":"Bart","value":null},
...
Find by name
$curl '.../by_name?key="Homer"&include_docs=true'
{"total_rows":4,"offset":2,
"rows":[
{"id":"homer",
"key":"...
Find by phone type
Map:
function (doc) {
var type;
if (doc.name && doc.phone_numbers) {
for (type in doc.phone_numbers) {
...
Count by phone type
Reduce:
function (keys, values, rereduce) {
return sum(values);
}
viernes 5 de noviembre de 2010
Count by phone type
$ curl '/ypages/_design/app/_view/by_phone'
{"rows":[{"key":null,"value":9}]}
(reduced by default)
vie...
Find by phone type
$ curl '.../by_phone?group=true'
{"rows":[
{"key":"home","value":1},
{"key":"mobile","value":4},
{"key"...
Find by phone type
$ curl '.../by_type?key="work"&reduce=false'
{"total_rows":9,"offset":5,"rows":[
{"key":"work","value":...
Many more options
• startkey, endkey, order, limit...
• View’s keys can be anything you like.
Ej. birthdate:
{"total_rows"...
viernes 5 de noviembre de 2010
CouchRest
• A RESTful CouchDB client based on
Heroku's RestClient and Couch.js
• http://github.com/couchrest/couchrest
vie...
CouchRest
• dbr=CouchRest.database("http://localhost:5984/ypages")
• record = dbr.get("homer")
=> {"name"=>"Homer", "_rev"...
viernes 5 de noviembre de 2010
Couch Potato
• http://github.com/langalex/couch_potato
• ActiveRecord-like syntax
viernes 5 de noviembre de 2010
Couch Potato
class Contact
include CouchPotato::Persistence
property :name
property :last_names
property :phone_numbers
va...
Couch Potato
contacts = CouchPotato.database.view Contact.by_phone
:key => “work”, :include_docs => true
=> [#<Contact _id...
Couch Potato
• Validations
• Callbacks
• Test suite
• ...
viernes 5 de noviembre de 2010
Simply Stored
• http://github.com/peritor/simply_stored
• Built on top of Couch Potato
• Associations: has_many, belongs_t...
CouchRest Model
• http://github.com/couchrest/couchrest_model
• For Rails 3
viernes 5 de noviembre de 2010
Where?
• Ubuntu CouchDesktop (Ubuntu 9.10)
• BBC: fault tolerant, scalable, multi-data
center key-value store.
• 32 nodes ...
Our use case
Tracking the activity on a website during a
live-broadcasted twitter-interview.
viernes 5 de noviembre de 2010
Video streaming server logs
(Flash media server)
{
"_id": "ffb297816c756289188ca61dc66d61c7",
"_rev": "1-b78bc6ddfbb1f2725...
Web server logs (Apache)
{
"_id": "fde06b5ecd70736e0deb6a22effff25a",
"_rev": "1-f496524ccba837b529426e0ad2177524",
"type"...
Questions made by citizens via Twitter
{
"_id": "219d1d871ad0b9a31b5f10302c8ac03e",
"_rev": "1-a4466ffd843164f0f538532aca7...
viernes 5 de noviembre de 2010
I want more!
• Get it! http://couchdb.apache.org/
• The Definitive Guide:
http://guide.couchdb.org/
• Case studies:
http://...
Questions?
Sparktography - http://flickr.com/photos/sparktography/65005867/
viernes 5 de noviembre de 2010
Upcoming SlideShare
Loading in...5
×

Meet Couch DB

809

Published on

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

  • Be the first to like this

No Downloads
Views
Total Views
809
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Meet Couch DB

  1. 1. Esti Álvarez @esti Conferencia Rails 2010 viernes 5 de noviembre de 2010
  2. 2. Relational DatabasesDocumental Databases Schema-free Document Oriented Database viernes 5 de noviembre de 2010
  3. 3. Key Features • Completely dynamic schema • Key-value store in JSON format with B- Tree indexing • Append-only (all or nothing) • Concurrent reads and writes • Very reliable (Erlang) viernes 5 de noviembre de 2010
  4. 4. Replication • Bidirectional replication with built in conflict resolution • Offline by default • Incremental replication viernes 5 de noviembre de 2010
  5. 5. Database interaction • Data is stored and returned in JSON format • Queried via HTTP RESTful API • Index building language: Javascript viernes 5 de noviembre de 2010
  6. 6. Database interaction • Data is stored and returned in JSON format • Queried via HTTP RESTful API • Index building language: Javascript “Django may be built for theWeb, but CouchDB is built of the Web. I’ve never seen software that so completely embraces the philosophies behind HTTP.“ Creator of Django viernes 5 de noviembre de 2010
  7. 7. When should I use CouchDB? • Concurrent Distributed systems • Systems where data consistency is important • Offline by default viernes 5 de noviembre de 2010
  8. 8. Futon. http://localhost:5984 viernes 5 de noviembre de 2010
  9. 9. Learn by example viernes 5 de noviembre de 2010
  10. 10. JSON data storage { "name": "Homer", "last_names": ["Simpson", "Duff"], "phone_numbers": { "mobile": "555-666-777", "work": "666-777-555" }, "interests": ["beer", "donuts", "couches"] } viernes 5 de noviembre de 2010
  11. 11. JSON data storage { "name": "Homer", "last_names": ["Simpson", "Duff"], "phone_numbers": { "mobile": "555-666-777", "work": "666-777-555" }, "interests": ["beer", "donuts", "couches"] } viernes 5 de noviembre de 2010
  12. 12. JSON data storage { "name": "Homer", "last_names": ["Simpson", "Duff"], "phone_numbers": { "mobile": "555-666-777", "work": "666-777-555" }, "interests": ["beer", "donuts", "couches"] } viernes 5 de noviembre de 2010
  13. 13. Schema-less { "_id": "debd7e7385464f4874dd2a38043f7825", "_rev": "3-839e43865b653a1ed73c3d21cc17c5dd", "name": "Homer", "last_names": ["Simpson", "Duff"], "phone_numbers": { "mobile": "555-666-777", "work": "666-777-555", "bar": "777-666-555" }, "interests": ["beer", "donuts", "couches"] } viernes 5 de noviembre de 2010
  14. 14. JSON data storage { "_id": "debd7e7385464f4874dd2a38043f7825", "_rev": "3-839e43865b653a1ed73c3d21cc17c5dd", "name": "Homer", "last_names": ["Simpson", "Duff"], "phone_numbers": { "mobile": "555-666-777", "work": "666-777-555" }, "interests": ["beer", "donuts", "couches"] } viernes 5 de noviembre de 2010
  15. 15. HTTP RESTful API $ curl http://localhost:5984/ypages HTTP/1.1 404 Object Not Found {"error":"not_found","reason":"no_db_file"} viernes 5 de noviembre de 2010
  16. 16. Creating the Database $ curl -X PUT http://localhost:5984/ypages HTTP/1.1 201 Created {"ok":true} viernes 5 de noviembre de 2010
  17. 17. Creating a Document $curl -v -X PUT http://localhost:5984/ypages/homer -d '{"name": "Homer", "last_names": ["Simpson", "Duff"], "phone_numbers": { "mobile": "555-666-777", "work": "666-777-555"}, "interests": ["beer", "donuts", "couches"]}' HTTP/1.1 201 Created Etag: "1-4a9c28dc2e4227cac13872c89a7875d8" {"ok":true,"id":"homer","rev":"1-4a9c..."} viernes 5 de noviembre de 2010
  18. 18. Getting a Document $ curl -X GET http://localhost:5984/ypages/homer HTTP/1.1 200 OK Etag: "1-4a9c28dc2e4227cac13872c89a7875d8" {"_id":"homer","_rev":"1-4a9c...","name":"Homer"...} viernes 5 de noviembre de 2010
  19. 19. Updating a Document $ curl -v -X PUT http://localhost:5984/ypages/homer -d '{"_rev": "1-4a9c...", "name": "Marge", ...}' HTTP/1.1 201 Created Etag: "2-ca3a39967947f974ac021d9f1bdafd1a" {"ok":true,"id":"homer","rev":"2-ca3a39..."} viernes 5 de noviembre de 2010
  20. 20. Querying the database • Views: the way to arrange data to answer our questions • Method to build views: Incremental MapReduce using Javascript viernes 5 de noviembre de 2010
  21. 21. Find by name function(doc) { if (doc.name) { emit(doc.name, null); } } viernes 5 de noviembre de 2010
  22. 22. Find by name $ curl '/ypages/_design/app/_view/by_name' {"total_rows":4,"offset":0, "rows":[ {"key":"Bart","value":null}, {"key":"Homer","value":null}, {"key":"Lisa","value":null}, {"key":"Marge","value":null} ]} viernes 5 de noviembre de 2010
  23. 23. Find by name $curl '.../by_name?key="Homer"&include_docs=true' {"total_rows":4,"offset":2, "rows":[ {"id":"homer", "key":"Homer", "value":1, "doc": {"_id":"debd", "_rev":"3-839", "name":"Homer", "last_names":["Simpson","Duff"], "phone_numbers":{"mobile":"555-666-777", "work":"666-777-555"}, "interests":["beer","donuts","couches"] } } ] } viernes 5 de noviembre de 2010
  24. 24. Find by phone type Map: function (doc) { var type; if (doc.name && doc.phone_numbers) { for (type in doc.phone_numbers) { emit(type, 1); } } } viernes 5 de noviembre de 2010
  25. 25. Count by phone type Reduce: function (keys, values, rereduce) { return sum(values); } viernes 5 de noviembre de 2010
  26. 26. Count by phone type $ curl '/ypages/_design/app/_view/by_phone' {"rows":[{"key":null,"value":9}]} (reduced by default) viernes 5 de noviembre de 2010
  27. 27. Find by phone type $ curl '.../by_phone?group=true' {"rows":[ {"key":"home","value":1}, {"key":"mobile","value":4}, {"key":"work","value":4} ]} viernes 5 de noviembre de 2010
  28. 28. Find by phone type $ curl '.../by_type?key="work"&reduce=false' {"total_rows":9,"offset":5,"rows":[ {"key":"work","value":1,"id":"..."}, {"key":"work","value":1,"id":"..."}, {"key":"work","value":1,"id":"..."}, {"key":"work","value":1,"id":"..."} ]} viernes 5 de noviembre de 2010
  29. 29. Many more options • startkey, endkey, order, limit... • View’s keys can be anything you like. Ej. birthdate: {"total_rows":4,"offset":0, "rows":[ {"key":[2000, 10, 10], "value":"Bart"}, {"key":[1998, 07, 13], "value":"Lisa"}, {"key":[1950, 05, 10], "value":"Homer"}, {"key":[1950, 02, 12], "value":"Marge"} ]} viernes 5 de noviembre de 2010
  30. 30. viernes 5 de noviembre de 2010
  31. 31. CouchRest • A RESTful CouchDB client based on Heroku's RestClient and Couch.js • http://github.com/couchrest/couchrest viernes 5 de noviembre de 2010
  32. 32. CouchRest • dbr=CouchRest.database("http://localhost:5984/ypages") • record = dbr.get("homer") => {"name"=>"Homer", "_rev"=>"3-b87d", "_id"=>"homer"} • record['name'] = "Moo" => "Moo" • record.save => true viernes 5 de noviembre de 2010
  33. 33. viernes 5 de noviembre de 2010
  34. 34. Couch Potato • http://github.com/langalex/couch_potato • ActiveRecord-like syntax viernes 5 de noviembre de 2010
  35. 35. Couch Potato class Contact include CouchPotato::Persistence property :name property :last_names property :phone_numbers validates_presence_of :name view :all, :key => :name view :by_phone, :map => "function(doc) { var phone_type; if (doc.name && doc.phone_numbers) { for (phone_type in doc.phone_numbers) { emit(phone_type, 1); } } }" viernes 5 de noviembre de 2010
  36. 36. Couch Potato contacts = CouchPotato.database.view Contact.by_phone :key => “work”, :include_docs => true => [#<Contact _id: "8f4aa”...>, ...] contacts.each do |contact| contact.name = “Dr. #{contact.name}” CouchPotato.database.save_document contact end viernes 5 de noviembre de 2010
  37. 37. Couch Potato • Validations • Callbacks • Test suite • ... viernes 5 de noviembre de 2010
  38. 38. Simply Stored • http://github.com/peritor/simply_stored • Built on top of Couch Potato • Associations: has_many, belongs_to • Sintactic sugar: find_by_name viernes 5 de noviembre de 2010
  39. 39. CouchRest Model • http://github.com/couchrest/couchrest_model • For Rails 3 viernes 5 de noviembre de 2010
  40. 40. Where? • Ubuntu CouchDesktop (Ubuntu 9.10) • BBC: fault tolerant, scalable, multi-data center key-value store. • 32 nodes in two datacenters. • A terabyte of data/day • ~ 5 billion of reqs/year. 150 million reqs/day viernes 5 de noviembre de 2010
  41. 41. Our use case Tracking the activity on a website during a live-broadcasted twitter-interview. viernes 5 de noviembre de 2010
  42. 42. Video streaming server logs (Flash media server) { "_id": "ffb297816c756289188ca61dc66d61c7", "_rev": "1-b78bc6ddfbb1f27259c333694d16ba6a", "type": "streaming", "datetime": "2010-03-26 18:15:56", "x-event": "pause", "c-client-id": "872845283", ... } http://www.irekia.euskadi.net viernes 5 de noviembre de 2010
  43. 43. Web server logs (Apache) { "_id": "fde06b5ecd70736e0deb6a22effff25a", "_rev": "1-f496524ccba837b529426e0ad2177524", "type": "web", "datetime": "2010-10-29 08:32:01", "path": "es/web_tv/agentes-sociales-economicos-suscriben-pacto-social-vivienda", "user-agent": "Mozilla/4.0 ...", ... } viernes 5 de noviembre de 2010
  44. 44. Questions made by citizens via Twitter { "_id": "219d1d871ad0b9a31b5f10302c8ac03e", "_rev": "1-a4466ffd843164f0f538532aca7d10c6", "datetime": "2010-10-26 18:24:37", "text": "#patxilopez Yo queria preguntar sobre un tema de actualidad q se esta hablando mucho en la calle: Cuando tendremos Bob Esponja en Euskera?", "type": "tweet" ... } viernes 5 de noviembre de 2010
  45. 45. viernes 5 de noviembre de 2010
  46. 46. I want more! • Get it! http://couchdb.apache.org/ • The Definitive Guide: http://guide.couchdb.org/ • Case studies: http://www.couchone.com/case-studies viernes 5 de noviembre de 2010
  47. 47. Questions? Sparktography - http://flickr.com/photos/sparktography/65005867/ viernes 5 de noviembre de 2010
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×