Example #1
0
    @Override
    protected void map(
        BytesWritable key,
        ArrayWritable value,
        Mapper<BytesWritable, ArrayWritable, BytesWritable, TextArrayWritable>.Context context)
        throws IOException, InterruptedException {
      Map<String, Report> reports = new LinkedHashMap<String, Report>();

      Writable[] repts = value.get();
      if (repts.length == 0 || !(repts[0] instanceof Text)) {
        System.out.println("error: bad input.");
        return; // bail out more drastically
      }
      Text[] repts_as_text = (Text[]) repts;
      for (Text t : repts_as_text) {
        Report r = Report.createFromString(t.toString());
        reports.put(r.getMetadata().getOpIdString(), r);
      }

      Text[] indexed = indexGraph(reports);
      TextArrayWritable output = new TextArrayWritable();
      output.set(indexed);

      context.write(key, output);
    }
    @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);
        }
      }
    }