2016-11-29 9 views
1

MapReduceを学習していて、入力ファイル(文章単位)を読み込み、「snake」という単語が含まれていない場合にのみ出力ファイルに書き出します。MapReduceで特定の単語を含む文全体を削除します

など。入力ファイル:

This is my first sentence. This is my first sentence. 
This is my first sentence. 

The snake is an animal. This is the second sentence. This is my third sentence. 

Another sentence. Another sentence with snake. 

その後、出力ファイルは次のようになります。文(value)はワードヘビが含まれている場合

This is my first sentence. This is my first sentence. 
This is my first sentence. 

This is the second sentence. This is my third sentence. 

Another sentence. 

そうするために、私は、mapメソッド内で、確認してください。文章に蛇語が含まれていない場合は、その文章をcontextに書きます。

さらに、私は減損タスクの数を0に設定します。そうでない場合は、出力ファイルにランダムな順序で文を取得します(最初の文、次に3番目の文、次に2番目の文など)。

私のコードは正常にヘビの単語と文をフィルタリングんが、問題は、それはこのように、新しい行にそれぞれの文が書き込まれていることである:私は新しい行に文を書くことができますどのように

This is my first sentence. 
This is my first sentence. 

This is my first sentence. 
This is the second sentence. 
This is my third sentence. 


Another sentence. 

. 

のみその文が入力テキストの新しい行に表示されたら?以下は、私のコードです:

public class RemoveSentence { 

    public static class SentenceMapper extends Mapper<Object, Text, Text, NullWritable>{ 

     private Text removeWord = new Text ("snake"); 

     public void map(Object key, Text value, Context context) throws IOException, InterruptedException { 
      if (!value.toString().contains(removeWord.toString())) { 
       Text currentSentence = new Text(value.toString()+". "); 
       context.write(currentSentence, NullWritable.get()); 
      } 
     } 
    } 


    public static void main(String[] args) throws Exception { 
     Configuration conf = new Configuration(); 
     conf.set("textinputformat.record.delimiter", "."); 

     Job job = Job.getInstance(conf, "remove sentence"); 
     job.setJarByClass(RemoveSentence.class); 

     FileInputFormat.addInputPath(job, new Path(args[0])); 
     FileOutputFormat.setOutputPath(job, new Path(args[1])); 

     job.setMapOutputKeyClass(Text.class); 
     job.setMapOutputValueClass(NullWritable.class); 

     job.setMapperClass(SentenceMapper.class); 
     job.setNumReduceTasks(0); 

     System.exit(job.waitForCompletion(true) ? 0 : 1); 
    } 
} 

Thisthis otherソリューションは、それがcontext.write(word, null);を設定するのに十分であるべきであるが、私の場合には動作しませんでした。

もう1つの問題はconf.set("textinputformat.record.delimiter", ".");と関連しています。さて、これは文章間の区切りをどのように定義するかです。このため、出力ファイルの文章が空白で始まることがあります(例:This is my first sentence.)。代わりに私はこれをこのように設定しようとしていますconf.set("textinputformat.record.delimiter", ". ");(完全な停止の後にスペースを入れます)が、このようにJavaアプリケーションはすべての文章を出力ファイルに書き出しません。

答えて

0

問題を解決するのに非常に近いです。 MapReduceプログラムの動作について考えてみましょう。あなたのマップメソッドは、 "。"で区切られたすべての単一の文を取ります。 (デフォルトではあなたが知っている改行文字です)を新しい値として返し、ファイルに書き込みます。すべてのmap()コールの後に改行を書くことを無効にするプロパティが必要です。私は確信していませんが、私はそのような財産が存在するとは思わない。

1つの回避策は通常どおり処理されます。例レコードは次のようになります。

This is first sentence. This is second snake. This is last.

言葉「蛇」を検索し、見つかった場合は、直前の後にすべてのものを削除します「」次へ "。"新しいStringをパッケージ化し、それをコンテキストに書き出します。

もちろん、map()呼び出しの後で改行を無効にする方法が見つかると、それは最も簡単です。

これが役に立ちます。

関連する問題