Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. public static class MMMultTaskMapper extends
  2.             Mapper<Object, Text, IntWritable, Text> {
  3.  
  4.  
  5.         @Override
  6.         public void map(Object key, Text value, Context context)
  7.                 throws IOException, InterruptedException {
  8.            
  9.             String name = ((FileSplit) context.getInputSplit()).getPath().getName();
  10.             String[]  values = value.toString().split(" ");
  11.             if(name.charAt(0)=='M'){
  12.             //  System.out.println(values[1]);
  13.                 context.write(new IntWritable(Integer.parseInt(values[1])), new Text(("M " + values[0] + " " + values[2])));
  14.             }else{
  15.                 context.write(new IntWritable(Integer.parseInt(values[0])), new Text(("N " + values[1] + " " + values[2])));
  16.             }
  17.        
  18.         }
  19.     }
  20.  
  21.    
  22.     public static class MMMultTaskReducer extends
  23.             Reducer<IntWritable, Text, Text, IntWritable> {
  24.        
  25.         HashMap<String, Integer> nMap = new HashMap<String, Integer> ();
  26.         HashMap<String, Integer> mMap = new HashMap<String, Integer> ();
  27.        
  28.         @Override
  29.         public void reduce(IntWritable key, Iterable<Text> values,
  30.                 Context context) throws IOException, InterruptedException {
  31.        
  32.             for(Text text:values){
  33.                 //System.out.println(text.toString());
  34.                 String[] singleValues = text.toString().split(" ");
  35.                
  36.                 if(singleValues[0].equals("M")){
  37.                     mMap.put(singleValues[1], Integer.parseInt(singleValues[2]));
  38.                 }else{
  39.                     nMap.put(singleValues[1], Integer.parseInt(singleValues[2]));
  40.                 }
  41.             }
  42.            
  43.             StringBuilder sb = new StringBuilder();
  44.            
  45.             for(String keyM : mMap.keySet()){
  46.                 for(String keyN : nMap.keySet()){
  47.                     String result = keyM + " " + keyN + " " + Integer.toString(mMap.get(keyM) * nMap.get(keyN))+",";
  48.                     //System.out.println(result);
  49.                     sb.append(result);
  50.                 }
  51.             }
  52.            
  53.             //System.out.println(list.toString());
  54.            
  55.             context.write(new Text(sb.toString()), key);
  56.                
  57.         }
  58.     }
  59.  
  60.     public static class MMMultTask2Mapper extends
  61.             Mapper<Object, Text, Text, IntWritable> {
  62.  
  63.         @Override
  64.         public void map(Object key, Text value, Context context)
  65.                 throws IOException, InterruptedException {
  66.             //System.out.println(value.toString());
  67.                 String[] editedList = value.toString().split(",");
  68.                 //System.out.println(editedList[1]);
  69.                     for(int i = 0; i<editedList.length; i++){
  70.                         //System.out.println(editedList[i]);
  71.                        
  72.                         String[] line = editedList[i].split(" ");
  73.                         //System.out.println(Integer.toString(line.length));
  74.                         if(line.length>2){
  75.                            
  76.                             String I = line[0];
  77.                             String K = line[1];
  78.                             String V = line[2];
  79.                             context.write(new Text(I+" "+K), new IntWritable(Integer.parseInt(V)));
  80.                        
  81.                         }
  82.                     }
  83.         }
  84.     }
  85.  
  86.     public static class MMMultTask2Reducer extends
  87.             Reducer<Text, IntWritable, Text, IntWritable> {
  88.  
  89.         @Override
  90.         public void reduce(Text key, Iterable<IntWritable> values,
  91.                 Context context) throws IOException, InterruptedException {
  92.            
  93.             int sum = 0;
  94.             for(IntWritable value : values){
  95.                 sum += value.get();
  96.             }
  97.             context.write(key, new IntWritable(sum));
  98.         }
  99.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement