Skip to main content

How To Use Map Reduce in Spring Mongo data


Suppose  I have order table and I want to find out best five selling product.

Create a method that returns best product of ValueObject Type .
ValueObject Type basically contain id and corresponding values to those ids as shown below:


public class ValueObject implements Comparable<ValueObject>{
private Object id;
private int value;
public Object getId() {
return id;
}
public void setId(Object id) {
this.id = id;
}

public int getValue() {
return value;
}

public void setValue(int value) {
this.value = value;
}

@Override
public String toString() {
return "ValueObject [id=" + id + ", value=" + value + "]";
}
@Override
public int compareTo(ValueObject o) {
if(o instanceof ValueObject){
return o.getValue()-this.getValue();
}
else{
throw new ClassCastException();
}
}

}


Now, you can call mapReduce method on MongoTemplate which return result as MapReduceResult

public Iterable<ValueObject> countBestSellingProduct() {
MapReduceResults<ValueObject> results = mongoTemplate.mapReduce("order", "map.js", ”reduce.js", ValueObject.class);
             
              return results;
       }

Syntax of mapReduce ()

mapReduce(“Collection Name”, “ mapfunction”, “reducefunction”, ValueObect.class);
* You can also pass your query to mapReduce then Sytax will be
mapReduce(query, “Collection Name”,  “mapfunction”, “reducefunction”, ValueObect.class);

map.js (This is just an example to map with id)

function ()
{
       var prt = this.$id;
       if(prt != null ){
             
       }
       emit(this.$id, 1);
}

reduce.js(Example of a reduce function)

function (key, values)
{
       var sum = 0;
       for (var i = 0; i < values.length; i++)
       {
              sum += values[i];
       }
       return sum;
}


You will get result which is Iterable ValueObject type
Now Convert it into List<ValueObject>
List<ValueObject> lst = new ArrayList<ValueObject>();

              for (ValueObject prt: result) {
                     lst.add(prt);
                     
              }
And finally, sort this list by using
Collections.sort(lst);
Now, you can top five or more best-selling products by taking out a sublist from the above list.

List<ValueObject> bestFiveSellingProduct=lst.subList(0,4)

Simple, isn' it??
Cheers !!!!!!!!!!!!!!!!!!!!!!!

Comments

  1. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. what county am i in

    ReplyDelete

Post a Comment

Thanks for your concern.

Popular posts from this blog

Why Dozer Framework (Bean Manipulation)

Why Dozer ? Let us think about a situation that you have a source bean which contains lot of fields and the source bean belongs to a different project or module. Now you want to expose the bean to  the outside world as a part of your web service REST service development. It is not advisable to do it. There may be the following reasons. The source bean is not serialized and a final class. The source system does not allow doing it because of security breach. The source bean is very heavy and contains lot of nested beans. The source bean has fields of different types which may not be required for other system. The source bean has lot of fields; some of them are not required. Scenario to use Dozer Suppose You want to make a REST call or web service call to get the minimal account details of a person. But the source system has a bean called “Acc0untBean” which contains many sensitive information like person’s internet banking passw0rd, PAN no or social sec...

Difference between Micro Service and Web Services

Micro web services and Web services are two different concepts of application development architecture, Which can be differentiated from it's development style and layered architecture.In This article I will explain the difference between Web Services and Micro Services Web Services ? Web services are services that are made available from a business's Web server for Web users or other Web-connected programs. it is a way to expose the functionality of an application to other application, without a user interface. It is a service which exposes an API over HTTP. Web Services allow applications developed in different technologies to communicate with each other through a common format like XML, Jason, etc.  Web services are not tied to any one operating system or programming language. For example, an application developed in Java can be used in C#, Android, Php etc., and vice versa.  Web Service is a connection technology, a way to connect services together into a ...

JAVA_OPTS Variable Details

Memory Available to the Java JVM Increasing the memory available to the Java JVM JAVA_OPTS="-Xmx1024m -Xms256m" export JAVA_OPT Options description: -Xmx sets the maximum amount of memory that can be allocated to the JVM heap; here it is being set to 1024 megabytes. -Xms sets the initial amount of memory allocated to the JVM heap; here it is being set to 256 megabytes. Run Java JVM in Server Mode The Java JVM can optimize a number of things for server environments. You can explicitly select the Java HotSpot Server VM with the -server option. JAVA_OPTS="-Xmx1024m -Xms256m -server" export JAVA_OPT What the option means: -server instructs the launcher to use the Java HotSpot Server VM. PermGen Memory If you start getting java.lang.OutOfMemoryError: PermGen space error messages. You may want to include a "-XX:MaxPermSize" option in your JAVA_OPTS. JAVA_OPTS="-Xmx1024m -Xms256m -server -XX:MaxPermSize=128m" export...