hadoop实现grep示例分享
时间:2023-12-24 05:31:04
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获取参数
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();
//传递参数
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;
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java判断用户名和密码是否符合要求过程详解
C# 获取当前年份的周期及周期所在日期范围(推荐)
![](https://img.aspxhome.com/file/2023/1/66131_0s.png)
Android模块化中数据传递/路由跳转实现示例
Java实现求数组最长子序列算法示例
Android 开发之Dialog,Toast,Snackbar提醒
![](https://img.aspxhome.com/file/2023/9/103889_0s.png)
SpringBoot2使用Jetty容器操作(替换默认Tomcat)
![](https://img.aspxhome.com/file/2023/9/59779_0s.jpg)
详解Spring MVC 集成EHCache缓存
![](https://img.aspxhome.com/file/2023/3/77933_0s.png)
解决Maven中关于依赖导入不进的问题
![](https://img.aspxhome.com/file/2023/0/60260_0s.jpg)
java解析json数组方式
![](https://img.aspxhome.com/file/2023/8/58188_0s.png)
Android ViewModel创建不受横竖屏切换影响原理详解
![](https://img.aspxhome.com/file/2023/7/137467_0s.png)
利用Java计算某个日期是星期几
C#泛型类创建与使用的方法
![](https://img.aspxhome.com/file/2023/3/96433_0s.jpg)
Spring Boot 实现图片上传并回显功能
![](https://img.aspxhome.com/file/2023/6/68776_0s.png)
在spring中实例化bean无效的问题
android开发教程之自定义控件checkbox的样式示例
C#中的值传递和引用传递详细解析
C# Winform 分页功能的实现
![](https://img.aspxhome.com/file/2023/1/69241_0s.png)