Friday, June 7, 2013

MongoDB HTTP Interface

I wondered does MongoDB has REST interface (This thought came to be while i was going through CouchDB Futon). Found something interesting with mongoDB also

1. Start your mongod process with command "mongod --rest"

2. Hit http://localhost:28017 in you browser
     ->It should list you some system admin data.

     ->It should have given list of databases as a JSON response. For example I had bellow

{ "databases" : [ 
    { "name" : "blog",
      "sizeOnDisk" : 218103808,
      "empty" : false }, 
    { "name" : "course",
      "sizeOnDisk" : 218103808,
      "empty" : false }, 
    { "name" : "m101",
      "sizeOnDisk" : 218103808,
      "empty" : false }, 
    { "name" : "mydb",
      "sizeOnDisk" : 218103808,
      "empty" : false }, 
    { "name" : "pcat",
      "sizeOnDisk" : 218103808,
      "empty" : false }, 
    { "name" : "school",
      "sizeOnDisk" : 218103808,
      "empty" : false }, 
    { "name" : "students",
      "sizeOnDisk" : 218103808,
      "empty" : false }, 
    { "name" : "test",
      "sizeOnDisk" : 218103808,
      "empty" : false }, 
    { "name" : "local",
      "sizeOnDisk" : 1,
      "empty" : true } ],
  "totalSize" : 1744830464 }

4. You could see list of commands supported over REST at the following location

5. To  lists all documents under "posts" collection in "blog" database

6. To see a specific document in posts collection with query {author="narayan"}

Following link gives some comprehensive note on this HTTP interface. Read and benefit!!!

http://docs.mongodb.org/ecosystem/tools/http-interfaces/

Saturday, June 1, 2013

MongoDB Map-Reduce Example with Code

Today I was learning mongoDB map-Reduce function. To understand the concept I built the following example.

Assume we have the collections called “things” which has following records



[
        {
                "_id" : ObjectId("51a9e753d7a333351958d6be"),
                "id_" : 1234,
                "title" : "Microsoft .Net",
                "tags" : [
                        "dot net",
                        ".net",
                        ". net"
                ]
        },
        {
                "_id" : ObjectId("51a9e7a7d7a333351958d6bf"),
                "id_" : 5678,
                "title" : "J2EE",
                "tags" : [
                        "Java",
                        "Servelet",
                        "JSP"
                ]
        },
        {
                "_id" : ObjectId("51a9ebefd7a333351958d6c0"),
                "id_" : 2,
                "title" : "J2EE",
                "tags" : [
                        "JSF",
                        "Portlet",
                        "REST"
                ]
        },
        {
                "_id" : ObjectId("51a9ec3fd7a333351958d6c1"),
                "id_" : 3,
                "title" : "Microsoft .Net",
                "tags" : [
                        "C#",
                        "VB.net",
                        "ASP.net"
                ]
        }
]


If you see in the above collection, there are 2 records with title “J2EE”. Each record has 3 tags. My objective is to know totally how many tags for each course title(J2EE). For this I am set to use map-Reduce of mongoDB

Map function goes like this:


var mapFun = function(){
var k=this.title;
var v=this.tags.length;
emit(k,v);
};


In the above map function for each record, I am finding the total number of “tags” and emitting (throws) the key value pair as (J2EE, [3,3])

MongoDB map-Reduce function catches the emitted key value pairs from emit function. And it the groups the similar key records and puts the value in a array. Hence the resulting record is of form (“J2EE”, [3,3])

Reduce function goes like this:


var redFun = function(key,values){
            return Array.sum(values);
};


In the above reduce function, it totals the values present in the values array. In out example it would have received input like redFun(“J2EE”,[3,3]). So the return value would be 6 which is total number of tags for J2EE title.

Now our map-Reduce function is written as follows


db.things.mapReduce(
mapFun,
redFun,
{out:"tagcount"}
)


The above mapReduce function, we are outputting the value in “tagcount” collection. When “tagcount” collection is seen, it looks like bellow


[
        {
                "_id" : "J2EE",
                "value" : 6
        },
        {
                "_id" : "Microsoft .Net",
                "value" : 6
        }
]


So now we got the needed collection.


Simple REST Example without Security

I wanted to do one sample project using CouchDB. So I have been looking in to internet for very simple REST example with Java. Could hardly find any. Hope bellow one will help many.

This article is not going to tell what is CouchDB. Assume it as a REST provider. So when I hit a url in browser , it is going to give me a text response.

Following is the REST client which shows how the same url could be fired through JAVA program to obtain the result

------------------------------------------
package course;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

public class RestClient {

    public final static String endpoint = "http://127.0.0.1:5984/courses/_all_docs";

    public static void main(String[] args) {
        HttpURLConnection request = null;
        BufferedReader reader = null;
        StringBuilder response = null;
        try{
            URL endpointUrl = new URL(endpoint);
            request = (HttpURLConnection)endpointUrl.openConnection();
            request.setRequestMethod("GET");
            request.connect();
            reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
            response = new StringBuilder();
            String line;
            while((line=reader.readLine())!=null){
                response.append(line);
            }
            reader.close();
            request.disconnect();
            System.out.println(response);

         } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
-----------------------------------------------------------------------------