NoSQL CGN: CouchDB (11/2011)

26
Relax! Sebastian Cohnen @tisba / tisba.de Mittwoch, 16. November 11

description

Kleine Einführung in CouchDB auf der NoSQL Usergroup Cologne am 2.11.2011.

Transcript of NoSQL CGN: CouchDB (11/2011)

Page 1: NoSQL CGN: CouchDB (11/2011)

Relax!Sebastian Cohnen@tisba / tisba.de

Mittwoch, 16. November 11

Page 2: NoSQL CGN: CouchDB (11/2011)

About Me

• Freier Entwickler

❤ Ruby/Rails & node.js

❤ Performance & Scalability Engineering

❤ Distributed Load Testing

❤ CouchDB, Redis & Riak

und ebenfalls interessiert an Erlang, AMQP, ...

Mittwoch, 16. November 11

Page 3: NoSQL CGN: CouchDB (11/2011)

Users of Couchdb

BBC

meebo

UbuntuOne

...

adcloud

Mittwoch, 16. November 11

Page 4: NoSQL CGN: CouchDB (11/2011)

CouchDB: Basics

Schema-frei

Dokumenten-orientiert

Key/Value-Speicher

fehlertolerant & robust

Mittwoch, 16. November 11

Page 5: NoSQL CGN: CouchDB (11/2011)

Key Features

HTTP + JSON

Multi-Master Replikation

AP aus CAP

Append-only

inkrementelle, materialisierte Views (map/reduce)

Mittwoch, 16. November 11

Page 6: NoSQL CGN: CouchDB (11/2011)

HTTP & JSON

Mittwoch, 16. November 11

Page 7: NoSQL CGN: CouchDB (11/2011)

Admin Interface

Mittwoch, 16. November 11

Page 8: NoSQL CGN: CouchDB (11/2011)

Create A Database

$ curl -w%{http_code} -X PUT http://127.0.0.1:5984/test_db ↵{"ok":true}201

$ $ curl -w%{http_code} -X GET http://127.0.0.1:5984/test_db ↵{"db_name":"test_db","doc_count":0,"doc_del_count":0, "update_seq":1,"purge_seq":0, "compact_running":false, "disk_size":16473, "instance_start_time": "1253091887127542","disk_format_version":4}200

$ alias curl='curl -w%{http_code} -X'*Mittwoch, 16. November 11

Page 9: NoSQL CGN: CouchDB (11/2011)

$ curl POST -d '{"hello":"world"}' http://127.0.0.1:5984/test_db ↵{"ok":true,"id":"67729d5013cf3f8858eb29cb17e5a723","rev":"1-15f65339921e497348be384867bb940f"}201

$ curl GET http://127.0.0.1:5984/test_db/67729d5013cf3f8858eb29cb17e5a723 ↵{"_id":"67729d5013cf3f8858eb29cb17e5a723","_rev":"1-15f65339921e497348be384867bb940f","hello":"world"}200

read

create A Document

Mittwoch, 16. November 11

Page 10: NoSQL CGN: CouchDB (11/2011)

update$ curl PUT -d \'{"_rev":"1-15f65339921e497348be384867bb940f", "hello":"world","message":"about to show CRUD in couch"}' http://127.0.0.1:5984/test_db/ \ 2f41f56816191b94ecbd127151faa242 ↵{"ok":true,"id":"2f41f56816191b94ecbd127151faa242","rev":"2-2873c60120aa6dbe2204cd2c1a0e8722"}201

$ curl GET http://127.0.0.1:5984/test_db/ \ 2f41f56816191b94ecbd127151faa242 ↵{"_id":"2f41f56816191b94ecbd127151faa242","_rev":"2-2873c60120aa6dbe2204cd2c1a0e8722","hello":"world","message":"about to show CRUD in couch"}200

Mittwoch, 16. November 11

Page 11: NoSQL CGN: CouchDB (11/2011)

$ curl DELETE http://127.0.0.1:5984/test_db/ \ 67729d5013cf3f8858eb29cb17e5a723? \ rev=1-15f65339921e497348be384867bb940f ↵{"ok":true,"id":"67729d5013cf3f8858eb29cb17e5a723","rev":"2-2d715caed35974bb33de24d1d6c95779"}200

delete

$ curl GET http://127.0.0.1:5984/test_db/ \ 67729d5013cf3f8858eb29cb17e5a723 ↵{"error":"not_found","reason":"deleted"}404

Mittwoch, 16. November 11

Page 12: NoSQL CGN: CouchDB (11/2011)

CouchApps

Dokumente können Attachments haben

CouchDB als HTTP Server

HTML+CSS+JS ausgeliefert von CouchDB greift via HTTP auf CouchDB als Datenbank zu

Beispiel: Kabul War Diaryhttp://upondata.com:5984/afgwar/_design/afgwardiary/index.html

Mittwoch, 16. November 11

Page 13: NoSQL CGN: CouchDB (11/2011)

Replikation

Mittwoch, 16. November 11

Page 14: NoSQL CGN: CouchDB (11/2011)

Replikation

Replikation läuft via HTTP

asynchron

inkrementell (online-offline)

automatische Konfliktlösung

Multi-Version Concurrency Control (MVCC)

Multi-Master als default

filterbar

Mittwoch, 16. November 11

Page 15: NoSQL CGN: CouchDB (11/2011)

Map/Reduce Views

Mittwoch, 16. November 11

Page 16: NoSQL CGN: CouchDB (11/2011)

Accessing Data

direkter Zugriff via http://server/DATABASE/DOC-ID

materialisierte Views

bestehen aus einer map-Phase

und einer optionalen reduce-Phase

show & list: (Dokument) Transformationen

filters, externals, ...

Mittwoch, 16. November 11

Page 17: NoSQL CGN: CouchDB (11/2011)

Views

sortiert, inkrementell, effizienter Zugriff via B-Trees

build-in Viewserver:

JavaScript (Spidermonkey)

Erlang

3rd Party Viewserver in Ruby, Python, Java, Perl, ...

Mittwoch, 16. November 11

Page 18: NoSQL CGN: CouchDB (11/2011)

map/reduce

map: Eine Funktion, welche key/value Paare in einen Index schreibt

Beispiel Tag-Cloud:

1 function(doc) {2 if (doc.tags) {3 for(var idx in doc.tags) {4 emit(doc.tags[idx], 1);5 }6 }7 }

Mittwoch, 16. November 11

Page 19: NoSQL CGN: CouchDB (11/2011)

map example

{“body”:”[...]”, “tags”:[“erlang”, “couchdb”]}{“body”:”[...]”, “tags”:[“couchdb”, “talk”, “rails”]}

map-function(doc)

{“key”:“couchdb”, “value”:1}{“key”:“couchdb”, “value”:1}{“key”:“erlang”, “value”:1}{“key”:“rails”, “value”:1}{“key”:“talk”, “value”:1}

* indices are automatically sorted

Mittwoch, 16. November 11

Page 20: NoSQL CGN: CouchDB (11/2011)

map/reduce

reduce wird verwendet, um das Ergebnis der map-Phase zu verarbeiten

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

Mittwoch, 16. November 11

Page 21: NoSQL CGN: CouchDB (11/2011)

reduce example

{"key":"erlang","value":1}{"key":"couchdb","value":2}{"key":"talk","value":1}{"key":"rails","value":1}

{“key”:“erlang”: “value”:1}{“key”:“couchdb”: “value”:1}{“key”:“couchdb”: “value”:1}{“key”:“talk”: “value”:1}{“key”:“rails”: “value”:1}

reduce function(...)

Mittwoch, 16. November 11

Page 22: NoSQL CGN: CouchDB (11/2011)

CouchDB: Pros

HTTP (Proxy, Caches, Loadbalancer, ...)

HTTP (CouchDB als Webserver, 2-tier Apps!)

Replikation

Views sind sortiert; effizienter Zugriff auf Ranges

Mittwoch, 16. November 11

Page 23: NoSQL CGN: CouchDB (11/2011)

Cons

Append-only erfordert Compaction

Append-only & MVCC/Konfliktlösung führen zu Probleme bei Update-heavy Anwendungen

keine verteilte Datenbank (sharding, clustering)

Mittwoch, 16. November 11

Page 24: NoSQL CGN: CouchDB (11/2011)

Get it!

http://couchdb.apache.org/downloads.html

brew install couchdb / apt-get install couchdb / ...

freies Hosting: www.iriscouch.com

BigCouch (cloudant.com / https://github.com/cloudant/bigcouch)

dynamo-style CouchDB Cluster

Couchbase (www.couchbase.com)

Membase+CouchDB

Mittwoch, 16. November 11

Page 25: NoSQL CGN: CouchDB (11/2011)

Thanks! Q & A?

?Sebastian Cohnen

@tisba

Mittwoch, 16. November 11

Page 26: NoSQL CGN: CouchDB (11/2011)

Understanding reduce

1 function(keys, values, rereduce) {}

a b c d | e f g h | i j k l | m n1 2 1 3 | 1 2 3 1 | 2 3 3 1 | 1 8 7 7 | 9 9

14 1832

function( [[“a”,id1],[“b”,id2],[“c”,id3],[“d”,id4]], [1, 2, 1, 3], false) {...}

function(null, [ 7, 7], true) {...} function(null, [ 9, 9], true) {...} function(null, [14,18], true) {...}

Mittwoch, 16. November 11