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);
}
}
Thisとthis otherソリューションは、それがcontext.write(word, null);
を設定するのに十分であるべきであるが、私の場合には動作しませんでした。
もう1つの問題はconf.set("textinputformat.record.delimiter", ".");
と関連しています。さて、これは文章間の区切りをどのように定義するかです。このため、出力ファイルの文章が空白で始まることがあります(例:This is my first sentence.
)。代わりに私はこれをこのように設定しようとしていますconf.set("textinputformat.record.delimiter", ". ");
(完全な停止の後にスペースを入れます)が、このようにJavaアプリケーションはすべての文章を出力ファイルに書き出しません。