我認爲這將很容易實現,但它開始是一個痛苦。Apache Giraph/Hadoop:迭代通過自定義ArrayWritable
我有像這樣一個ArrayWritable子類:
public class VertexDistanceArrayWritable extends ArrayWritable {
public VertexDistanceArrayWritable() {
super(VertexDistanceWritable.class);
}
public VertexDistanceArrayWritable(VertexDistanceWritable[] v) {
super(VertexDistanceWritable.class, v);
}
}
和可寫的子類,像這樣:
public class VertexDistanceWritable implements Writable {
//Implements write, readFields, and some custom functions that aren't used yet
}
在我Giraph計算功能,消息是VertexDistanceArrayWritable的。我想遍歷每個VertexDistanceWritable每條消息(VertexDistanceArrayWritable)。這裏是我的計算功能:
@Override
public void compute(Vertex<Text, MapWritable, FloatWritable> vertex,
Iterable<VertexDistanceArrayWritable> messages) throws IOException {
for(VertexDistanceArrayWritable message : messages) {
for(VertexDistanceWritable distEntry : message) {
//Do stuff with distEntry
}
}
//do other stuff
vertex.voteToHalt();
}
當我編譯的代碼,我得到這個錯誤:
for-each not applicable to expression type
for(VertexDistanceWritable distEntry : message) {
required: array or java.lang.Iterable
found: VertexDistanceArrayWritable
所以現在我有一個問題。我想迭代arrayWritable子類。
我已經試過如下:
把上面一行
for(VertexDistanceWritable distEntry : message.toArray())
告訴我,換每個不applicaable鍵入對象(需要:數組或java.lang.Iterable,發現:對象) 。將該行更改爲
for(VertexDistanceWritable distEntry : message.get())
,這給我error: incompatible types
-required: VertexDistanceWritable, found: Writable
。 這是最奇怪的問題 - VertexDistanceWritable擴展了Writable,這不應該好嗎?爲VertexDistanceWritable編寫我自己的自定義「get_foo()」函數,該函數返回VertexDistanceWritable []的值。當然,值是私有的,並根據該我已經具有
我只是想辦法遍歷我VertexDistanceArrayWritable類問題的其他the documentation比得到的()沒有getter函數。這甚至可以在Hadoop中使用嗎?它必須是,對吧?我應該可以迭代我在數組中創建的一組元素,不是嗎?這似乎是非常基本的東西。