2013-06-28 29 views
2

我正在使用Apache Giraph編寫分佈式集羣算法。在compute()方法中,我需要訪問每個鄰居發送的值加上當前頂點與發送該消息的鄰居之間的邊的權重。但是,我在Giraph示例中看到的唯一消息類型是單類型消息(DoubleWritable,IntWritable等),它只能傳遞值但不傳遞發件人信息,我們如何訪問發件人信息或邊緣信息呢?Apache Giraph SendMessage

例如,在上面的代碼中,我們可以獲取每條消息的值,但我們不知道哪個節點將此值發送到當前節點。

public void compute(Iterator<DoubleWritable> msgIterator) { 
    ... 
    double minDist = isSource() ? 0d : Double.MAX_VALUE; 
    while (msgIterator.hasNext()) { 
     // Get who sent this message, how? 
     minDist = Math.min(minDist, msgIterator.next().get()); 
    } 
    ... 
} 

感謝,

+0

一個代碼示例可能會吸引更多的關注。 –

+0

謝謝安德烈斯,做到了。 – user2533067

+0

您當然可以根據自己的需要編寫自己的「Writable」實現。如果你根本不涉及Hadoop,請不要使用Hadoop標籤。 –

回答

4

我同意托馬斯Jungblut;編寫自己的Writable可能是最好的(也是最簡單的)解決方案。

我最近寫了一個定製的Writable,叫做IntPairWritable,它只保存兩個整數。這是我的代碼。

import java.io.DataInput; 
import java.io.DataOutput; 
import java.io.IOException; 
import org.apache.giraph.utils.IntPair; 
import org.apache.hadoop.conf.Configurable; 
import org.apache.hadoop.conf.Configuration; 
import org.apache.hadoop.io.Writable; 

public class IntPairWritable extends IntPair implements Writable, Configurable { 

    private Configuration conf; 

    public IntPairWritable() { 
     super(0, 0); 
    } 

    public IntPairWritable(int fst, int snd) { 
     super(fst, snd); 
    } 

    @Override 
    public void readFields(DataInput input) throws IOException { 
     super.setFirst(input.readInt()); 
     super.setSecond(input.readInt()); 
    } 

    @Override 
    public void write(DataOutput output) throws IOException { 
     output.writeInt(super.getFirst()); 
     output.writeInt(super.getSecond()); 
    } 

    @Override 
    public Configuration getConf() { 
     return this.conf; 
    } 

    @Override 
    public void setConf(Configuration conf) { 
     this.conf = conf; 
    } 

    @Override 
    public String toString() { 
     return super.getFirst() + "," + super.getSecond(); 
    } 
} 

Writable類可能看起來只是相似。也許就像

public class RetraceableWritable<I extends Writable, D extends Writable> implements Writable, Configurable { 
    private I senderId; 
    private D data; 
    ... 

...等等。


  • 注1:默認構造函數必須始終存在,以確保Hadoop的可創建類的實例。
  • 注2:當所有東西都是configurable時,Giraph似乎喜歡它,所以實現這個接口是個好主意。

問候