hadoop實(shí)現(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+slf4j線程池全鏈路調(diào)用日志跟蹤問題及解決思路(二)
本文主要給大家介紹如何實(shí)現(xiàn)子線程中的traceId日志跟蹤,本文通過封裝Callable為例給大家介紹的非常詳細(xì),需要的朋友一起看看吧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
Java運(yùn)用設(shè)計(jì)模式中的建造者模式構(gòu)建項(xiàng)目的實(shí)例解析
這篇文章主要介紹了Java運(yùn)用設(shè)計(jì)模式中的建造者模式構(gòu)建項(xiàng)目的實(shí)例解析,建造者模式對外隱藏創(chuàng)建過程的產(chǎn)品,使用組合的方式,由指揮者來決定建造的流程,需要的朋友可以參考下2016-04-04
SpringMVC自定義攔截器登錄檢測功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了SpringMVC自定義攔截器登錄檢測功能的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08
淺談Spring Cloud下微服務(wù)權(quán)限方案
這篇文章主要介紹了淺談Spring Cloud下微服務(wù)權(quán)限方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06

