2016-03-24 7 views
0

Hadoop初心者はこちらです。 私は、ある行の単語が他の単語と何回表示されているかのように、1行の単語の同時発生を数えたいと思います。 そのために、私は特別なクラスの単語ペアを作成しました。そのため、MapReduceは私に単語のペアを与え、次にカウントを与えます。問題は、結果が乱れ、私はどこが間違っているのか分かりません。Hadoop結果が台無しになりました

マイ単語対クラスは、このようなものです:

public class Co_OcurrenciaMapper extends Mapper<LongWritable, Text, Par, IntWritable> { 
    @Override 
    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { 
     IntWritable one = new IntWritable(1); 
     String[] palabras = value.toString().split("\\s+"); 
     if (palabras.length > 1) { 
      for (int i = 0; i < palabras.length - 1; i++) { 
       for (int j = i + 1; j < palabras.length; j++) { 
        context.write(new Par(palabras[i], palabras[j]), one); 
       } 
      } 
     } 
    } 
} 

そして、私はMapReduceので得た結果は次のとおりです:

public class Par implements Writable,WritableComparable<Par> { 

    public String palabra; 
    public String vecino; 

    public Par(String palabra, String vecino) { 
     this.palabra = palabra; 
     this.vecino = vecino; 
    } 

    public Par() { 
     this.palabra = new String(); 
     this.vecino = new String(); 
    } 

    @Override 
    public int compareTo(Par otra) { 
     int retorno = this.palabra.compareTo(otra.palabra); 
     if(retorno != 0){ 
      return retorno; 
     } 
     return this.vecino.compareTo(otra.vecino); 
    } 

    @Override 
    public void write(DataOutput out) throws IOException { 
     out.writeUTF(palabra); 
     out.writeUTF(vecino); 
    } 

    @Override 
    public void readFields(DataInput in) throws IOException { 
     palabra = in.readUTF(); 
     vecino = in.readUTF(); 
    } 

    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((palabra == null) ? 0 : palabra.hashCode()); 
     result = prime * result + ((vecino == null) ? 0 : vecino.hashCode()); 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     Par other = (Par) obj; 
     if (palabra == null) { 
      if (other.palabra != null) 
       return false; 
     } else if (!palabra.equals(other.palabra)) 
      return false; 
     if (vecino == null) { 
      if (other.vecino != null) 
       return false; 
     } else if (!vecino.equals(other.vecino)) 
      return false; 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "Par [" + palabra + " , " + vecino + "]"; 
    } 


} 

私のマッパーがある

私は間違っ
[[email protected] Desktop]$ hadoop fs -cat salidaO11/part-r-00000 |head -15 
Par [ , &c.] 35 
Par [ , &c.'] 2 
Par [ , &c.,] 4 
Par [ , &c]] 23 
Par [ , '] 6 
Par [ , ''Od's] 1 
Par [ , ''Tis] 2 
Par [ , ''tis] 1 
Par [ , ''twas] 1 
Par [ , '--O] 1 
Par [ , 'A] 17 
Par [ , 'ARTEMIDORUS.'] 1 
Par [ , 'Above] 1 
Par [ , 'Achilles] 2 
Par [ , 'Ad] 3 
cat: Unable to write to output stream. 

?友人は2つの単語を1つのStringに結びつけるように提案しましたが、それはそれほどエレガントではないと思います。

+0

質問が返ってきたので、MRの仕組みを知りたいと思っていますか?そうでなければ、あなたはスパークを見ているべきです。この全体の仕事は3行のコードで書くことができます: 'val input = sc.textFile(" s3:// ... ") val words = input.flatMap(x => x.split(" ")) reduceByKey((x、y)=> x + y) ' – Havnar

+0

正確には、私はMapReduceを学んでいるので、これは何かよりも練習問題です。製造。 –

答えて

2

私はここに何か間違っているとは思わない。あなたはデータクレンジングをしていないようですので、そのような汚れた出力を生成することは公正だと思います。

いくつかのMRUnitテストを作成したり、仕事に小さな、よりクリーンなデータセットを入力して、それが期待どおりに実行されていることを確認できます。

+0

テキストファイルは完全にきれいです。私はすでに他のMapReduceタスクに問題なく使用しました。私はMRUnitテストを書くつもりです。ありがとう! –

関連する問題