apache couchdb presentation @ sept. 2104 gtalug meeting
TRANSCRIPT
![Page 2: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/2.jpg)
What is CouchDB?
![Page 3: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/3.jpg)
Apache CouchDB is a database that uses JSON for documents,
JavaScript for MapReduce indexes, and regular HTTP for its
API.
![Page 4: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/4.jpg)
Django may be built for the Web, but CouchDB is built of the
Web.— Jacob Kaplan-Moss, Django Developer
![Page 5: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/5.jpg)
Document Based Key/Value Store
Unlike a relational database (i.e. Postgres & MySQL), CouchDB doesn’t store it’s data and relationships in tables. Instead, each database is a collection of independent JSON documents.
![Page 6: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/6.jpg)
Data Types
— "string": "abcdefghijklmnopqrstuvwxyz"
— "number": 123
— "float": 123.45
— "dict": {}
— "list": []
— "bool": true
![Page 7: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/7.jpg)
{ "_id": "30b0ed91384411e4af34c42c03094720", "_rev": "1-3573aeb5384411e4b121c42c03094720", "name": { "given_name": "Myles", "family_name": "Braithwaite" }, "emails": [ { "type": "personal", "email": "[email protected]" }, { "type": "work", "email": "[email protected]" } ]}
![Page 8: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/8.jpg)
HTTP Based API for Interacting with your Data
— Create = INSERT = PUT
— Retrieve = SELECT = GET
— Update = UPDATE = POST
— Delete = DELETE = DELETE
![Page 9: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/9.jpg)
Examples
— Written in Python using the Kenneth Reitz's requests library.
from myles_custom_urllib_parse import urljoin
from requests import get, post, put, delete, request
COUCHDB_URL = "http://127.0.0.1:5984/"DB_NAME = "contacts"
![Page 10: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/10.jpg)
r = requests.get(COUCHDB_URL)
print r.json()
{ "couchdb": "Welcome", "uuid": "f9d2966e384711e499cfc42c03094720", "vendor": { "name": "The Apache Software Foundation", "version": "1.4.0" }, "version": "1.4.0"}
![Page 11: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/11.jpg)
Create a Database
r = put(urljoin(COUCHDB_URL, DB_NAME))
if not r.status_code == 201: print ERROR_RESPONSE[r.status_code]
print r.json()
{"ok": true}
![Page 12: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/12.jpg)
Create
data = {"name": {"first_name": "Myles", "last_name": "Braithwaite"}}
r = post(urljoin(COUCHDB_URL, DB_NAME), data)
if not r.status_code == 201: print ERROR_RESPONSE[r.status_code]
print r.json()
{"ok": true, "id": "30b0ed91384411e4af34c42c03094720", "rev": "1-3573aeb5384411e4b121c42c03094720"}
![Page 13: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/13.jpg)
Create (with a non-automatic ID)
data = {"name": {"first_name": "Myles", "last_name": "Braithwaite"}}
DOC_ID = "9999-myles-braithwaite"
r = put(urljoin(COUCHDB_URL, DB_NAME, DOC_ID), data)
if not r.status_code == 201: print ERROR_RESPONSE[r.status_code]
print r.json()
{"ok": true, "id": "30b0ed91384411e4af34c42c03094720", "rev": "1-3573aeb5384411e4b121c42c03094720"}
![Page 14: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/14.jpg)
Retrieve
DOC_ID = "30b0ed91384411e4af34c42c03094720"
r = get(urljoin(COUCHDB_URL, DB_NAME, DOC_ID))
if not r.ok: print ERROR_RESPONSE[r.status_code]
r.json()
![Page 15: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/15.jpg)
Update
DOC_ID = "30b0ed91384411e4af34c42c03094720"
r = get(urljoin(COUCHDB_URL, DB_NAME, DOC_ID))data = r.json()
data['emails']: [ { "type": "Personal", "email": "[email protected]", "type": "Work", "email": "[email protected]" }]
r = post(urljoin(COUCHDB_URL, DB_NAME, DOC_ID), data)
if r.ok: print ERROR_RESPONSE[r.status_code]
r.json()
{"ok": true, "id": "30b0ed91384411e4af34c42c03094720", "rev": "2-f57cf4d1384a11e4a3edc42c03094720"}
![Page 16: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/16.jpg)
Delete
DOC_ID = "myles-braithwaite"
r = delete( urljoin(COUCHDB_URL, DB_NAME, DOC_ID) + "?rev=%s" % DOC_REV) )
![Page 17: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/17.jpg)
Attachment
curl -vX \ PUT $COUCHDB_URL/$DB_NAME/$DOC_ID/headshot.jpg?rev=$REV \ --data-binary @avatar.jpg -H "Content-Type: image/jpg"
![Page 18: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/18.jpg)
Copy
r = request( 'COPY', urljoin(COUCHDB_URL, DB_NAME, DOC_ID), {'destination': 'new-document'} )
![Page 19: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/19.jpg)
Other Features
— Replication
— Document Revisions
— Futon (similar to PHPMyAdmin)
— Auth (Basic, Cookie, Database)
— JavaScript based Map/Reduce
![Page 20: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/20.jpg)
PouchDB
![Page 21: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/21.jpg)
JavaScript clone of CouchDB that can run well within a web
browser.
![Page 22: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/22.jpg)
var db = new PouchDB('contacts');
db.put({ _id: 'myles-braithwaite', first_name: 'Myles', last_name: 'Braithwaite'})
db.replicate.to('http://127.0.0.1:5984/contacts/)
![Page 23: Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting](https://reader034.vdocuments.mx/reader034/viewer/2022042615/55d70db5bb61eb7f188b456f/html5/thumbnails/23.jpg)
Questions