Introduction à CouchDB

http://paulgreg.me/training-couchdb/full/

par Grégory PAUL - @paulgreg

Valid XHTML 1.0 Strict

Apache CouchDB

Architecture

schéma

Exemples d'utilisation

Données

Objet JSON :

{ "_id": "3c64f8626e3cde009c110ac984e3d2fe", "_rev": "1-0a81337cc29f445ebf7fddaa4014abc3", "tags": [ "Amis" ], "lastname": "DUPONT", "firstname": "Emile", "birthdate": "1978-01-13", "contacts": [ { "type": "Portable", "value": "06 75 47 61 00" }, { "type": "email", "value": "[email protected]" } ], "tags": ["amis","pro"], "country": "France", "town": "Paris", "_attachments": { "photo.jpg": { "content_type": "image/jpeg", "length": 1276983, "stub": true } } }

Accès REST

Démo : CouchDB, premiers pas

curl http://127.0.0.1:5984/ {"couchdb":"Welcome","version":"0.10.1"} curl -X GET http://127.0.0.1:5984/_all_dbs [] curl -X PUT http://127.0.0.1:5984/db {"ok":true} curl -X GET http://127.0.0.1:5984/_all_dbs [db] curl -X DELETE http://127.0.0.1:5984/db {"ok":true}

CouchDB Http Requestor

Avec Futon

http://127.0.0.1:5984/_utils/

futon

Démo : import de données

MySQL to CouchDB, SQL to REST

Vues

Démo : tous les contacts

Map

function(doc) { if (doc.lastname) emit(doc.lastname, doc); }
function(doc) { emit([doc.lastname, doc.firstname], doc); }

Démo : nuage de tags 1/2

Map

function(doc) { if(doc.tags.length > 0) { for(var idx in doc.tags) { emit(doc.tags[idx], 1); } } }

Reduce

function(keys, values) { var sum=0; for(var i=0; i < values.length; i++) sum += values[i]; return sum; } // ou sum(values) ou _count

Démo : nuage de tags 2/2

Démo : Niveau de réduction 1/2

Map

function(doc) { for(var idx in doc.addresses) { emit([doc.addresses[idx].country, doc.addresses[idx].town ],null); } }

Reduce

_count

Démo : Niveau de réduction 2/2

JOINs Redux 1/3

{ "_id": "Claire", "title": "VP of Official Attitude" } { "_id": "Mikeal", "title": "VP of Pastries and Automating Stuff" } { "_id": "Jason", "title": "VP of Hosting and Lightning" } { "_id": "team", "members": ["Claire", "Mikeal", "Jason"] }

JOINs Redux 2/3

Map :
function(doc) { if(doc.members) { doc.members.forEach(function(member) { emit(member, {_id: member}); }); } }
Résultats :
{ total_rows: 4, offset: 0, rows: [ {"key":"Claire", "value":{"_id":"Claire"}}, {"key":"Jason", "value":{"_id":"Jason"}}, {"key":"Mikeal", "value":{"_id":"Mikeal"}} ] }

JOINs Redux 3/3

&include_docs=true ⇒ résultats :
{ total_rows: 4, offset: 0, rows: [ { "key":"Claire", "value":{"_id":"Claire"}, "doc": {"_id":"Claire","title":"VP of Official Attitude"} }, { "key":"Jason", "value":{"_id":"Jason"}, "doc": {"_id":"Jason","title":"VP of Hosting and Lightning"} }, { "key":"Mikeal", "value":{"_id":"Mikeal"}, "doc": {"_id":"Mikeal","title":"VP of Pastries and Automating Stuff"} } ] }

Un exemple : GContact2

Réplication

Réplication "one-shot"

POST /_replicate HTTP/1.1 { "source":"example-database", "target":"http://example.org/example-database" }

Réplication continue

POST /_replicate HTTP/1.1 { "source":"example-database", "target":"http://example.org/example-database", "continuous":true }

Stand-alone applications / CouchApps

http://jchrisa.net/cal/_design/cal/index.html GET /cal/_design/cal/index.html HTTP/1.1 Host: jchrisa.net User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 [...] HTTP/1.1 200 OK Date: Thu, 06 May 2010 20:12:22 GMT Server: CouchDB/0.10.0 (Erlang OTP/R13B) Connection: close [...]

Autres exemples...

De nouvelles façons de faire

quote from Jan Lenhardt

Ressources