@Override public void reduce(NullWritable key, Iterable<TextArrayWritable> values, Context context) throws IOException, InterruptedException { // TODO // TextArrayWritable tmp; // while(values.hasNext()){ for (TextArrayWritable value : values) { for (Writable w : value.get()) { String str = ((Text) w).toString(); String arrStr[] = str.split("\\|"); tree.add(new Pair(Integer.parseInt(arrStr[0]), arrStr[1])); } } // reverse tree set TreeSet<Pair> treereverse = new TreeSet<Pair>(); treereverse = (TreeSet) tree.descendingSet(); Iterator itr = treereverse.iterator(); int count = 1; while (itr.hasNext() && count != N + 1) { Pair p = (Pair) itr.next(); // send top N entry context.write( new Text(p.second.toString()), new IntWritable(Integer.parseInt(p.first.toString()))); count++; } }
@Override public void reduce(NullWritable key, Iterable<TextArrayWritable> values, Context context) throws IOException, InterruptedException { for (TextArrayWritable val : values) { Text[] pair = (Text[]) val.toArray(); String word = pair[0].toString(); Float avg = Float.parseFloat(pair[1].toString()); /* Text tWord = new Text(word); FloatWritable value = new FloatWritable(avg); context.write(tWord, value); */ String[] parts = word.split("-"); String newKey = parts[0] + "-" + parts[1]; String carrier = parts[2]; if (!carrierAvgDelay.containsKey(newKey)) { TreeSet<Pair<Float, String>> avgDelayMap = new TreeSet<Pair<Float, String>>(); carrierAvgDelay.put(newKey, avgDelayMap); } TreeSet<Pair<Float, String>> ts = carrierAvgDelay.get(newKey); ts.add(new Pair<Float, String>(avg, carrier)); if (ts.size() > 10) { ts.remove(ts.last()); } carrierAvgDelay.put(newKey, ts); } Iterator it = carrierAvgDelay.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); TreeSet<Pair<Float, String>> ts = (TreeSet<Pair<Float, String>>) pair.getValue(); for (Pair<Float, String> item : ts) { Text word = new Text(pair.getKey() + "-" + item.second); FloatWritable value = new FloatWritable(item.first); context.write(word, value); } } }