hadoop實現(xiàn)grep示例分享
hadoop做的一個簡單grep程序,可從文檔中提取包含某些字符串的行
/*
* 一個簡單grep程序,可從文檔中提取包含莫些字符串的行
*/
public class grep extends Configured implements Tool{
public static class grepMap extends Mapper<LongWritable, Text, Text,NullWritable>{
public void map(LongWritable line,Text value,Context context) throws IOException, InterruptedException{
//通過Configuration獲取參數(shù)
String str = context.getConfiguration().get("grep");
if(value.toString().contains(str)){
context.write(value, NullWritable.get());
}
}
}
@Override
public int run(String[] args) throws Exception {
if(args.length!=3){
System.out.println("ERROR");
System.exit(1);
}
Configuration configuration = getConf();
//傳遞參數(shù)
configuration.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);
Path in = new Path(args[0]);
Path out = new Path(args[1]);
FileSystem 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);
return 0;
}
相關文章
SpringBoot+slf4j線程池全鏈路調用日志跟蹤問題及解決思路(二)
本文主要給大家介紹如何實現(xiàn)子線程中的traceId日志跟蹤,本文通過封裝Callable為例給大家介紹的非常詳細,需要的朋友一起看看吧2021-05-05
Mybatis分頁插件PageHelper的配置和簡單使用方法(推薦)
在使用Java Spring開發(fā)的時候,Mybatis算是對數(shù)據(jù)庫操作的利器了。這篇文章主要介紹了Mybatis分頁插件PageHelper的配置和使用方法,需要的朋友可以參考下2017-12-12
spring boot中使用RabbitMQ routing路由詳解
本篇文章主要介紹了spring boot中使用RabbitMQ routing路由詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
SpringMVC自定義攔截器登錄檢測功能的實現(xiàn)代碼
這篇文章主要介紹了SpringMVC自定義攔截器登錄檢測功能的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08

