GTALUG Presentation on CouchDB

Post on 24-Apr-2015

1,649 views 0 download

description

 

transcript

Who am I?

Myles Braithwaite

We Develop Websites

What is CouchDB?

Erlang

Apache CouchDB is a distributed, fault-tolerant and schema-free document-oriented database accessible via a RESTful HTTP/JSON API. Among other features, it provides robust, incremental replication with bi-directional conflict detection and resolution, and is queryable and indexable using a table-oriented view engine with JavaScript acting as the default view definition language.

Quote from http://couchdb.org/.

Document-Oriented Database

• Each record is store as a document.

• Any number of fields of any length.

• Fields can also contain multiple pieces of data.

<people> <person first_name="Myles" last_name="Braithwaite"> <emails> <email value="me@myles.tk"/> <email value="myles@miys.net"/> </emails> </person></people>

[ { "first_name": "Myles", "last_name": "Braithwaite", "emails": [ { "email": "me@myles.tk" }, { "email": "myles@miys.net" } ] }]

Relational Database it would require two tables

What is RESTful HTTP?

Does anyone not know what HTTP is?

REST

• Representational State Transfer

• The foundation of all Web Services

• SOAP,

• XML-RPC,

• and basic HTTP methods like:

• POST, GET, PUT, DELETE

RESTful HTTP Methods CRUD

POST Create, Update & Delete

GET Read

PUT Create & Replace

DELETE Delete

“Django may be built for the Web, but CouchDB is built of the Web. I’ve never seen software that so completely embraces the philosophies behind HTTP. CouchDB makes Django look old-school in the same way that Django makes ASP look outdated.”

— Jacob Kaplan-Moss, Django Developer

What is JSON?

• JavaScript version of XML.

• A less dramatic/lightweight version of XML.

• Google and Yahoo are using it in there Web Services.

Distributed

Bi-Directional Conflict Detection and

Resolution

Bi-Directional Conflict Detection and

Resolution

Bi-Directional Conflict Detection and

Resolution

Query Documents with JavaScript

Views

• Map Functions

• Reduce Functions

• Lookup Views -- Demo

• Complex Keys -- Demo

function(doc) {emit(null, doc);}

Map Functions

function(doc) { if (doc.first_name && doc.last_name) { var full_name = (doc.first_name + " " + doc.last_name) emit(full_name, doc); }};

Text

Map Functions

Reduce Functions

function (key, values, rereduce) { return sum(values);}

Futon

Basically the PHPMyAdmin for

CouchDB

Demo Time

$ curl -X PUT -v 'http://127.0.0.1:5984/temp_database/'

> PUT /temp_database/ HTTP/1.1> User-Agent: curl> Host: 127.0.0.1:5984> Accept: */*> < HTTP/1.1 201 Created< Server: CouchDB< Date: Tue, 09 Dec 2008 20:32:15 GMT< Content-Type: text/plain;charset=utf-8< Content-Length: 11< {"ok":true}

$ curl -X DELETE -v 'http://127.0.0.1:5984/temp_database/'

> DELETE /temp_database/ HTTP/1.1> User-Agent: curl> Host: 127.0.0.1:5984> Accept: */*> < HTTP/1.1 200 OK< Server: CouchDB< Date: Tue, 09 Dec 2008 20:35:26 GMT< Content-Type: text/plain;charset=utf-8< Content-Length: 11< {"ok":true}

$ curl 'http://127.0.0.1:5984/'

{"couchdb":"Welcome","version":"0.8.1-incubating"}

$ curl 'http://127.0.0.1:5984/address_book/'

{"db_name":"address_book","doc_count":18,"doc_del_count":2,"update_seq":117,"compact_running":false,"disk_size":242905}

$ curl 'http://127.0.0.1:5984/address_book/_all_docs/'

{"total_rows":18,"offset":0,"rows":[{"id":"1074d8805eaa79dfec356d34aa719a95","key":"1074d8805eaa79dfec356d34aa719a95","value":{"rev":"3720758831"}},...

$ curl 'http://127.0.0.1:5984/address_book/_all_docs?count=1'

{"total_rows":18,"offset":0,"rows":[{"id":"1074d8805eaa79dfec356d34aa719a95","key":"1074d8805eaa79dfec356d34aa719a95","value":{"rev":"3720758831"}}]}

curl 'http://127.0.0.1:5984/address_book/1074d8805eaa79dfec356d34aa719a95/'

{"_id":"1074d8805eaa79dfec356d34aa719a95","_rev":"3720758831","From":{"name":"Myles Braithwaite","email":"me@mylesbraithwaite.net"},"To":"tlug@ss.org","Subject":"Re: [TLUG]: Re: [TLUG-ANNOUNCE]: TLUG Meeting. Tue Sep 9","Date":"Wed, 10 Sep 2008 15:28:22 -0400","body":"..."}

$ curl 'http://127.0.0.1:5984/address_book/1074d8805eaa79dfec356d34aa719a95?rev=3720758831'

$ curl 'http://127.0.0.1:5984/address_book/_view/contacts/contact_list/'

{"total_rows":5,"offset":0,"rows":[{"id":"93a31f6756545d9d59ca53fba6f92fe0","key":"Myles Braithwaite","value":{"_id":"93a31f6756545d9d59ca53fba6f92fe0","_rev":"3685624062","fn":{"family-name":"Braithwaite","given-name":"Myles"},"email":[{"value":"me@mylesbraithwaite.net","type":"Personal"},{"value":"myles@monkeyinyoursoul.com","type":"Work"}],"org":{"organization-name":"Monkey in your Soul"}}}...

Use Cases

What CouchDB should not be used for!

• A relational database.

• A replacement for relational databases.

• An object-oriented database. Or more specifically, meant to function as a seamless persistence layer for an OO programming language.

Pictures• http://flickr.com/photos/

83737641@N00/2377691249/

• http://flickr.com/photos/dragon2309/1490657223/

• http://flickr.com/photos/herzogbr/359841353/

• http://flickr.com/photos/irisheyes/1160297224/

• http://flickr.com/photos/jessefriedman/1435220149/

• http://flickr.com/photos/tomnatt/2763592134/

• http://flickr.com/photos/theo_reth/188334429/

• http://flickr.com/photos/photonquantique/2596581870/

• http://flickr.com/photos/malcolmtredinnick/202679799/

• http://flickr.com/photos/andrewmoir/3012531/

• http://flickr.com/photos/apesara/2499666202/

• http://flickr.com/photos/mehrwert/18711050/

• http://flickr.com/photos/bohemianrabbit/2815366720/

• http://flickr.com/photos/ianturton/2155423758/

• http://flickr.com/photos/mbiddulph/2037845171/

• http://flickr.com/photos/cgc/130309167/

• http://www.flickr.com/photos/daveaustria/2654190796