Weitere ähnliche Inhalte Ähnlich wie Couchdb w Ruby'm (20) Kürzlich hochgeladen (20) Couchdb w Ruby'm3. Plan prezentacji
CouchDB Ruby
● Nierelacyjna baza danych ● Couchrest
● RESTful JSON API ● Couch_foo
● Widoki (MapReduce) ● Couch_potato
● Replikacje ● Reverse proxy
teoria praktyka
20.04.2010 Stanisław Wasiutyński 3
4. 20.04.2010 Stanisław Wasiutyński 4
Źródło: http://blog.nahurst.com/visual-guide-to-nosql-systems
5. tu jestem
20.04.2010 Stanisław Wasiutyński 5
Źródło: http://blog.nahurst.com/visual-guide-to-nosql-systems
6. Document-Oriented
● Wszystkie dane są przechowywane w formie dokumentów
o dowolnym rozmiarze.
● Każdy dokument ma unikalną nazwę, oraz posiada
dowolną liczbę pól o unikalnej nazwie.
● Każdemu polu (kluczowi) odpowiada wartość dowolnego
typu (np. String, Integer, Array, Hash).
● Na dokumentach wykonywane są operacje CRUD (Create,
Read, Update, Delete) tylko na całym dokumencie, nigdy
na jego fragmencie.
20.04.2010 Stanisław Wasiutyński 6
7. RESTful JSON API
{
"_id": "fb288af72d22797f5cf68a73a7a5cb89",
"_rev": "12-8f531b2a273d35ecd4af58e930525e8c",
"couchrest-type": "Line",
"no": 117,
"begin_date": "2009/10/03",
"timetables": [
{
"data": {"pon-pt": [...], "sob": [...]},
"stop_id": "acf81f2770a21481bf0e784b7b506d38"
},
...
],
"updated_at": "2010/03/08 21:08:50 +0000",
"created_at": "2010/01/22 21:50:19 +0000"
}
20.04.2010 Stanisław Wasiutyński 7
8. RESTful JSON API
Stwórz: POST /db/<id>
Czytaj: GET /db/<id>
Aktualizuj: PUT /db/<id>
Usuń: DELETE /db/<id>
20.04.2010 Stanisław Wasiutyński 8
10. RESTful JSON API
[~] ➔ curl http://localhost:5984/
[info] [<0.95.0>] 127.0.0.1 - - 'GET' / 200
{"couchdb":"Welcome","version":"0.11.0"}
[~] ➔ curl -X PUT http://localhost:5984/krug
[info] [<0.124.0>] 127.0.0.1 - - 'PUT' /krug 201
{"ok":true}
[~] ➔ curl -X POST http://localhost:5984/krug -d '{"Temat": "Witaj świecie!"}'
[info] [<0.216.0>] 127.0.0.1 - - 'POST' /krug 201
{"ok":true,"id":"8cb7299cc68d35a2ca4a0c18070009be","rev":"1-4777d27f9e980ed1ce8cf94c0bc1d27a"}
[~] ➔ curl -X PUT http://localhost:5984/krug/8cb7299cc68d35a2ca4a0c18070009be
> -d '{"Temat": "Witaj okrutny świecie!"}'
[info] [<0.668.0>] 127.0.0.1 - - 'PUT' /krug/8cb7299cc68d35a2ca4a0c18070009be 409
{"error":"conflict","reason":"Document update conflict."}
[~] ➔ curl -X PUT http://localhost:5984/krug/8cb7299cc68d35a2ca4a0c18070009be
> -d '{"_rev":"1-4777d27f9e980ed1ce8cf94c0bc1d27a", "Temat": "Witaj okrutny świecie!"}'
{"ok":true,"id":"8cb7299cc68d35a2ca4a0c18070009be","rev":"2-e51a077b62b9bfbc8faac8aff60733b4"}
[info] [<0.774.0>] 127.0.0.1 - - 'PUT' /krug/8cb7299cc68d35a2ca4a0c18070009be 201
[~] ➔ curl http://localhost:5984/krug/8cb7299cc68d35a2ca4a0c18070009be
[info] [<0.1264.0>] 127.0.0.1 - - 'GET' /krug/8cb7299cc68d35a2ca4a0c18070009be 200
{"_id":"8cb7299cc68d35a2ca4a0c18070009be","_rev":"2-e51a077b62b9bfbc8faac8aff60733b4",
"Temat":"Witaj okrutny u015bwiecie!"}
[~] ➔
20.04.2010 Stanisław Wasiutyński 10
11. RESTful JSON API
[~] ➔ curl http://localhost:5984/
[info] [<0.95.0>] 127.0.0.1 - - 'GET' / 200
{"couchdb":"Welcome","version":"0.11.0"}
[~] ➔ curl -X PUT http://localhost:5984/krug
[info] [<0.124.0>] 127.0.0.1 - - 'PUT' /krug 201
{"ok":true}
[~] ➔ curl -X POST http://localhost:5984/krug -d '{"Temat": "Witaj świecie!"}'
[info] [<0.216.0>] 127.0.0.1 - - 'POST' /krug 201
{"ok":true,"id":"8cb7299cc68d35a2ca4a0c18070009be","rev":"1-4777d27f9e980ed1ce8cf94c0bc1d27a"}
[~] ➔ curl -X PUT http://localhost:5984/krug/8cb7299cc68d35a2ca4a0c18070009be
> -d '{"Temat": "Witaj okrutny świecie!"}'
[info] [<0.668.0>] 127.0.0.1 - - 'PUT' /krug/8cb7299cc68d35a2ca4a0c18070009be 409
{"error":"conflict","reason":"Document update conflict."}
[~] ➔ curl -X PUT http://localhost:5984/krug/8cb7299cc68d35a2ca4a0c18070009be
> -d '{"_rev":"1-4777d27f9e980ed1ce8cf94c0bc1d27a", "Temat": "Witaj okrutny świecie!"}'
{"ok":true,"id":"8cb7299cc68d35a2ca4a0c18070009be","rev":"2-e51a077b62b9bfbc8faac8aff60733b4"}
[info] [<0.774.0>] 127.0.0.1 - - 'PUT' /krug/8cb7299cc68d35a2ca4a0c18070009be 201
[~] ➔ curl http://localhost:5984/krug/8cb7299cc68d35a2ca4a0c18070009be
[info] [<0.1264.0>] 127.0.0.1 - - 'GET' /krug/8cb7299cc68d35a2ca4a0c18070009be 200
{"_id":"8cb7299cc68d35a2ca4a0c18070009be","_rev":"2-e51a077b62b9bfbc8faac8aff60733b4",
"Temat":"Witaj okrutny u015bwiecie!"}
[~] ➔
20.04.2010 Stanisław Wasiutyński 11
12. RESTful JSON API
[~] ➔ curl http://localhost:5984/
[info] [<0.95.0>] 127.0.0.1 - - 'GET' / 200
{"couchdb":"Welcome","version":"0.11.0"}
[~] ➔ curl -X PUT http://localhost:5984/krug
[info] [<0.124.0>] 127.0.0.1 - - 'PUT' /krug 201
{"ok":true}
[~] ➔ curl -X POST http://localhost:5984/krug -d '{"Temat": "Witaj świecie!"}'
[info] [<0.216.0>] 127.0.0.1 - - 'POST' /krug 201
{"ok":true,"id":"8cb7299cc68d35a2ca4a0c18070009be","rev":"1-4777d27f9e980ed1ce8cf94c0bc1d27a"}
[~] ➔ curl -X PUT http://localhost:5984/krug/8cb7299cc68d35a2ca4a0c18070009be
> -d '{"Temat": "Witaj okrutny świecie!"}'
[info] [<0.668.0>] 127.0.0.1 - - 'PUT' /krug/8cb7299cc68d35a2ca4a0c18070009be 409
{"error":"conflict","reason":"Document update conflict."}
[~] ➔ curl -X PUT http://localhost:5984/krug/8cb7299cc68d35a2ca4a0c18070009be
> -d '{"_rev":"1-4777d27f9e980ed1ce8cf94c0bc1d27a", "Temat": "Witaj okrutny świecie!"}'
{"ok":true,"id":"8cb7299cc68d35a2ca4a0c18070009be","rev":"2-e51a077b62b9bfbc8faac8aff60733b4"}
[info] [<0.774.0>] 127.0.0.1 - - 'PUT' /krug/8cb7299cc68d35a2ca4a0c18070009be 201
[~] ➔ curl http://localhost:5984/krug/8cb7299cc68d35a2ca4a0c18070009be
[info] [<0.1264.0>] 127.0.0.1 - - 'GET' /krug/8cb7299cc68d35a2ca4a0c18070009be 200
{"_id":"8cb7299cc68d35a2ca4a0c18070009be","_rev":"2-e51a077b62b9bfbc8faac8aff60733b4",
"Temat":"Witaj okrutny u015bwiecie!"}
[~] ➔
20.04.2010 Stanisław Wasiutyński 12
13. RESTful JSON API
[~] ➔ curl http://localhost:5984/
[info] [<0.95.0>] 127.0.0.1 - - 'GET' / 200
{"couchdb":"Welcome","version":"0.11.0"}
[~] ➔ curl -X PUT http://localhost:5984/krug
[info] [<0.124.0>] 127.0.0.1 - - 'PUT' /krug 201
{"ok":true}
[~] ➔ curl -X POST http://localhost:5984/krug -d '{"Temat": "Witaj świecie!"}'
[info] [<0.216.0>] 127.0.0.1 - - 'POST' /krug 201
{"ok":true,"id":"8cb7299cc68d35a2ca4a0c18070009be","rev":"1-4777d27f9e980ed1ce8cf94c0bc1d27a"}
[~] ➔ curl -X PUT http://localhost:5984/krug/8cb7299cc68d35a2ca4a0c18070009be
> -d '{"Temat": "Witaj okrutny świecie!"}'
[info] [<0.668.0>] 127.0.0.1 - - 'PUT' /krug/8cb7299cc68d35a2ca4a0c18070009be 409
{"error":"conflict","reason":"Document update conflict."}
[~] ➔ curl -X PUT http://localhost:5984/krug/8cb7299cc68d35a2ca4a0c18070009be
> -d '{"_rev":"1-4777d27f9e980ed1ce8cf94c0bc1d27a", "Temat": "Witaj okrutny świecie!"}'
{"ok":true,"id":"8cb7299cc68d35a2ca4a0c18070009be","rev":"2-e51a077b62b9bfbc8faac8aff60733b4"}
[info] [<0.774.0>] 127.0.0.1 - - 'PUT' /krug/8cb7299cc68d35a2ca4a0c18070009be 201
[~] ➔ curl http://localhost:5984/krug/8cb7299cc68d35a2ca4a0c18070009be
[info] [<0.1264.0>] 127.0.0.1 - - 'GET' /krug/8cb7299cc68d35a2ca4a0c18070009be 200
{"_id":"8cb7299cc68d35a2ca4a0c18070009be","_rev":"2-e51a077b62b9bfbc8faac8aff60733b4",
"Temat":"Witaj okrutny u015bwiecie!"}
[~] ➔
20.04.2010 Stanisław Wasiutyński 13
14. RESTful JSON API
[~] ➔ curl http://localhost:5984/
[info] [<0.95.0>] 127.0.0.1 - - 'GET' / 200
{"couchdb":"Welcome","version":"0.11.0"}
[~] ➔ curl -X PUT http://localhost:5984/krug
[info] [<0.124.0>] 127.0.0.1 - - 'PUT' /krug 201
{"ok":true}
[~] ➔ curl -X POST http://localhost:5984/krug -d '{"Temat": "Witaj świecie!"}'
[info] [<0.216.0>] 127.0.0.1 - - 'POST' /krug 201
{"ok":true,"id":"8cb7299cc68d35a2ca4a0c18070009be","rev":"1-4777d27f9e980ed1ce8cf94c0bc1d27a"}
[~] ➔ curl -X PUT http://localhost:5984/krug/8cb7299cc68d35a2ca4a0c18070009be
> -d '{"Temat": "Witaj okrutny świecie!"}'
[info] [<0.668.0>] 127.0.0.1 - - 'PUT' /krug/8cb7299cc68d35a2ca4a0c18070009be 409
{"error":"conflict","reason":"Document update conflict."}
[~] ➔ curl -X PUT http://localhost:5984/krug/8cb7299cc68d35a2ca4a0c18070009be
> -d '{"_rev":"1-4777d27f9e980ed1ce8cf94c0bc1d27a", "Temat": "Witaj okrutny świecie!"}'
{"ok":true,"id":"8cb7299cc68d35a2ca4a0c18070009be","rev":"2-e51a077b62b9bfbc8faac8aff60733b4"}
[info] [<0.774.0>] 127.0.0.1 - - 'PUT' /krug/8cb7299cc68d35a2ca4a0c18070009be 201
[~] ➔ curl http://localhost:5984/krug/8cb7299cc68d35a2ca4a0c18070009be
[info] [<0.1264.0>] 127.0.0.1 - - 'GET' /krug/8cb7299cc68d35a2ca4a0c18070009be 200
{"_id":"8cb7299cc68d35a2ca4a0c18070009be","_rev":"2-e51a077b62b9bfbc8faac8aff60733b4",
"Temat":"Witaj okrutny u015bwiecie!"}
[~] ➔
20.04.2010 Stanisław Wasiutyński 14
15. RESTful JSON API
[~] ➔ curl http://localhost:5984/
[info] [<0.95.0>] 127.0.0.1 - - 'GET' / 200
{"couchdb":"Welcome","version":"0.11.0"}
[~] ➔ curl -X PUT http://localhost:5984/krug
[info] [<0.124.0>] 127.0.0.1 - - 'PUT' /krug 201
{"ok":true}
[~] ➔ curl -X POST http://localhost:5984/krug -d '{"Temat": "Witaj świecie!"}'
[info] [<0.216.0>] 127.0.0.1 - - 'POST' /krug 201
{"ok":true,"id":"8cb7299cc68d35a2ca4a0c18070009be","rev":"1-4777d27f9e980ed1ce8cf94c0bc1d27a"}
[~] ➔ curl -X PUT http://localhost:5984/krug/8cb7299cc68d35a2ca4a0c18070009be
> -d '{"Temat": "Witaj okrutny świecie!"}'
[info] [<0.668.0>] 127.0.0.1 - - 'PUT' /krug/8cb7299cc68d35a2ca4a0c18070009be 409
{"error":"conflict","reason":"Document update conflict."}
[~] ➔ curl -X PUT http://localhost:5984/krug/8cb7299cc68d35a2ca4a0c18070009be
> -d '{"_rev":"1-4777d27f9e980ed1ce8cf94c0bc1d27a", "Temat": "Witaj okrutny świecie!"}'
{"ok":true,"id":"8cb7299cc68d35a2ca4a0c18070009be","rev":"2-e51a077b62b9bfbc8faac8aff60733b4"}
[info] [<0.774.0>] 127.0.0.1 - - 'PUT' /krug/8cb7299cc68d35a2ca4a0c18070009be 201
[~] ➔ curl http://localhost:5984/krug/8cb7299cc68d35a2ca4a0c18070009be
[info] [<0.1264.0>] 127.0.0.1 - - 'GET' /krug/8cb7299cc68d35a2ca4a0c18070009be 200
{"_id":"8cb7299cc68d35a2ca4a0c18070009be","_rev":"2-e51a077b62b9bfbc8faac8aff60733b4",
"Temat":"Witaj okrutny u015bwiecie!"}
[~] ➔
20.04.2010 Stanisław Wasiutyński 15
16. Coś więcej niż wymyślny
serwer plików.
20.04.2010 Stanisław Wasiutyński 16
http://www.flickr.com/photos/santos/1704875109/
17. Widoki
● Są przechowywane jako dokumenty,
● definiują dowolne funkcje map i reduce,
● służą do budowy wydajnych indeksów,
● pozwalają na niezależne przetwarzanie pojedynczych
dokumentów i zwrócenie ich w pożądanym formacie,
● generowane równolegle, inkrementalnie i na żądanie,
● brak dynamicznych zapytań („close to the metal”),
● rozbudowane API.
20.04.2010 Stanisław Wasiutyński 17
18. Map Reduce
GET /_design/comments/_view/cenzor?limit=11&reduce=false
GET /_design/comments/_view/cenzor?limit=11&reduce=false&
startkey=[<id>, <rok>, <miesiąc>, <dzień>]&
endkey=[<id>, <rok>, <miesiąc>, <dzień>+3]
GET /_design/comments/_view/cenzor?limit=11&group=true&group_level=3
GET /_design/comments/_view/cenzor?group=true&group_level=1&key=[<id>]
20.04.2010 Stanisław Wasiutyński 18
19. Replikacje
● Dwustronne, inkrementalne,
● replikacja podzbioru (przez filtry),
● rozwiązywanie konfliktów.
korzyści
● Łatwe skalowanie wszerz,
● praca off-line.
curl -X POST http://localhost:5984/_replicate -d
{"source": "http://couch.db/remote", "target": "local"}
20.04.2010 Stanisław Wasiutyński 19
20. Replikacje
20.04.2010 Stanisław Wasiutyński 20
http://www.slideshare.net/mlmilleratmit/20100310-miller-sts
22. Couchrest
Couchrest
20.04.2010 Stanisław Wasiutyński 22
30. O czym nie mówiłem?
● Couchdb-Lucene – pełno-tekstowa wyszukiwarka
dokumentów CouchDB,
● Couchdb-Lounge – proxy framework do partycjonowania
CouchDB,
● CouchApps - samowystarczalne aplikacje CouchDB,
● bezpieczeństwo,
● binarne załączniki,
● walidacja dokumentów,
● BrowserCouch – CouchDB w przeglądarce.
20.04.2010 Stanisław Wasiutyński 30
31. Więcej o CouchDB na:
● http://couchdb.apache.org/
● http://www.couch.io/
● http://wiki.apache.org/couchdb/
● http://books.couchdb.org/relax/
Oraz:
● http://stackoverflow.com/questions/tagged/couchdb
● http://www.reddit.com/search?q=couchdb
20.04.2010 Stanisław Wasiutyński 31
33. Fork me:
github.com/Stanley
20.04.2010 Stanisław Wasiutyński 33