コード例 #1
0
    // reduce(nid m, [d1,d2,...])
    public void reduce(Text key, Iterable<NodeWritable> values, Context context)
        throws IOException, InterruptedException {

      float spokeVal = 0.0f;
      float hubVal = 0.0f;
      ArrayList<NodeWritable> arr = new ArrayList<NodeWritable>();
      NodeWritable M = null;

      // Extract Original Node from Values
      for (NodeWritable x : values) {
        if (x.getNid().toString().equals(key.toString())) {
          M = new NodeWritable(x.toString());
          break;
        }
      }

      // Store All Values in arr
      for (NodeWritable x : values) {
        arr.add(x);
      }

      // Create List of All In Nodes
      ArrayList<NodeWritable> ins = new ArrayList<NodeWritable>();

      // Create List of all Out Nodes
      ArrayList<NodeWritable> outs = new ArrayList<NodeWritable>();

      // Populate Above Lists
      for (NodeWritable x : arr) {
        if (!x.getIsInList().toString().equals("***")) {
          if (x.getIsInList().toString().equals("YES")) {
            ins.add(x);
          } else {
            if (x.getIsInList().toString().equals("NO")) {
              outs.add(x);
            }
          }
        }
      }

      // For Each Out nodes change the current hubVal
      for (NodeWritable t : outs) {
        hubVal += Float.parseFloat(t.getSpokeVal().toString());
      }

      // For Each In Nodes change current spokeVal
      for (NodeWritable t : ins) {
        spokeVal += (Float.parseFloat(t.getHubVal().toString()));
      }

      // Set new  Hub value
      M.getHubVal().set(hubVal);

      // Set new Spoke Value
      M.getSpokeVal().set(spokeVal);

      // Store current Node in Map
      map.put(M.getNid(), M);
    }
コード例 #2
0
    public void cleanup(Context context) throws IOException, InterruptedException {
      float normHubVal = (float) 0.0;
      float normSpokeVal = (float) 0.0;

      // Calculate Sum of Squares of Each nodes Hub And spoke value
      for (Text nid : map.keySet()) {
        NodeWritable n = map.get(nid);
        normHubVal += Math.pow(n.getHubVal().get(), 2);
        normSpokeVal += Math.pow(n.getSpokeVal().get(), 2);
      }

      // Normalize the Hub And Spoke values and Emit for next iteration
      for (Text nid : map.keySet()) {
        NodeWritable n = map.get(nid);
        n.getHubVal().set((float) (n.getHubVal().get() / Math.sqrt(normHubVal)));
        n.getSpokeVal().set((float) (n.getSpokeVal().get() / Math.sqrt(normSpokeVal)));
        context.write(n, new Text());
      }
    }