0

オブジェクトはWritable、Hadoop での送信時にシリアル化されるようにインターフェイスを実装する必要があります。ScoreDoc例としてLuceneクラスを取り上げます。

public class ScoreDoc implements java.io.Serializable {

  /** The score of this document for the query. */
  public float score;

  /** Expert: A hit document's number.
   * @see Searcher#doc(int) */
  public int doc;

  /** Only set by {@link TopDocs#merge} */
  public int shardIndex;

  /** Constructs a ScoreDoc. */
  public ScoreDoc(int doc, float score) {
    this(doc, score, -1);
  }

  /** Constructs a ScoreDoc. */
  public ScoreDoc(int doc, float score, int shardIndex) {
    this.doc = doc;
    this.score = score;
    this.shardIndex = shardIndex;
  }

  // A convenience method for debugging.
  @Override
  public String toString() {
    return "doc=" + doc + " score=" + score + " shardIndex=" + shardIndex;
  }
}

インターフェイスでどのようにシリアル化する必要がありWritableますか? Writableと の間の接続は何java.io.serializableですか?

4

2 に答える 2

0

最初に Hadoop を参照してください: Java シリアライゼーションを使用できる書き込み可能なインターフェイスなしでオブジェクトを出力値として持つ簡単な方法または

http://developer.yahoo.com/hadoop/tutorial/module5.htmlを参照してください。独自の書き込みおよび読み取り関数を作成する必要があります。内部で API を呼び出して int、flaot、string などを読み書きできるため、非常に簡単です。

書き込み可能な例(インポートする必要があります)

public class ScoreDoc implements java.io.Serializable, Writable  {      
    /** The score of this document for the query. */
    public float score;//... as in above

  public void write(DataOutput out) throws IOException {
      out.writeInt(score);
      out.writeInt(doc);
      out.writeInt(shardIndex);
  }

  public void readFields(DataInput in) throws IOException {
      score = in.readInt();
      doc = in.readInt();
      shardIndex = in.readInt();    
  }

  //rest toStirng etc
}

注:書き込みと読み取りの順序は同じである必要があります。そうしないと、一方の値が他方に移動し、タイプが異なる場合、読み取り時にシリアル化エラーが発生します

于 2013-05-30T14:09:43.110 に答える