Introduction à CouchDB

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

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": "dupont@herge.fr" } ], "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}

Avec Futon

http://127.0.0.1:5984/_utils/

futon

Démo : import de données

MySQL to CouchDB, SQL to REST

Vues

map reduce schema
From blog.jteam.nl

Démo : tous les contacts

Map

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

Démo : nuage de tags

Map

function(doc) { if(doc.tags && doc.tags.length > 0) { var tags = doc.tags.split(/[  ]/g); for(var idx in tags) emit(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

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 }

CouchApps

http://glitterbacon.couchone.com/couchappspora/_design/couchappspora/index.html GET /couchappspora/_design/couchappspora/index.html HTTP/1.1 Host: couchone.com 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 [...]

De nouvelles approches

Ressources