Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

23
Nils Adermann [email protected]

description

These are the slides for the Apache CouchDB talk I held at the PHP Usergroup Karlsruhe's meeting on January 27th, 2009.

Transcript of Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

Page 1: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

NilsAdermann [email protected]

Page 2: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

CouchDBist...

keine relationale Datenbank

eine dokument-orientierte Datenbank

für das Web gemacht

skalierbar

Page 3: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

Rechnung, Brief, Visitenkarte, …

Struktur folgt nicht aus Typ:

Visitenkarte A enthält Name, Adresse und Webseite

Visitenkarte B enthält Name, E-Mail und Mobilnr.

natürliche und semi-strukturierte Daten

WasisteinDokument?

Page 4: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

Dokument-orientiert?

Keine Tabellen mit Zeilen und Spalten

Kein festes Schema für Daten

Daten werden in semi-strukturierten Dokumenten gespeichert

Page 5: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

CouchDBDokumente

In frühen Versionen: XML

Jetzt: JSON

kompakt

portabel

leicht aus nativen Objekten erzeugbar

Page 6: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

BeispielDokument

{ "_id": ”098F6BCD4621D373CADE4E832627B4F6”, "_rev": 4134847, "type": "Rechnung", "Bedienung": "Max Mustermann", "Posten": [ {"Name": "Tagesgericht", "Preis": 5.95}, {"Name": "Getränk", "Preis": 2.50} ], "verminderte MwSt": false}

Page 7: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

ArbeitenmitDokumentenviaREST

Erstellen

Lesen

Ersetzen

Löschen

HTTP POST /db/

HTTP GET /db/D0C1DFF

HTTP PUT /db/D0C1DFF

HTTP DELETE /db/D0C1DFF

HTTP Proxies/Caches können verwendet werden

Varnish

squid

...

Page 8: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

PHPillow<?php$doc = new myBlogDocument();$doc->title = 'New blog post';$doc->text = 'Hello world.';$doc->save();$id = $doc->_id;

$doc = myBlogDocument::fetchById($id);

Page 9: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

Views

Filtern, sortieren und aggregieren

Map/Reduce

Default Engine: Javascript

Aktualisierung bei Abfrage

Ad-hoc views: POST /db/_slow_view

Page 10: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

ExampleDesignDocument{ _id: "_design/rechnungen", "language": "text/javascript", "views": { "all": {"map": "<map function all>"} "gesamt_umsatz": { "map": "<map function umsatz>", "reduce": "<reduce function>" }

}}

Page 11: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

Exampleviewresult(all)

GET /db/_view/rechnungen/all

Key Value[2008, 12, 10, 19, 17, 23] {_id:...}[2008, 12, 22, 21, 43, 40] {_id:...}[2009, 1, 3, 22, 10, 45] {_id:...}[2009, 1, 5, 20, 56, 19] {_id:...}

Page 12: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

ViewFunctionallfunction(doc) { if (doc.type == "rechnung") emit(doc.date, doc);}

Page 13: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

Examplereduceresult

Key Valuenull 12345

GET /db/_view/rechnungen/gesamt_umsatz

Page 14: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

Examplereduceresult

GET /db/_view/rechnungen/gesamt_umsatz?group_level=1

Key Value[2008] 10000[2009] 2345

Page 15: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

ViewFunctions:Gesamtumsatzmap: function(doc) { if (doc.type == "rechnung") for (var posten in doc.Posten) emit(doc.date, posten.Preis);}

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

Page 16: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

Replikation

Pull und Push (bi-direktional)

Inkrementell

POST /_replicate

{ "source":"URI/lokaler Name", "target":"URI/lokaler Name"}

Einschließlich Applikationslogik - Views

Page 17: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

Offlinearbeiten

Partielle Replikation von großen Datenbeständen

Applikation kann offline weiter genutzt werden

Änderungen werden später zurück gespielt

Konfliktlösung manuell oder automatisch durch Applikation

Page 18: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

LoadBalancing

Master-Slave Replikation

Regelmäßige Synchronisation

HTTP Load Balancer (z.B. nginx)

Page 19: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

Konflikte

Konflikt-Flag

Alle Revisionen werden gespeichert

Eine „winning“ Revision – identisch auf allen Instanzen bei mehreren Konflikten

Konfliktdokumente werden mit repliziert

Compaction

Page 20: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

Zukunftssicher

Cluster Of Unreliable Commodity Hardware DB

In Erlang geschrieben

Lock-freie Architektur

MVCC (Multiversion Concurrency Control)

ACID (Atomicity, Consistency, Isolation, Durability)

Als verteiltes System geplant – anders als RDBMS

Page 21: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

Projektgeschichte

Ideen aus Lotus Notes

Damien Katz hat CouchDB zunächst in C++ begonnen

Jetzt Open Source Apache Projekt

Von IBM unterstützt

Page 22: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

Futon...

Page 23: Apache CouchDB at PHPUG Karlsruhe, Germany (Jan 27th 2009)

VielenDank!

Fragen?

Links:

CouchDB: http://couchdb.org

PHPillow: http://kore-nordmann.de/projects/phpillow/Vielen Dank an Jan Lehnardt http://jan.prima.de/