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;
}
相關(guān)文章
SpringBoot如何配置獲取request中body的json格式參數(shù)
這篇文章主要介紹了SpringBoot如何配置獲取request中body的json格式參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06Java事務(wù)管理學(xué)習(xí)之Spring和Hibernate詳解
這篇文章主要給大家介紹了Java事務(wù)管理學(xué)習(xí)之Spring和Hibernate的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友們可以參考借鑒,下面來一起看看吧。2017-03-03Springboot自動配置與@Configuration配置類詳解
這篇文章主要介紹了SpringBoot中的@Configuration與自動配置,在進行項目編寫前,我們還需要知道一個東西,就是SpringBoot對我們的SpringMVC還做了哪些配置,包括如何擴展,如何定制,只有把這些都搞清楚了,我們在之后使用才會更加得心應(yīng)手2022-07-07springboot項目中mapper.xml文件找不到的三種解決方案
這篇文章主要介紹了springboot項目中mapper.xml文件找不到的三種解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01Java Swing中的表格(JTable)和樹(JTree)組件使用實例
這篇文章主要介紹了Java Swing中的表格(JTable)和樹(JTree)組件使用實例,本文同時講解了表格和樹的基本概念、常用方法、代碼實例,需要的朋友可以參考下2014-10-10Classloader隔離技術(shù)在業(yè)務(wù)監(jiān)控中的應(yīng)用詳解
這篇文章主要為大家介紹了Classloader隔離技術(shù)在業(yè)務(wù)監(jiān)控中的應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08