-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathShortestPathsReducer.java
55 lines (46 loc) · 1.82 KB
/
ShortestPathsReducer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.log4j.Logger;
public class ShortestPathsReducer
extends Reducer<Text, Text, Text, Text> {
private Logger logger = Logger.getLogger(this.getClass());
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
Text result = new Text();
long minDistance = Long.MAX_VALUE;
String pathFromSource = null;
String neighbours = null;
Counter updated =
context.getCounter(ShortestPaths.MoreIterations.numUpdated);
long existingDistance = Long.MAX_VALUE;
for (Text val : values) {
logger.debug("Input: " + key + " " + val);
String[] curNodeData = val.toString().split("\\s+");
if (curNodeData.length == 2) {
long distance = Long.parseLong(curNodeData[0]);
if (distance < minDistance) {
minDistance = distance;
pathFromSource = curNodeData[1];
}
} else {
existingDistance = Long.parseLong(curNodeData[1]);
if (existingDistance < minDistance) {
minDistance = existingDistance;
pathFromSource = curNodeData[2];
}
neighbours = curNodeData[0];
}
}
if (minDistance < existingDistance) {
updated.increment(1);
}
result.set(String.join(" ", neighbours,
Long.toString(minDistance),
pathFromSource));
logger.debug(key + " " + result);
context.write(key, result);
}
}