Advertisement
Guest User

groupby

a guest
Nov 22nd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. public static class Map extends Mapper<LongWritable, Text, Text, DoubleWritable> {
  2.         //private final static DoubleWritable one = new DoubleWritable(1);
  3.         private final static String emptyWords[] = { "" };
  4.  
  5.         @Override
  6.         public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  7.             // TODO: à compléter
  8.             String line = value.toString();
  9.             // Une méthode pour créer des messages de log
  10. //          LOG.info("MESSAGE INFO");
  11.  
  12.             String[] words = line.split(",");
  13.             // La ligne est vide : on s'arrête
  14.             if (Arrays.equals(words, emptyWords))
  15.                 return;
  16.  
  17.             //for (String word : words)
  18.             try{
  19.                 context.write(new Text(words[5]), new DoubleWritable(Double.parseDouble(words[words.length-1])));
  20.             }catch(RuntimeException e)
  21.             {
  22.                 System.out.println(e);
  23.             }
  24.                
  25.         }
  26.        
  27.     }
  28.  
  29.     public static class Reduce extends Reducer<Text, DoubleWritable, Text, DoubleWritable> {
  30.  
  31.         @Override
  32.         public void reduce(Text key, Iterable<DoubleWritable> values, Context context)
  33.                 throws IOException, InterruptedException {
  34.             // TODO: à compléter
  35.             int sum = 0;
  36.  
  37.             for (DoubleWritable val : values)
  38.                 sum += val.get();
  39.            
  40.             context.write(key, new DoubleWritable(sum));
  41.         }
  42.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement