برنامج grep بسيط تم إنشاؤه بواسطة hadoop، والذي يمكنه استخراج أسطر تحتوي على سلاسل معينة من المستندات
انسخ رمز الكود كما يلي:
/*
* برنامج grep بسيط يمكنه استخراج الأسطر التي تحتوي على سلاسل معينة من المستندات
*/
الطبقة العامة grep تمتد أداة التنفيذ التي تم تكوينها {
الطبقة العامة الثابتة grepMap تمتد Mapper<LongWritable, Text, Text,NullWritable>{
خريطة فارغة عامة (سطر طويل قابل للكتابة، قيمة نصية، سياق السياق) تطرح IOException، InterruptedException {
// احصل على المعلمات من خلال التكوين
String str = context.getConfiguration().get("grep");
إذا (value.toString().contains(str)){
context.write(value, NullWritable.get());
}
}
}
@تجاوز
تشغيل int العام (String[] args) يطرح استثناء {
إذا (args.length!=3){
System.out.println("خطأ");
System.exit(1);
}
تكوين التكوين = getConf();
// تمرير المعلمات
التكوين.set("grep", args[2]);
Job job = new 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 fileSystem = out.getFileSystem(configuration);
إذا (fileSystem.exists (خارج))
fileSystem.delete(out, true);
FileInputFormat.addInputPath(job, in);
FileOutputFormat.setOutputPath(job, out);
System.exit(job.waitForCompletion(true)?0:1);
العودة 0؛
}