这段代码是从《Practical Graph Analytics with Apache Giraph》一书中借来的。
我的代码的目标是将图形从仅使用单向边转换为使用双向边。
我的数据是 TextDoubleDoubleAdjacencyListVertexInputFormat 的制表符分隔的文本文件:
A 0 B 0
B 0
C 0 A 0 D 0
D 0 B 0 G 0
E 0 A 0 B 0 D 0
F 0 C 0 D 0
G 0 C 0 E 0
我最小的非工作示例是:
import org.apache.giraph.GiraphRunner;
import org.apache.giraph.graph.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.util.ToolRunner;
import org.apache.giraph.edge.EdgeFactory;
public class DigraphToGraph extends BasicComputation<Text,DoubleWritable,DoubleWritable,Text>
{
static final DoubleWritable ORIG_E = new DoubleWritable (1),
NEW_E = new DoubleWritable (2);
@Override
public void compute(Vertex <Text,DoubleWritable, DoubleWritable> vertex, Iterable<Text> messages) {
if (getSuperstep () == 0)
sendMessageToAllEdges (vertex, vertex.getId ());
else {
for (Text m:messages) {
DoubleWritable edgeValue = vertex.getEdgeValue (m);
if (edgeValue == null)
vertex.addEdge (EdgeFactory.create (m, NEW_E));
else
// problem HERE: try to relabel edge
vertex.setEdgeValue (m, ORIG_E);
}
}
vertex.voteToHalt ();
}
}
执行此代码后,我期望边缘只有 1 或 2 个值。但是,我得到的是:
G 0.0 C 0.0 E 0.0 D 2.0
E 0.0 A 0.0 B 0.0 D 0.0 G 2.0
F 0.0 C 0.0 D 0.0
D 0.0 B 0.0 G 0.0 E 2.0 B 2.0 C 2.0
B 0.0 E 2.0 D 2.0 A 2.0
C 0.0 A 0.0 D 0.0 G 2.0 B 2.0
A 0.0 B 0.0 E 2.0 C 2.0
为什么我的代码没有修改预先存在的边缘?