@@ -0,0 +1,2814 @@
+/* %0A* @Author: Zeyuan Shang%0A* @Date:   2016-01-22 23:16:25%0A* @Last Modified by:   Zeyuan Shang%0A* @Last Modified time: 2016-01-22 23:16:33%0A*/%0Aimport java.util.*;%0Aimport org.json.simple.JSONObject;%0A%0Aclass MapReduce%0A%7B%0A   private LinkedHashMap intermediate;%0A   private JSONObject finalResult = new JSONObject();%0A   private int resultCount;%0A   MapReduce()%0A   %7B%0A      resultCount=0;%0A      finalResult = new JSONObject();%0A      intermediate=new LinkedHashMap();%0A   %7D%0A   %0A   JSONObject execute(JSONObject inputdata)%0A   %7B%0A      for(int i=0;i%3Cinputdata.size();i++)%0A      %7B%0A         JSONObject record=(JSONObject)inputdata.get(i);%0A         mapper(record);%0A      %7D%0A      %0A      Iterator it = intermediate.entrySet().iterator();%0A      while (it.hasNext()) %7B%0A         Map.Entry pair = (Map.Entry)it.next();%0A         reducer((String)pair.getKey(), (ArrayList)pair.getValue());%0A         it.remove();%0A      %7D%0A      return finalResult;%0A      %0A      %0A   %7D%0A   private void emit(LinkedHashMap obj)%0A   %7B%0A      finalResult.put(resultCount++,obj);%0A   %7D%0A   private void reducer(String key, ArrayList value)%0A   %7B%0A      //Complete the line below by filling up the question marks%0A      int total=0;%0A      for (Object c : value) %7B%0A          total += (int)c;%0A      %7D%0A      LinkedHashMap obj=new LinkedHashMap();%0A      obj.put(%22key%22,key);%0A      obj.put(%22value%22,total);%0A      emit(obj);%0A      %0A   %7D%0A   private void mapper(JSONObject record)%0A   %7B%0A      String value=(String)record.get(%22value%22);%0A      int key=(int)record.get(%22key%22);%0A      %0A      for (String word: value.split(%22 %22))%0A      %7B%0A         //Complete the line below by filling up the question marks%0A         emitIntermediate(word, 1);%0A      %7D%0A      %0A      %0A   %7D%0A   private void emitIntermediate(String key, int value)%0A   %7B%0A      if(!intermediate.containsKey(key))%0A         intermediate.put(key,new ArrayList());%0A%0A      ArrayList temp=(ArrayList)intermediate.get(key);%0A      temp.add(value);%0A      intermediate.put(key,temp);%0A   %7D%0A%7D%0Apublic class Main%0A%7B%0A   %0A   public static void main(String %5B%5Dargh)%0A   %7B%0A      JSONObject inputdata= new JSONObject();%0A      Scanner sc=new Scanner(System.in);%0A      %0A      int linecount=0;%0A      while(sc.hasNext())%0A      %7B%0A         String line=sc.nextLine();%0A            if(line.length()==0)continue;%0A         Map obj=new JSONObject();%0A         obj.put(%22key%22,linecount);%0A         obj.put(%22value%22,line);%0A         %0A         inputdata.put(linecount++,obj);%0A      %7D%0A      MapReduce mapred= new MapReduce();%0A      JSONObject result=mapred.execute(inputdata); %0A   %0A      for(int i=0;i%3Cresult.size();i++)%0A      %7B%0A         LinkedHashMap record=(LinkedHashMap)result.get(i);%0A         String key=(String)record.get(%22key%22);%0A         int value=(int)record.get(%22value%22);%0A         System.out.println (%22%7B%5C%22key%5C%22:%5C%22%22+key+%22%5C%22,%5C%22value%5C%22:%22+value+%22%7D%22);%0A%0A      %7D%0A   %0A   %7D%0A   %0A%7D%0A

add map-reduce-tutorials-1-word-counts