Thursday, November 25, 2010

CouchDB Hello World

Since the next COJUG (http://www.meetup.com/techlifecolumbus/calendar/15532445/) is going to be on CouchDB and Grails, I figured I should know what CouchDB is. Google hooked me up with http://couchdb.apache.org/ where I read the Intro and the Overview. I was introduced to the concepts of NoSQL and document-oriented database. I'm not the best data person out there, so when I saw you use JSON over HTTP instead of SQL, I was eager to get my hands dirty. I found the wiki section and 15 mins up and running thing - http://wiki.apache.org/couchdb/CouchIn15Minutes. I decided to do my own version of it just to say i did not quite blindly follow it. The idea was to develop a database that would store all the donkeys I own! So here it is step-by-step, assuming you are on a Windows as I am

1. Install CouchDBhttp://people.apache.org/~mhammond/dist/1.0.1/   I chose to install it as a Windows Service during install. It also asks you if you want to start the service automatically, which I did

2. Make sure CouchDB is running - go to http://localhost:5984/ . 
You should see {"couchdb":"Welcome","version":"1.0.1"}

3. Create the donkey database - got to http://localhost:5984/_utils/ and click on Create Database.
   Type donkey_db in the prompt and click create.

4. Install http4e - it is an HTTP client eclipse plugin - http://www.ywebb.com/eclipse-restful-http-client-plugin-install/

5. Open the HTTP4e view in Eclipse - Window -> Show View -> Other -> HTTP -> HTTP4E Client

6. Fire a HTTP PUT request to create the first donkey
    Type http://localhost:5984/donkey_db/first_donkey in the url field in HTTP4E, select PUT from the drop-    down, enter "Content-Type: application/json" (no quotes) in the Header text area, enter the following JSON in the "Body" text area:

{
    "donkeys" : {
        "name": "Boyko",
        "date_of_birth": "01/17/1987",
        "temper": "stubborn",
        "kick_strength": "powerful"
    }
}

Hit the green arrow button to fire the request. Now the HTTP Response section in HTTP4E should say something like :

HTTP/1.1 201 Created
Server: CouchDB/1.0.1 (Erlang OTP/R14B)
Location: http://localhost:5984/donkey_db/first_donkey
Etag: "1-23e171bbbb09fd8f266bcac74319c2f1"
Date: Fri, 26 Nov 2010 07:04:17 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 75
Cache-Control: must-revalidate

{"ok":true,"id":"first_donkey","rev":"1-23e171bbbb09fd8f266bcac74319c2f1"}

So, BOOM, there yah go, you successfully created you first CouchDB donkey.

7. Fire a HTTP GET through HTTP4E to get the document. Just change the drop-down from PUT to GET, hit the green arrow again and you should see:

HTTP/1.1 200 OK
Server: CouchDB/1.0.1 (Erlang OTP/R14B)
Etag: "1-23e171bbbb09fd8f266bcac74319c2f1"
Date: Fri, 26 Nov 2010 07:08:17 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 170
Cache-Control: must-revalidate

{"_id":"first_donkey","_rev":"1-23e171bbbb09fd8f266bcac74319c2f1","donkeys":{"name":"Boyko","date_of_birth":"01/17/1987","temper":"stubborn","kick_strength":"powerful"}}

8. Go to FUTON (CouchDB's UI) and check out the first donkey there

Create as many donkeys as you like.

The 15 mins section talked about also creating a view for the documents, so 

9. I created a view by firing an HTTP PUT request to http://localhost:5984/donkey_db/_design/render with the following JSON in the "BODY" section {"shows" : {"catalog" : "function(doc, req) {return {body: doc.donkeys}}"}} and got a good response:

HTTP/1.1 201 Created
Server: CouchDB/1.0.1 (Erlang OTP/R14B)
Location: http://localhost:5984/donkey_db_1/_design/render
Etag: "1-26580f227ea0cbd53ee2458144bea59c"
Date: Fri, 26 Nov 2010 07:13:41 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 77
Cache-Control: must-revalidate

{"ok":true,"id":"_design/render","rev":"1-26580f227ea0cbd53ee2458144bea59c"}

{"error":"unknown_error","reason":"badarg"}
The following gets logged under $CouchDB_install_dir/var/log/couchdb/couch.log - 

[ [error] [<0.7469.0>] Badarg error in HTTP request

[Fri, 26 Nov 2010 07:17:33 GMT] [info] [<0.7469.0>] Stacktrace: [{erlang,iolist_size,
                     [{[{<<"name">>,<<"Boyko">>},
                        {<<"date_of_birth">>,<<"01/17/1987">>},
                        {<<"temper">>,<<"stubborn">>},
                        {<<"kick_strength">>,<<"powerful">>}]}]},
             {mochiweb_request,respond,2},
             {couch_httpd_external,send_external_response,2},
             {couch_httpd_db,do_db_req,2},
             {couch_httpd,handle_request_int,5},
             {mochiweb_http,headers,5},
             {proc_lib,init_p_do_apply,3}] 

So I will have to dig a little more around that and maybe update here if I figure it out...
[Edit] Turns out you can set the logging level to debug - go to <CouchDB_install_dir>/etc/couchdb/default.ini and in the [log] section change level = info to level = debug. It started logging more after I restarted the Windows Apache Couch DB service, but still can't figure out the 'badarg' error [/end EDIT]

Pretty cool with the JSON over HTTP instead of SQL, yet not being able to create the Hello Word-ish view makes me wonder...

No comments:

Post a Comment