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;
 }

标签:java,hadoop,grep
0
投稿

猜你喜欢

  • Java判断用户名和密码是否符合要求过程详解

    2023-09-24 08:08:36
  • C# 获取当前年份的周期及周期所在日期范围(推荐)

    2021-10-06 15:00:44
  • Android模块化中数据传递/路由跳转实现示例

    2023-06-29 03:12:06
  • Java实现求数组最长子序列算法示例

    2023-09-28 12:35:14
  • Android 开发之Dialog,Toast,Snackbar提醒

    2021-05-31 14:27:20
  • SpringBoot2使用Jetty容器操作(替换默认Tomcat)

    2023-11-24 01:17:15
  • 详解Spring MVC 集成EHCache缓存

    2022-05-28 04:42:52
  • 解决Maven中关于依赖导入不进的问题

    2023-09-05 23:13:08
  • java解析json数组方式

    2023-08-10 17:22:14
  • Android ViewModel创建不受横竖屏切换影响原理详解

    2023-08-31 21:21:20
  • 利用Java计算某个日期是星期几

    2023-11-17 05:49:42
  • C#泛型类创建与使用的方法

    2023-02-28 21:26:36
  • Spring Boot 实现图片上传并回显功能

    2021-10-11 17:45:20
  • 在spring中实例化bean无效的问题

    2022-03-16 17:55:34
  • android开发教程之自定义控件checkbox的样式示例

    2023-10-23 13:33:55
  • C#中的值传递和引用传递详细解析

    2022-03-20 20:11:02
  • C# Winform 分页功能的实现

    2023-03-29 06:07:10
  • Spring实战之@Autowire注解用法详解

    2021-11-17 20:37:19
  • winform调用javascript的小例子

    2022-05-21 21:17:26
  • Java二维数组查找功能代码实现

    2023-01-04 19:47:17
  • asp之家 软件编程 m.aspxhome.com