2012-04-10 13 views
0

私は非分散モードでHadoopのワードカウントの例を実行することができました。私は "part-00000"という名前のファイルに出力します。私はそれがすべての入力ファイルのすべての単語を組み合わせてリストすることがわかります。ファイル数Wordcount一般的な単語

ワードカウントコードをトレースした後、私はそれが行を取り、スペースに基づいて単語を分割することがわかります。

私は、複数のファイルに出現した単語とその出現をリストする方法を考えようとしていますか?これはMap/Reduceで達成できますか? -Added- これらの変更は適切ですか?

 //changes in the parameters here 

    public static class Map extends MapReduceBase implements Mapper<Text, Text, Text, Text> { 

     // These are the original line; I am not using them but left them here... 
     private final static IntWritable one = new IntWritable(1); 
     private Text word = new Text(); 

        //My changes are here too 

     private Text outvalue=new Text(); 
     FileSplit fileSplit = (FileSplit)reporter.getInputSplit(); 
     private String filename = fileSplit.getPath().getName();; 



     public void map(Text key, Text value, OutputCollector<Text, Text> output, Reporter reporter) throws IOException { 

     String line = value.toString(); 
     StringTokenizer tokenizer = new StringTokenizer(line); 
     while (tokenizer.hasMoreTokens()) { 
     word.set(tokenizer.nextToken()); 

      // And here   
       outvalue.set(filename); 
      output.collect(word, outvalue); 

     } 

     } 

    } 

答えて

0

あなたは、キー、およびその単語がどこから来たのファイル名を表す値としてテキストとして出力する単語をマッパーを修正することができます。あなたの減速機では、ファイル名を切り捨てて、その単語が複数のファイルに表示されるエントリを出力するだけです。

処理されるファイルのファイル名を取得するかどうかは、新しいAPIを使用しているかどうかによって異なります(mapredまたはmapreduceパッケージ名)。新しいAPIについては、getInputSplitメソッドを使用してContextオブジェクトからマッパー入力の分割を抽出できることを理解しています(を使用していると仮定するとInputSplitからFileSplitになる場合があります)。古いAPIの場合は試したことがありませんが、明らかにmap.input.fileというコンフィグレーションプロパティを使用することができます。

これは、同じマッパーから複数の単語の出現を除外するためにも適しています。

更新

だからあなたの問題に対応して、あなたは次のように修正し、マッパーのクラスscoptに存在しないレポーターと呼ばれるインスタンス変数を、使用しようとしている:

public static class Map extends MapReduceBase implements Mapper<Text, Text, Text, Text> { 
    // These are the original line; I am not using them but left them here... 
    private final static IntWritable one = new IntWritable(1); 
    private Text word = new Text(); 

    //My changes are here too 
    private Text outvalue=new Text(); 
    private String filename = null; 

    public void map(Text key, Text value, OutputCollector<Text, Text> output, Reporter reporter) throws IOException { 
    if (filename == null) { 
     filename = ((FileSplit) reporter.getInputSplit()).getPath().getName(); 
    } 

    String line = value.toString(); 
    StringTokenizer tokenizer = new StringTokenizer(line); 
    while (tokenizer.hasMoreTokens()) { 
     word.set(tokenizer.nextToken()); 

     // And here   
     outvalue.set(filename); 
     output.collect(word, outvalue); 
    } 
    } 
} 

(SO上記に書式設定を尊重されていない理由は本当にわからない...)

+0

はあなたにクリスありがとう...それを行う方法に私を導いていただけますか?私はワードカウントマップクラスに次の行を追加しました: \t \t \t FileSplit fileSplit =(FileSplit)reporter.getInputSplit(); \t \t \tプライベート文字列ファイル名= fileSplit.getPath()。getName();; \t \t \t output.collectの中のwhileループの中で次のもの(word、filename)。 \t 私はこれまでに何をしていますか?現在の単語を現在のファイルにするための最初のステップとして... – ibininja

+0

私は現在、Hadoopを使用しています。0.20.2 – ibininja

+0

私にはいい音です。見てみましょう(FYI、0.20.2を使っていても、まだ運動しています古いAPI) –

関連する問題