Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.94 KB | None | 0 0
  1. public class FirstJobReducer extends Reducer<Text, Text, Text, Text> {
  2.  
  3.     private ArrayList<Text> cache;
  4.  
  5.     @Override
  6.     public void reduce(Text carCategory, Iterable<Text> distance, Context context) throws IOException, InterruptedException {
  7.  
  8.         this.cache = new ArrayList<>();
  9.         double count = 0;
  10.         long totalDistance = 0;
  11.  
  12.         for (Text d : distance) {
  13.             totalDistance += (long)Double.parseDouble(d.toString());
  14.             cache.add(d);
  15.             count++;
  16.         }
  17.  
  18.         double average = totalDistance / count;
  19.         double variance = 0;
  20.  
  21.         for (Text d : cache) {
  22.             variance += Math.pow((Double.parseDouble(d.toString()) - average), 2);
  23.         }
  24.  
  25.         variance = variance / count;
  26.         String result = String.format("%.2f", average) + ";" + String.format("%.2f", variance) + ";" + totalDistance;
  27.  
  28.         context.write(carCategory, new Text(result));
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement