Apache CouchDB @ OGLF09

These are the slides from my Ontario [GNU] Linux Fest 2009 talk on Apache CouchDB. You can download the PDF or see the Slideshare.

Apache CouchDB

Who am I?

Monkey in your Soul

What is CouchDB?

Document-Oriented Database

No Rows or Columns

But

Collection of JSON Doucments

This is know as Schema-Free

Example of a JSON document.
This is an example of a JSON document.

Same example as above but with XML.
This is the same example as before but formated in XML.

Data types

JSON Data types
JSON has the same basic data types as JavaScript.

HTTP RESTFul API

Quote from Jacob Kaplan-Moss

RFC 2616
RFC 2616

REST

Low level XML-RPC and SOAP

Basic HTTP Methods.
RFC 2616 Section 9

Basic HTTP Response Codes.
RFC 2616 Section 10

Views

Aggregation and Reporting on Documents

But how do you aggreate non-structured data?

Instead of using SQL

JavaScript

Distributed

Using Replication

You have your database.

You

You want to share.

You and your Friend

Full control
Now you Friend has full control of the data on his instance of CouchDB.

Bi-Directional Changes

You and your Friend
You Fiends can replicate his changes back to you.

Mutiple Nodes
This can work on large scale distribution as while.

Built in Conflict Detection

Using the API

Database

Create a Database
We are calling the PUT method to our new database name.

Response
And if everything is okay we will receive a 201 Created response and some JSON of {"ok":true}.

Duplicate Error
If we try an create a database that already exists on the system we get a 412 Precondition Failed.

Delete a Database
We are calling the DELETE method to out database to remove the database from the CouchDB node.

Response
And because the database exists we get back a 200 OK response.

Fetching all Rows
Using the GET method...

Response
We receive a JSON of all the documents in the Database and a 200 OK response.

JSON Response
Here is the JSON in pretty print.

Document

Creating a Document
To create a document in the database we need to use the POST method and include some data...

Response
We then get a 201 Created response and a JSON document that contains the id and rev (reversion) of the newly created document.

Get a Document
We use the GET method and include the document id...

Response
and get back a response of 200 OK and a JSON document. Note the HTTP header Etag, this is here so if you wanted to setup a proxy in front of a cluster of CouchDB nodes.

JSON Document
This is the same JSON document as above in pretty print.

Update a Document
To update a document we use the method PUT and include the current document with our appended changes.

Our append changes.

Request

Response
Notice the rev has gone up by one.

Attaching a File to a Document

Response

Looking up the attachment.

This is the attachment.

Copy a Document

Response

Delete a Document

Response

Libraries

Python
couchdb-python, CouchKit, and CouchQuery.

Ruby
CouchRest.

Erlang
CouchBeam.

Java
CouchDB4J.

I have created a Library in an afternoon.

Source Code

Just sky of 300 lines.

Use your builtin modules.
If you are going to build your own library all you really need is your language's HTTP module (httplib, Net::HTTP, java.net.HttpUrlConnection, and net/http) and a third party JSON dumper.

Mozilla Raindrop
Mozilla Raindrop is an exciting new messaging platform developed by the Thunderbird team. Not only does it use CouchDB as a database but also uses the built in HTTP server for the client. Using a technique called CouchApp, you can build HTML/JavaScript applications on top of your CouchDB database.

Ubunut One
Ubuntu One is using CouchDB to store Contacts, Calendars, and bookmarks on your computer. With the release of Karmic Kola CouchDB will be installed by default on your computer.

Other Document-Oriented Database Systems

MongoDB

Amazon SimpleDB

Photo Credits