relax your project with couchdb

42
Relax your project with CouchDB Benoît Chesneau 26/05/2010 - djangocon berlin Wednesday, May 26, 2010

Upload: benoit-chesneau

Post on 18-Nov-2014

118 views

Category:

Documents


5 download

DESCRIPTION

Talk given at djangocon.eu 2010 berlin

TRANSCRIPT

Page 1: Relax your project with CouchDB

Relax your project with CouchDB

Benoît Chesneau26/05/2010 - djangocon berlin

Wednesday, May 26, 2010

Page 2: Relax your project with CouchDB

• Why using a “NoSQL” solution ?

• CouchDB ?

• CouchDBKit: CouchDB & Django

Wednesday, May 26, 2010

Page 3: Relax your project with CouchDB

• CouchDB developer• Maintainer of CouchDBkit,

Couchbeam, Couchapp , couchdbproxy, ...

[email protected]

Why i’m here

Wednesday, May 26, 2010

Page 4: Relax your project with CouchDB

• We don’t need “scalability” sometimes• Most of DB size < 300 GB (2 GB)• All my data could be on a machine

(comcast)• Often, DB requests are fast enough

Why NOSQL ?

Wednesday, May 26, 2010

Page 5: Relax your project with CouchDB

Agile

• Different kinds of data• Easy denormalisation• Migrate your data easily• Keep your data safe• Flexibility, Simplicity

Wednesday, May 26, 2010

Page 6: Relax your project with CouchDB

KEEP YOUR DATA SAFE

Wednesday, May 26, 2010

Page 7: Relax your project with CouchDB

Order Customer Delivery

Item Item

Ex. Shop

Wednesday, May 26, 2010

Page 8: Relax your project with CouchDB

• Oriented Document Database• REST• Map/Reduce (M/R)• Append-Only• Javascript/Erlang• Replication

CouchDB

Wednesday, May 26, 2010

Page 9: Relax your project with CouchDB

Document Oriented?

• Schema-less• JSON• Attachments

Wednesday, May 26, 2010

Page 10: Relax your project with CouchDB

JSON DOCUMENT

{ "_id": "foo", "_rev": "1-....", "url": "http://apache.couchdb.org", "vote": 1}

Wednesday, May 26, 2010

Page 11: Relax your project with CouchDB

FutonWednesday, May 26, 2010

Page 12: Relax your project with CouchDB

Map/Reduce ?

• Map (M) : Collect a list of values associated to a key

• Return a K-V list• Reduce (R) : Reduce a list of K-V in one

list of values• Rereduce

Wednesday, May 26, 2010

Page 13: Relax your project with CouchDB

MAP.JS

function(doc) { if (doc.url && doc.vote) emit(doc.url, doc.vote);}

Wednesday, May 26, 2010

Page 14: Relax your project with CouchDB

{ "total_rows": 3, "offset": 0, "rows": [{ "id": "15c92051cc81d564db4337a05087bc8d", "key": "http://apache.couchdb.org", "value": 1 }, { "id": "fa9658810d25cac893748e4ff15e7253", "key": "http://apache.couchdb.org", "value": 1 }, { "id": "1fa0c68d8455196507b8b01645e65186", "key": "http://mysql.com", "value": -1 }]}

Wednesday, May 26, 2010

Page 15: Relax your project with CouchDB

REDUCE.JS

function(keys, values, rereduce) { return sum(values);}

{ "rows": [ { "key": "http://mysql.com", "value": -1 }, { "key": "http://apache.couchdb.org", "value": 2 } ]}

Wednesday, May 26, 2010

Page 16: Relax your project with CouchDB

Réplicatuon

• Incremental• Master-Master• Continue or when needed

Wednesday, May 26, 2010

Page 17: Relax your project with CouchDB

Wednesday, May 26, 2010

Page 18: Relax your project with CouchDB

Wednesday, May 26, 2010

Page 19: Relax your project with CouchDB

Wednesday, May 26, 2010

Page 20: Relax your project with CouchDB

Wednesday, May 26, 2010

Page 21: Relax your project with CouchDB

Wednesday, May 26, 2010

Page 22: Relax your project with CouchDB

Wednesday, May 26, 2010

Page 23: Relax your project with CouchDB

Wednesday, May 26, 2010

Page 24: Relax your project with CouchDB

Wednesday, May 26, 2010

Page 25: Relax your project with CouchDB

More

• Sharding: yes• couchdb-lounge, cloudant, yours• Geocouch. Full r-tree

Wednesday, May 26, 2010

Page 26: Relax your project with CouchDB

couchdbkit

Wednesday, May 26, 2010

Page 27: Relax your project with CouchDB

CouchDBKit

• CouchDB Python framework• support CouchDB 0.10.2 & 0.11.0• Simple client• Object Document Mapping (“ODM”)• Django extension• Couchapp compatible

Wednesday, May 26, 2010

Page 28: Relax your project with CouchDB

client

from couchdbkit import Server

s = Server("http://127.0.0.1:5984")db = s["mydb"]doc = { "a": 1 }db.save_doc(doc)doc["b"] = 2db.save(doc)

doc1 = db.get(doc['_id'])

Wednesday, May 26, 2010

Page 29: Relax your project with CouchDB

Other features

• db.views• from couchdbkit.loaders import

FileSystemDocsLoader

Wednesday, May 26, 2010

Page 30: Relax your project with CouchDB

from couchdbkit.loaders import FileSystemDocsLoader

loader = FileSystemDocsLoader('/path/to/example/_design')loader.sync(db, verbose=True)

Wednesday, May 26, 2010

Page 31: Relax your project with CouchDB

Object Document Mapping

from couchdbkit.schema import *

class MyDoc(Document): a = IntegerProperty() contain(db, MyDoc)

doc = MyDoc()doc.a = 1doc.save()

doc.b = 2doc.save()

doc1 = MyDoc.get(doc._id)

Wednesday, May 26, 2010

Page 32: Relax your project with CouchDB

Django Extension

• from couchdbkt.ext.django import *• Django helper• manage.py syncdb• DocumentForm

Wednesday, May 26, 2010

Page 33: Relax your project with CouchDB

settings.py

...

COUCHDB_DATABASES = ( ('djangoapp.greeting', 'http://127.0.0.1:5984/greeting'), )

...

INSTALLED_APPS = ( ... 'couchdbkit.ext.django', 'djangoapp.greeting', ... )

Wednesday, May 26, 2010

Page 34: Relax your project with CouchDB

models.py

from datetime import datetimefrom couchdbkit.ext.django.schema import *

class Greeting(Document): author = StringProperty() content = StringProperty(required=True) date = DateTimeProperty(default=datetime.utcnow)

Wednesday, May 26, 2010

Page 35: Relax your project with CouchDB

forms.py

class GreetingForm(DocumentForm): class Meta: document = Greeting

Wednesday, May 26, 2010

Page 36: Relax your project with CouchDB

views.py

def home(request): greet = None if request.POST: form = GreetingForm(request.POST) if form.is_valid(): greet = form.save() else: form = GreetingForm() greetings = Greeting.view("greeting/all") return render("home.html", { "form": form, "greet": greet, "greetings": greetings }, context_instance=RequestContext(request))

Wednesday, May 26, 2010

Page 37: Relax your project with CouchDB

0.5

• New mapping (Objects with annotations)

• Admin integration• Possibility to attach design doc to some

objects• MultiDB & DB backend in extension• Eventlet support

Wednesday, May 26, 2010

Page 38: Relax your project with CouchDB

Wednesday, May 26, 2010

Page 39: Relax your project with CouchDB

Liens

• http://apache.couchdb.org• http://couchdbkit.org

Wednesday, May 26, 2010

Page 40: Relax your project with CouchDB

Questions

Wednesday, May 26, 2010

Page 41: Relax your project with CouchDB

@benoitc

Wednesday, May 26, 2010

Page 42: Relax your project with CouchDB

Cette création est mise à disposition selon le Contrat Paternité 2.0 France disponible en ligne http://

creativecommons.org/licenses/by/2.0/fr/ ou par courrier postal à Creative Commons, 171 Second Street, Suite

300, San Francisco, California 94105, USA.

Wednesday, May 26, 2010