Map Reduce Operation With Suitable Example Using MongoDB. | I'M FROSTY
Make some Money!

What's Hot

recentposts

Map Reduce Operation With Suitable Example Using MongoDB.

======== MAP Reduce JAVA Program For MongoDB ========


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 PretechMapReduceExample {

      public static void main(String[] args) {

          Mongo mongo;

              try {

                     mongo = new Mongo("localhost", 27017);

                     DB db = mongo.getDB("library");

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

                     BasicDBObject customer = new BasicDBObject();

                     customer.put("name", "VARSHA");

                     customer.put("age", 24);

                     pretechCustomer.insert(customer);

                     customer = new BasicDBObject();

                     customer.put("name", "VIJAY");

                     customer.put("age", 28);

                     pretechCustomer.insert(customer);

                     customer = new BasicDBObject();

                     customer.put("name", "VAIBHAV");

                     customer.put("age", 24);

                     pretechCustomer.insert(customer);

                     customer = new BasicDBObject();

                     customer.put("name", "VISHAKHA");

                     customer.put("age", 20);

                     pretechCustomer.insert(customer);

                     customer = new BasicDBObject();

                     customer.put("name", "ISHA");

                     customer.put("age", 12);

                     pretechCustomer.insert(customer);

                     customer.put("name", "REEJU");

                     customer.put("age", 18);

                     pretechCustomer.insert(customer);

                     customer = new BasicDBObject();

                     customer.put("name", "VINITHA");

                     customer.put("age", 32);

                     pretechCustomer.insert(customer);

                     customer = new BasicDBObject();

                     customer.put("name", "NIRMAL");

                     customer.put("age", 23);

                     pretechCustomer.insert(customer);

                     customer = new BasicDBObject();

                     customer.put("name", "RAGHAV");

                     customer.put("age", 10);

                     pretechCustomer.insert(customer);

                     customer = new BasicDBObject();

                     customer.put("name", "SAYOOJ");

                     customer.put("age", 33);

                     pretechCustomer.insert(customer);

                     System.out.println("Total records in Customer collections");

                     // Retrieving collection details

                     DBCursor cursorDoc = pretechCustomer.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 {pretechCustomer: sum};} ";

                     MapReduceCommand cmd = new MapReduceCommand(pretechCustomer, map,

                                  reduce, null, MapReduceCommand.OutputType.INLINE, null);

                     MapReduceOutput out = pretechCustomer.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();

              }

       }

}

============ OUTPUT ============

Total records in Customer collections
Customer details { "_id" : { "$oid" : "540a8a3a55c4e27d1396fd4c"} , "name" : "VARSHA" , "age" : 24} Customer details { "_id" : { "$oid" : "540a8a3a55c4e27d1396fd4d"} , "name" : "VIJAY" , "age" : 28} Customer details { "_id" : { "$oid" : "540a8a3a55c4e27d1396fd4e"} , "name" : "VAIBHAV" , "age" : 24} Customer details { "_id" : { "$oid" : "540a8a3a55c4e27d1396fd4f"} , "name" : "VISHAKHA" , "age" : 20} Customer details { "_id" : { "$oid" : "540a8a3a55c4e27d1396fd50"} , "name" : "ISHA" , "age" : 12} Customer details { "_id" : { "$oid" : "540a8a3a55c4e27d1396fd51"} , "name" : "VINITHA" , "age" : 32} Customer details { "_id" : { "$oid" : "540a8a3a55c4e27d1396fd52"} , "name" : "NIRMAL" , "age" : 23} Customer details { "_id" : { "$oid" : "540a8a3a55c4e27d1396fd53"} , "name" : "RAGHAV" , "age" : 10} Customer details { "_id" : { "$oid" : "540a8a3a55c4e27d1396fd54"} , "name" : "SAYOOJ" , "age" : 33}
Mapreduce results
{ "_id" : "Major" , "value" : { "pretechCustomer" : 12.0}}
{ "_id" : "Minor" , "value" : { "pretechCustomer" : 6.0}}
Map Reduce Operation With Suitable Example Using MongoDB. Reviewed by Hardik Pandya on 3:27:00 PM Rating: 5

No comments:

All Rights Reserved by I'M FROSTY © 2014 - 2016
*The Content Is Copyrighted To & May Not Be Copied / Republished.

Contact Form

Name

Email *

Message *

Powered by Blogger.