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()); } }
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { NodeWritable n = new NodeWritable(); n.setNid(key); for (Text value : values) { String[] valueArray = value.toString().split(","); String flag = valueArray[0]; if (flag.equals("In")) { NodeWritable x = new NodeWritable(); x.setNid(new Text(valueArray[1])); n.addInNode(x); } else { NodeWritable x = new NodeWritable(); x.setNid(new Text(valueArray[1])); n.addOutNode(x); } } context.write(n, new Text()); }
@Override public void reduce( Text key, Iterator<NodeWritable> values, OutputCollector<Text, NodeWritable> output, Reporter reporter) throws IOException { // The nodes are either the node definition or possible distances to the // node. // For a given key, there should be a single node definition and // multiple distances. // Find the node definition and the minimum distance. int minDistance = Integer.MAX_VALUE; NodeWritable theNode = new NodeWritable(); while (values.hasNext()) { NodeWritable nextNode = values.next(); if (nextNode.isNode()) { // There should only be one per key since the mapper emits the node once to // preserve the graph from one iteration to another. theNode.setNode(nextNode); } if (nextNode.isDistanceKnown() && nextNode.getDistance() < minDistance) { minDistance = nextNode.getDistance(); } } if (minDistance != Integer.MAX_VALUE) { // We found the minimum theNode.setDistance(minDistance); // Increment a counter indicating that the shortest path to the // node has been found. reporter.getCounter("node", "nodeDistanceSet").increment(1); } // output the node with the distance updated. output.collect(key, theNode); }
// 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); }
public void map(Object key, Text value, Context context) throws IOException, InterruptedException { NodeWritable n = new NodeWritable(value.toString().trim()); // Emit node to carry forward the Model. NodeWritable p = new NodeWritable(value.toString().trim()); p.setIsNode(new Text("YES")); p.setIsInList(new Text("***")); context.write(new Text(p.getNid().toString()), p); // For Each OutLinks Emit This Node for (NodeWritable x : n.getOuts()) { if (!x.getNid().toString().equals(n.getNid().toString())) { n.setIsInList(new Text("YES")); n.setIsNode(new Text("NO")); context.write(new Text(x.getNid().toString()), n); } } // For Each Inlinks Emit This Node for (NodeWritable x : n.getIns()) { if (!x.getNid().toString().equals(n.getNid().toString())) { n.setIsInList(new Text("NO")); n.setIsNode(new Text("NO")); context.write(new Text(x.getNid().toString()), n); } } }