5. Key Features
HTTP + JSON
Multi-Master Replikation
AP aus CAP
Append-only
inkrementelle, materialisierte Views (map/reduce)
Mittwoch, 16. November 11
8. 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
9. create A Document
$ curl POST -d '{"hello":"world"}' http://127.0.0.1:5984/
test_db ↵
{"ok":true,"id":"67729d5013cf3f8858eb29cb17e5a723","rev":"1-1
5f65339921e497348be384867bb940f"}
201
read
$ curl GET http://127.0.0.1:5984/test_db/
67729d5013cf3f8858eb29cb17e5a723 ↵
{"_id":"67729d5013cf3f8858eb29cb17e5a723","_rev":"1-15f653399
21e497348be384867bb940f","hello":"world"}
200
Mittwoch, 16. November 11
10. 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-2
873c60120aa6dbe2204cd2c1a0e8722"}
201
$ curl GET http://127.0.0.1:5984/test_db/
2f41f56816191b94ecbd127151faa242 ↵
{"_id":"2f41f56816191b94ecbd127151faa242","_rev":"2-2873c6012
0aa6dbe2204cd2c1a0e8722","hello":"world","message":"about to
show CRUD in couch"}
200
Mittwoch, 16. November 11
12. 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 Diary
http://upondata.com:5984/afgwar/_design/afgwardiary/
index.html
Mittwoch, 16. November 11
16. 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
17. 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
18. 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
19. 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
20. 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
22. 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
23. 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