当前位置: 移动技术网 > IT编程>开发语言>Java > hadoop实现grep示例分享

hadoop实现grep示例分享

2019年07月22日  | 移动技术网IT编程  | 我要评论
hadoop做的一个简单grep程序,可从文档中提取包含某些字符串的行 复制代码 代码如下:/* * 一个简单grep程序,可从文档中提取包含莫些字符串的行&n

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

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网