文書から特定の文字列を含む行を抽出できるHadoop製のシンプルなgrepプログラム
次のようにコードをコピーします。
/*
* 文書から特定の文字列を含む行を抽出できるシンプルな grep プログラム
*/
public class grep extends 構成された実装ツール{
public static class grepMap extends Mapper<LongWritable, Text, Text,NullWritable>{
public void map(LongWritable line,Text value,Context context) throws IOException, InterruptedException{
//設定を通じてパラメータを取得します
文字列 str = context.getConfiguration().get("grep");
if(value.toString().contains(str)){
context.write(値, NullWritable.get());
}
}
}
@オーバーライド
public int run(String[] args) は例外をスローします {
if(args.length!=3){
System.out.println("エラー");
System.exit(1);
}
構成設定 = getConf();
//パラメータを渡す
configuration.set("grep", args[2]);
ジョブ job = 新しいジョブ(configuration,"grep");
job.setJarByClass(grep.class);
job.setMapperClass(grepMap.class);
job.setNumReduceTasks(0);
job.setMapOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
パスの = 新しいパス(args[0]);
パス出力 = 新しいパス(args[1]);
ファイルシステム fileSystem = out.getFileSystem(configuration);
if(fileSystem.exists(out))
fileSystem.delete(out, true);
FileInputFormat.addInputPath(job, in);
FileOutputFormat.setOutputPath(job, out);
System.exit(job.waitForCompletion(true)?0:1);
0を返します。
}