2016-03-30 9 views
-1

Hadoopプログラミングの新機能です。申し訳ありませんが、これは愚かな質問ですが、私を助けてください。Javaを使用してHadoopでカスタムリンクリストを作成する

私のプロジェクトでは、(キー、値)のペアの値のリンクリストを持つカスタムデータ型を作成する必要があります。ここに私のクラスです。

public class Nikhil implements Writable { 
    String str; 
    Nikhil next; 
    Nikhil() { 
    this.str=""; 
    this.next=null; 
    } 

    public void add(String t) { 
    if(this.str.equals("")) { 
     this.str=t; 
     this.next=null; 
     return; 
    } else { 
     Nikhil n=new Nikhil(); 
     n.str=t; 
     n.next=null; 
     Nikhil temp=this; 
     while(temp.next!=null) 
     { 
     temp=temp.next; 
     } 
     temp.next=n; 
     return; 
    } 
    } 

    public String get() 
    { 
    if(!this.str.toString().equals("")) 
    { 
     String result=""; 
     result=this.str.toString(); 
     Nikhil temp=this.next; 
     while(temp!=null) 
     { 
     result=result+","+temp.str.toString(); 
     temp=temp.next; 
     } 
     return result; 
    } 
    else 
     return ""; 
    } 

    @Override 
    public void readFields(DataInput in) throws IOException { 
    str=in.readUTF(); 
    //some code for reading next pointer 
    } 

    @Override 
    public void write(DataOutput out) throws IOException { 
    out.writeUTF(str); 
    //some code for next 
    // 
    } 
} 

次のコードを修正して、私の問題を解決してください。hadoopでカスタムデータ型としてツリーを形成するアプローチは何ですか?

+1

あなたのコードにはHadoopに関連するものはありません。Javaだけです。 –

+0

これは、私が減算器に送るために私のmappper値で使用しているカスタムデータ型です。私はreadFieldsと混乱していますし、上記のコードで関数を書く – Nikhil

+0

あなたは_question_を持っていますか?既に持っているコードに何が問題なのですか?コンパイルエラーがある場合、それは何ですか?ランタイム例外がある場合、スタックトレースは何ですか?それが実行されたが何かが間違っていた場合、それは何をしたのですか? (また、木はこれと何をしなければならないのでしょうか?その最後の文が別の質問を開始したら、[新しい質問](https://stackoverflow.com/questions/ask)でそれを尋ねます)。 –

答えて

0
@Override 
    public void readFields(DataInput in) throws IOException { 
     str = in.readUTF(); 
     //some code for reading next pointer 
     if (!"".equals(str)) { 
      boolean existNext = in.readBoolean(); 
      if (existNext) { 
       next = new Nikhil(); 
       next.readFields(in); 
      } 
     } 
    } 

    @Override 
    public void write(DataOutput out) throws IOException { 
     out.writeUTF(str); 
     //some code for next 
     // 
     if (!"".equals(str)) { 
      boolean existNext = null != next; 
      out.writeBoolean(existNext); 
      if (existNext) { 
       next.write(out); 
      } 
     } 
    } 

多分上記のコードはあなたが望むものです。あなたのコードの他の部分、特にadd()はそれほど厳格ではありません。より多くの最適化を行うべきです。

関連する問題