调用Vertex.getEdgeValue()两次后,EdgeValue不相同

时间:2015-05-21 21:47:47

标签: java algorithm hadoop graph giraph

我试图在giraph中实现Spinner图分区算法。 在第一步中,我的程序将边添加到给定的输入图,使其成为无向图,每个顶点选择一个随机分区。 (此分区整数存储在VertexValue中)在此初始化步骤结束时,每个顶点向顶点ID(a LongWritable)和顶点选择的分区的所有传出边发送消息

一切正常。现在,在遇到问题的步骤中,每个顶点迭代接收到的消息,并将接收到的分区保存在相应边缘的EdgeValue中。 (VertexValue VVertex<I,V,E>EdgeValue EEdge<I,E>

以下是我的代码的重要部分:

包装类:

public class EdgeValue implements Writable {
private int weight;
private int partition;
// Getters and setters for weight and partition
    public EdgeValue() {
    this.weight = -2;
    this.partition = -1;
}
// Constructors taking 1 and 2 ints and setting weight/partition to the given value

@Override
public void readFields(DataInput in) throws IOException {
    this.weight = in.readInt();
    this.partition = in.readInt();
}

@Override
public void write(DataOutput out) throws IOException {
    out.writeInt(this.weight);
    out.writeInt(this.partition);
}
}

public class SpinnerMessage implements Writable, Configurable {
private long senderId;
private int updatePartition;
public SpinnerMessage() {
    this.senderId = -1;
    this.updatePartition = -1;
}
// Constructors taking int and/or LongWritable and setting the fields
// Getters and setters for senderId and updatePartition

@Override
public void readFields(DataInput in) throws IOException {
    this.senderId = in.readLong();
    this.updatePartition = in.readInt();
}

@Override
public void write(DataOutput out) throws IOException {
    out.writeLong(this.senderId);
    out.writeInt(this.updatePartition);
}
}

之前步骤中的compute方法(运行是Random对象):

public void compute(Vertex<LongWritable, VertexValue, EdgeValue> vertex, Iterable<LongWritable> messages) {
    int initialPartition = this.ran.nextInt(GlobalInformation.numberOfPartitions);
    vertex.getValue().setPartition(initialPartition);
    sendMessageToAllEdges(vertex, new SpinnerMessage(vertex.getId(),initialPartition));
}

发生错误的步骤中的compute方法:

public void compute(Vertex<LongWritable, VertexValue, EdgeValue> vertex,Iterable<SpinnerMessage> messages) throws IOException {
for (SpinnerMessage m : messages) {
    vertex.getEdgeValue(new LongWritable(m.getSenderWritable().get())).setPartition(m.getUpdatePartition());
}
// ... some other code, e.g. initializing the amountOfNeighbors array.
// Here I get an ArrayIndexOutOfBoundsException since the partition is -1:
for (Edge<LongWritable, EdgeValue> edge : vertex.getEdges()) {
    EdgeValue curValue = edge.getValue();
    amountOfNeighbors[curValue.getPartition()] += curValue.getWeight();
}

然而,当我用例如

迭代边缘时
for(Edge<LongWritable, EdgeValue> e : vertex.getEdges())

或通过

vertex.getEdgeValue(someVertex)

然后返回的EdgeValue具有权重-2和分区-1(标准构造函数中的默认值)

我的想法可能导致错误:

  1. getEdgeValue(new LongWritable(someLong))可能不起作用,因为它与具有相同值的另一个new LongWritable(someLong)不同。但是,我已经看到giraph代码中使用了这个,所以这似乎没有问题,只有LongWritable内存储的长期似乎很重要。

  2. 最可能的原因)Hadoop序列化和反序列化以某种方式改变了我的EdgeValue对象。由于Hadoop适用于非常大的图形,因此它们可能不适合RAM。为此,VertexValueEdgeValue必须实施Writable。然而,在用一些giraph代码在线检查之后,我以一种对我来说似乎正确的方式实现了read()write()(以相同的顺序编写和阅读重要字段)。 (由于第二次调用返回的EdgeValue具有标准构造函数的字段值,因此我认为这与问题有关,

  3. 我还在文档中读到了一些内容:

      

    E getEdgeValue(I targetVertexId)   返回具有给定目标顶点id的第一个边的值,如果没有这样的边,则返回null。注意:下一次调用可能会使此方法返回的边值对象无效。因此,保持对边缘值的引用几乎总会导致不希望的行为。

    但是,这并不适用于我,因为我只有一个EdgeValue变量,对吧?

    提前感谢所有花时间帮助我的人。 (我使用hadoop 1.2.1和giraph 1.2.0)

1 个答案:

答案 0 :(得分:0)

在查看了一些giraph代码示例后,我找到了解决方案:Vertex.getEdgeValue()方法基本上创建了EdgeValue的副本 顶点。如果更改它返回的对象,则不会写入这些更改 回到磁盘。要在EdgeValueVertexValue中保存信息,您必须使用setVertexValue()setEdgeValue()