Searching...
Tuesday, 18 September 2012

Java Mongodb Mapreduce Example

23:14
Map Reduce

MapReduce is a software framework introduced by Google in 2004 to support distributed computing on large data sets on clusters of computers, more about Map reduce.

Here is one simple example of data aggregation using via a java program.

Goals

1. Insert Customer records in to Mongodb

2. Categorize Customer as Major or Minor based on age and count the number of customers

Steps 

Create a Maven project and add below dependency in pom.xml
 
<dependency>
                     <groupId>org.mongodb</groupId>
                     <artifactId>mongo-java-driver</artifactId>
                     <version>2.5.2</version>
              </dependency>

Create a Java class to insert Customer details and test MapRedeuce.
 
package com.vinod.examples;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MapReduceCommand;
import com.mongodb.MapReduceOutput;
import com.mongodb.Mongo;

public class MapReduceExample {

    public static void main(String[] args) {
    Mongo mongo;

    try {
        mongo = new Mongo("localhost", 27017);
        DB db = mongo.getDB("library");

        DBCollection customerCollection = db.getCollection("CustomerDetails");

        BasicDBObject customer = new BasicDBObject();
        customer.put("name", "RAM");
        customer.put("age", 20);
        customerCollection.insert(customer);

        customer = new BasicDBObject();
        customer.put("name", "CHANDRAN");
        customer.put("age", 26);
        customerCollection.insert(customer);

        customer = new BasicDBObject();
        customer.put("name", "VINOD");
        customer.put("age", 24);
        customerCollection.insert(customer);

        customer = new BasicDBObject();
        customer.put("name", "SRIKUMAR");
        customer.put("age", 30);
        customerCollection.insert(customer);

        customer = new BasicDBObject();
        customer.put("name", "SURAJ");
        customer.put("age", 12);
        customerCollection.insert(customer);

        customer.put("name", "REEJU");
        customer.put("age", 18);
        customerCollection.insert(customer);

        customer = new BasicDBObject();
        customer.put("name", "VINITHA");
        customer.put("age", 32);
        customerCollection.insert(customer);

        customer = new BasicDBObject();
        customer.put("name", "NIRMAL");
        customer.put("age", 23);
        customerCollection.insert(customer);

        customer = new BasicDBObject();
        customer.put("name", "RAGHAV");
        customer.put("age", 10);
        customerCollection.insert(customer);

        customer = new BasicDBObject();
        customer.put("name", "SAYOOJ");
        customer.put("age", 33);
        customerCollection.insert(customer);

        System.out.println("Total records in Customer collections");
        // Retrieving collection details
        DBCursor cursorDoc = customerCollection.find();
        while (cursorDoc.hasNext()) {
        System.out.println("Customer details " + cursorDoc.next());
        }

        String map = "function() { " + "var category; " + "if ( this.age >= 21 ) " + "category = 'Major'; "
            + "else " + "category = 'Minor'; " + "emit(category, {name: this.name});}";

        String reduce = "function(key, values) { " + "var sum = 0; " + "values.forEach(function(doc) { "
            + "sum += 1; " + "}); " + "return {mycustomer: sum};} ";

        MapReduceCommand cmd = new MapReduceCommand(customerCollection, map, reduce, null,
            MapReduceCommand.OutputType.INLINE, null);

        MapReduceOutput out = customerCollection.mapReduce(cmd);
        System.out.println("Mapreduce results");

        for (DBObject o : out.results()) {
        System.out.println(o.toString());
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }

}
 
 
Start mongodb and run the program (See this example How to start Mongodb)

Output
Customer details { "_id" : { "$oid" : "56a352ad7661b92901d2901e"} , "name" : "RAM" , "age" : 20}
Customer details { "_id" : { "$oid" : "56a352ad7661b92901d2901f"} , "name" : "CHANDRAN" , "age" : 26}
Customer details { "_id" : { "$oid" : "56a352ad7661b92901d29020"} , "name" : "VINOD" , "age" : 24}
Customer details { "_id" : { "$oid" : "56a352ad7661b92901d29021"} , "name" : "SRIKUMAR" , "age" : 30}
Customer details { "_id" : { "$oid" : "56a352ad7661b92901d29022"} , "name" : "SURAJ" , "age" : 12}
Customer details { "_id" : { "$oid" : "56a352ad7661b92901d29023"} , "name" : "VINITHA" , "age" : 32}
Customer details { "_id" : { "$oid" : "56a352ad7661b92901d29024"} , "name" : "NIRMAL" , "age" : 23}
Customer details { "_id" : { "$oid" : "56a352ad7661b92901d29025"} , "name" : "RAGHAV" , "age" : 10}
Customer details { "_id" : { "$oid" : "56a352ad7661b92901d29026"} , "name" : "SAYOOJ" , "age" : 33}
Mapreduce results
{ "_id" : "Major" , "value" : { "mycustomer" : 12.0}}
{ "_id" : "Minor" , "value" : { "mycustomer" : 6.0}}
 
Done!! download example from
 
 

 

0 comments: