Friday, 20 December 2013

CouchDB on Linux Mint - Cheat sheet

Here's my cheat sheet for working with CouchDB on a Linux Mint server.

Installation

Installation is trivial
sudo apt-get install couchdb -y

Test to see that it works.
curl http://127.0.0.1:5984/

should return something like

{"couchdb":"Welcome","version":"1.2.0"}

Create a database named 'johantest'
curl -X PUT http://127.0.0.1:5984/johantest

Delete the same database
curl -X DELETE http://127.0.0.1:5984/johantest


Adding documents

Assume you didn't delete the database, add a document like this
curl -X POST http://127.0.0.1:5984/johantest \
-H 'Content-Type: application/json' \
-d '{"SomeProperty":"Test1","AnotherProperty":"Test2","Age":32,"PropList":["Prop1","Prop2","Prop3"]}'

To retrieve all documents in a database

curl -X GET http://127.0.0.1:5984/johantest/_all_docs

Gives
{"total_rows":1,"offset":0,"rows":[
{"id":"4571d772c7d61fa34a7579ce6f04d47b","key":"4571d772c7d61fa34a7579ce6f04d47b","value":{"rev":"1-a268e2541be30dae295fb88015bd1a5c"}}
]}

That will only give references so you can GET each document detail. To fetch the contents of an individual document, fetch using the key from the provious GET.
curl -X GET http://127.0.0.1:5984/johantest/4571d772c7d61fa34a7579ce6f04d47b

Gives
{"_id":"4571d772c7d61fa34a7579ce6f04d47b","_rev":"1-a268e2541be30dae295fb88015bd1a5c","SomeProperty":"Test1","AnotherProperty":"Test2","Age":32,"PropList":["Prop1","Prop2","Prop3"]}

 If you wish to fetch all documents in one large JSON blob
curl -X GET http://127.0.0.1:5984/johantest/_all_docs?include_docs=true 

Gives
{"total_rows":1,"offset":0,"rows":[
{"id":"4571d772c7d61fa34a7579ce6f04d47b","key":"4571d772c7d61fa34a7579ce6f04d47b","value":{"rev":"1-a268e2541be30dae295fb88015bd1a5c"},"doc":{"_id":"4571d772c7d61fa34a7579ce6f04d47b","_rev":"1-a268e2541be30dae295fb88015bd1a5c","SomeProperty":"Test1","AnotherProperty":"Test2","Age":32,"PropList":["Prop1","Prop2","Prop3"]}}
]}

Settings and logs

Settings are stored in /etc/couchdb/default.ini
To be able to connect to the databse via HTTP from other machines on network, change
bind_address = 0.0.0.0

The actual database is stored by default at
/var/lib/couchdb/1.2.0 and is named in this case johantest.couch

If you wish to move it to another partition or similar, change the settings
database_dir = /var/lib/couchdb/1.2.0
view_index_dir = /var/lib/couchdb/1.2.0

To get a feel of the amount of disk used by Couch I notice that my Weather Station application has stored 8800 JSON documents in a format like this

{"id":"6ea52d20ab226121a325d0e3c5ffe4b5","key":"6ea52d20ab226121a325d0e3c5ffe4b5","value":{"rev":"1-087372f223546fb03c75605decb4a442"},"doc":{"_id":"6ea52d20ab226121a325d0e3c5ffe4b5","_rev":"1-087372f223546fb03c75605decb4a442","temperature":23.75858110517378563,"rawDate":1385961273368,"key":"holmon"}}

That will occupy 28.9 MB of disk with the default settings of compression etcetera.

To see what requests has been made against the database, the log is placed at
 /var/log/couchdb/couch.log
which can be changed in the settings file as well. Output for the tests above

[Sat, 28 Dec 2013 09:08:41 GMT] [info] [<0.2082.8>] 127.0.0.1 - - PUT /johantest 201
[Sat, 28 Dec 2013 09:34:03 GMT] [info] [<0.28823.7>] 127.0.0.1 - - DELETE /johantest 200
[Sat, 28 Dec 2013 09:34:07 GMT] [info] [<0.25248.7>] 127.0.0.1 - - PUT /johantest 201
[Sat, 28 Dec 2013 09:34:13 GMT] [info] [<0.3856.8>] 127.0.0.1 - - POST /johantest 201
[Sat, 28 Dec 2013 09:34:22 GMT] [info] [<0.2437.8>] 127.0.0.1 - - GET /johantest/_all_docs?include_docs=true 200
[Sat, 28 Dec 2013 09:35:29 GMT] [info] [<0.2449.8>] 127.0.0.1 - - GET /johantest/_all_docs 200
[Sat, 28 Dec 2013 09:35:41 GMT] [info] [<0.2339.8>] 127.0.0.1 - - GET /johantest/4571d772c7d61fa34a7579ce6f04d47b 200


No comments:

Post a Comment