daily pastebin goal
68%
SHARE
TWEET

Untitled

a guest Jan 18th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Map.Entry;
  9. import java.util.Set;
  10. import java.util.TreeMap;
  11.  
  12. import org.apache.hadoop.io.IntWritable;
  13. import org.apache.hadoop.io.Text;
  14. import org.apache.hadoop.mapreduce.Reducer;
  15.  
  16. public class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
  17.     TreeMap<Text,IntWritable> result = new TreeMap<Text, IntWritable>();
  18.    
  19.     public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
  20.         int sum = 0;
  21.         for (IntWritable val : values) {
  22.             sum += val.get();
  23.         }
  24.         result.put(new Text(key), new IntWritable(sum));
  25.     }
  26.    
  27.     @Override
  28.     public void cleanup(Context context) throws IOException, InterruptedException {
  29.         Set<Entry<Text, IntWritable>> set = result.entrySet();
  30.         List<Entry<Text, IntWritable>> list = new ArrayList<Entry<Text,IntWritable>>(set);
  31.         Collections.sort( list, new Comparator<Map.Entry<Text, IntWritable>>()
  32.         {
  33.             public int compare( Map.Entry<Text, IntWritable> o1, Map.Entry<Text, IntWritable> o2 )
  34.             {
  35.                 return (o2.getValue()).compareTo( o1.getValue() );
  36.             }
  37.         });
  38.         int cpt = 0;
  39.         for(Map.Entry<Text, IntWritable> entry:list) {
  40.             if(cpt < 10) {
  41.                 cpt ++;
  42.                 context.write(entry.getKey(), entry.getValue());
  43.             } else {
  44.                 break;
  45.             }
  46.         }
  47.     }
  48. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top