Wednesday 12 June 2013

Installing CouchDB - Get Training with Real time POC Project

Installing CouchDB

 CouchDB allows you to write a client side application that talks directly to the Couch without the need for a server side middle layer, significantly reducing development time. With CouchDB, you can easily handle demand by adding more replication nodes with ease. CouchDB allows you to replicate the database to your client and with filters you could even replicate that specific user’s data.
Having the database stored locally means your client side application can run with almost no latency. CouchDB will handle the replication to the cloud for you. Your users could access their invoices on their mobile phone and make changes with no noticeable latency, all whilst being offline. When a connection is present and usable, CouchDB will automatically replicate those changes to your cloud CouchDB.
CouchDB is a database designed to run on the internet of today for today’s desktop-like applications and the connected devices through which we access the internet.

Step 1 

The easiest way to get CouchDB up and running on your system is to head to CouchOne and download a CouchDB distribution for your OS — OSX in my case. Download the zip, extract it and drop CouchDBX in my applications folder (instructions for other OS’s on CouchOne).
Finally, open CouchDBX.

Step 2 – Welcome to Futon

After CouchDB has started, you should see the Futon control panel in the CouchDBX application. In case you can’t, you can access Futon via your browser. Looking at the log, CouchDBX tells us CouchDB was started at http://127.0.0.1:5984/ (may be different on your system). Open a browser and go tohttp://127.0.0.1:5984/_utils/ and you should see Futon.

CouchDB jQuery Plugin

Futon is actually using a jQuery plugin to interact with CouchDB. You can view that plugin athttp://127.0.0.1:5984/_utils/script/jquery.couch.js (bear in mind your port may be different). This gives you a great example of interacting with CouchDB.

Step 3 – Users in CouchDB

CouchDB, by default, is completely open, giving every user admin rights to the instance and all its databases. This is great for development but obviously bad for production. Let’s go ahead and setup an admin. In the bottom right, you will see “Welcome to Admin Party! Everyone is admin! Fix this”.
Go ahead and click fix this and give yourself a username and password. This creates an admin account and gives anonymous users access to read and write operations on all the databases, but no configuration privileges.
Users in CouchDB can be a little confusing to grasp initially, specially if you’re used to creating a single user for your entire application and then managing users yourself within a users table (not the MySQL users table). In CouchDB, it would be unwise to create a single super user and have that user do all the read/write, because if your app is client-side then this super user’s credentials will be in plain sight in your JavaScript source code.
CouchDB has user creation and authentication baked in. You can create users with the jQuery plugin using$.couch.signup(). These essentially become the users of your system. Users are just JSON documents like everything else so you can store any additional attributes you wish like email for example. You can then use groups within CouchDB to control what documents each user has write access to. For example, you can create a database for that user to which they can write to and then add them to a group with read access to the other databases as required.

Step 4 – Creating a Product Document

Now let’s create our first document using Futon through the following steps:
  1. Open the mycouchshop database.
  2. Click “New Document”.
  3. Click “Add Field” to begin adding data to the JSON document. Notice how an ID is pre-filled out for you, I would highly advise not changing this. Add key “name” with the value of “Nettuts CouchDB Tutorial One”.
  4. Make sure you click the tick next to each attribute to save it.
  5. Click “Save Document”.

Step 5 – Updating a Document

CouchDB is an append only database — new updates are appended to the database and do not overwrite the old version. Each new update to a JSON document with a pre-existing ID will add a new revision. This is what the automatically inserted revision key signifies. Follow the steps below to see this in action:
  • Viewing the contents of the mycouchshop database, click the only record visible.
  • Add another attribute with the key “type” and the value “product”.
  • Hit “Save Document”.

Step 6 – Creating a Document Using cURL

I’ve already mentioned that CouchDB uses a RESTful interface and the eagle eyed reader would have noticed Futon using this via the console in Firebug. In case you didn’t, let’s prove this by inserting a document using cURL via the Terminal.
First, let’s create a JSON document with the below contents and save it to the desktop calling the fileperson.json.
  1. {  
  2.     "forename""Gavin",  
  3.     "surname":  "Cooper",  
  4.     "type":     "person"  
  5. }  
Next, open the terminal and execute cd ~/Desktop/ putting you in the correct directory and then perform the insert with curl -X POST http://127.0.0.1:5984/mycouchshop/ -d @person.json -H "Content-Type: application/json". CouchDB should have returned a JSON document similar to the one below.
  1. {"ok":true,"id":"c6e2f3d7f8d0c91ce7938e9c0800131c","rev":"1-abadd48a09c270047658dbc38dc8a892"}  
This is the ID and revision number of the inserted document. CouchDB follows the RESTful convention and thus:
  • POST – creates a new record
  • GET – reads records
  • PUT – updates a record
  • DELETE – deletes a record

Step 7 – Viewing All Documents

We can further verify our insert by viewing all the documents in our mycouchshop database by executingcurl -X GET http://127.0.0.1:5984/mycouchshop/_all_docs.

Step 8 – Creating a Simple Map Function

Viewing all documents is fairly useless in practical terms. What would be more ideal is to view all product documents. Follow the steps below to achieve this:
  • Within Futon, click on the view drop down and select “Temporary View”.
  • This is the map reduce editor within Futon. Copy the code below into the map function.
    1. function (doc) {  
    2.     if (doc.type === "product" && doc.name) {  
    3.         emit(doc.name, doc);  
    4.     }  
    5. }  
  • Click run and you should see the single product we added previously.
  • Go ahead and make this view permanent by saving it.
After creating this simple map function, we can now request this view and see its contents over HTTP using the following command curl -X GET http://127.0.0.1:5984/mycouchshop/_design/products/_view/products.
A small thing to notice is how we get the document’s ID and revision by default.

Step 9 – Performing a Reduce

To perform a useful reduce, let’s add another product to our database and add a price attribute with the value of 1.75 to our first product.
  1. {  
  2.     "name":     "My Product",  
  3.     "price":    2.99,  
  4.     "type":     "product"  
  5. }  
For our new view, we will include a reduce as well as a map. First, we need to map defined as below.
  1. function (doc) {  
  2.     if (doc.type === "product" && doc.price) {  
  3.         emit(doc.id, doc.price);  
  4.     }  
  5. }  
The above map function simply checks to see if the inputted document is a product and that it has a price. If these conditions have been met, the products price is emitted. The reduce function is below.
  1. function (keys, prices) {  
  2.     return sum(prices);  
  3. }  
The above function takes the prices and returns the sum using one of CouchDB’s built in reduce functions. Make sure you check the reduce option in the top right of the results table as you may otherwise be unable to see the results of the reduce. You may need to do a hard-refresh on the page to view the reduce option.

Get Hands-on Training @ BigDataTraining.IN
Contact us:

#67,2nd Floor, 1st Main Road, Gandhi Nagar, Adyar, Chennai- 600020




No comments:

Post a Comment