Another beta version of the book, finally with the chapter on CouchDB. I was going through the Redis chapter, but the third day uses other databases, in particular CouchDB. So I’ll get back to Redis after I’m done with CouchDB.
Today is just a short introduction: CouchDB is (yet another) key-value store; it has a ReST API, stores JSON data, and, like Riak, only supports full updates. Unlike Riak, however, it does not support concurrent updates; instead it requires the client to only update from the latest version of the data.
I thought at first that the data was versioned, like in HBase, but
this is not the case: the version id (_rev
) is there to ensure that
updates occur sequentially, not concurrently. CouchDB can keep
previous versions of documents, but the retention is unreliable as
explained here.
Besides the HTTP based ReST API, CouchDB also provides a web interface; among other tools, there is a complete test suite, which is always nice to check the installation.
Exercises
CouchDB HTTP Document API documentation
The documentation is here; there is also a reference
HTTP commands
Besides the basic CRUD POST
GET
PUT
and DELETE
, there is also
HEAD
(for basic information on a document):
1 2 3 4 5 6 7 8 |
|
When using cURL
, the command HEAD
must be used with the flag -I
,
otherwise cURL
will wait (endlessly) for data after the headers.
Finally, there is a COPY
command, which as expected copies a
document (without having to retrieve it first):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
PUT
a new document with a specific _id
It is just a matter of specifying an id when creating the document:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Document with a text attachment
To create an attachment, it is necessary to know the version of the
document, as it is considered an update. The URL for the attachment is
just the URL for its document, with any suffix (the suffix naming the
attachment). The _rev
is specified by passing a rev
parameter.
Using the document with _id
‘beatles’ created above, the attachment
is uploaded with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
The document now has a new _rev
.
To retrieve the attachment, just use its URL:
1 2 |
|
(the line breaks have been lost…)
Onward to Day 2!