Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FirstJobReducer extends Reducer<Text, Text, Text, Text> {
- private ArrayList<Text> cache;
- @Override
- public void reduce(Text carCategory, Iterable<Text> distance, Context context) throws IOException, InterruptedException {
- this.cache = new ArrayList<>();
- double count = 0;
- long totalDistance = 0;
- for (Text d : distance) {
- totalDistance += (long)Double.parseDouble(d.toString());
- cache.add(d);
- count++;
- }
- double average = totalDistance / count;
- double variance = 0;
- for (Text d : cache) {
- variance += Math.pow((Double.parseDouble(d.toString()) - average), 2);
- }
- variance = variance / count;
- String result = String.format("%.2f", average) + ";" + String.format("%.2f", variance) + ";" + totalDistance;
- context.write(carCategory, new Text(result));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement