/** * Clones this object. * * @return clone of this object */ public PairOfLongs clone() { return new PairOfLongs(this.leftElement, this.rightElement); }
/** * Checks two pairs for equality. * * @param obj object for comparison * @return <code>true</code> if <code>obj</code> is equal to this object, <code>false</code> * otherwise */ public boolean equals(Object obj) { PairOfLongs pair = (PairOfLongs) obj; return leftElement == pair.getLeftElement() && rightElement == pair.getRightElement(); }
/** * Creates a pair. * * @param left the left element * @param right the right element */ public PairOfLongs(long left, long right) { set(left, right); }
@Override public void reduce(Text key, Iterable<PairOfLongs> values, Context context) throws IOException, InterruptedException { Iterator<PairOfLongs> iter = values.iterator(); long sum = 0L; long cnt = 0L; while (iter.hasNext()) { PairOfLongs pair = iter.next(); sum += pair.getLeftElement(); cnt += pair.getRightElement(); } context.write(key, new PairOfLongs(sum, cnt)); } }
@Override public void reduce(Text key, Iterable<PairOfLongs> values, Context context) throws IOException, InterruptedException { Iterator<PairOfLongs> iter = values.iterator(); long sum = 0L; long cnt = 0L; while (iter.hasNext()) { PairOfLongs pair = iter.next(); sum += pair.getLeftElement(); cnt += pair.getRightElement(); } context.write(key, new PairOfLongs(sum, cnt)); } }
/** * Defines a natural sort order for pairs. Pairs are sorted first by the left element, and then by * the right element. * * @return a value less than zero, a value greater than zero, or zero if this pair should be * sorted before, sorted after, or is equal to <code>obj</code>. */ public int compareTo(PairOfLongs pair) { long pl = pair.getLeftElement(); long pr = pair.getRightElement(); if (leftElement == pl) { if (rightElement < pr) return -1; if (rightElement > pr) return 1; return 0; } if (leftElement < pl) return -1; return 1; }
public static PairOfLongs of(long left, long right) { return new PairOfLongs(left, right); }
@Override public void reduce(Text key, Iterable<PairOfLongs> values, Context context) throws IOException, InterruptedException { Iterator<PairOfLongs> iter = values.iterator(); long sum = 0L; long cnt = 0L; while (iter.hasNext()) { PairOfLongs pair = iter.next(); sum += pair.getLeftElement(); cnt += pair.getRightElement(); } context.write(key, new IntWritable((int) (sum/cnt))); } }
@Override public void map(Text key, Text value, Context context) throws IOException, InterruptedException { context.write(key, new PairOfLongs(Long.parseLong(value.toString()), 1L)); } }
@Override public void reduce(Text key, Iterable<PairOfLongs> values, Context context) throws IOException, InterruptedException { Iterator<PairOfLongs> iter = values.iterator(); long sum = 0L; long cnt = 0L; while (iter.hasNext()) { PairOfLongs pair = iter.next(); sum += pair.getLeftElement(); cnt += pair.getRightElement(); } context.write(key, new IntWritable((int) (sum/cnt))); } }
@Override public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { Iterator<IntWritable> iter = values.iterator(); long sum = 0L; long cnt = 0L; while (iter.hasNext()) { sum += iter.next().get(); cnt++; } context.write(key, new PairOfLongs(sum, cnt)); } }
@Override public void reduce(Text key, Iterable<PairOfLongs> values, Context context) throws IOException, InterruptedException { Iterator<PairOfLongs> iter = values.iterator(); long sum = 0L; long cnt = 0L; while (iter.hasNext()) { PairOfLongs pair = iter.next(); sum += pair.getLeftElement(); cnt += pair.getRightElement(); } context.write(key, new IntWritable((int) (sum/cnt))); } }
@Override public void cleanup(Context context) throws IOException, InterruptedException { for (String key : sums.keySet()) { context.write(new Text(key), new PairOfLongs(sums.get(key), counts.get(key))); } } }