package org.apache.hadoop.examples;
import java.util.Random;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.mapred.lib.*;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
/* 对输入文件按正则表达式查找,把结果写到输出文件上。
查找用到了RegexMapper,LongSumReducer,InverseMapper系统自带的
工具类。这个源代码运行了两个job,一个查找,一个是排序。
Extracts matching regexs from input files and counts them. */
public class Grep extends Configured implements Tool {
private Grep() {} // 单例模式singleton
public int run(String[] args) throws Exception {
if (args.length < 3) {
System.out.println("Grep <inDir> <outDir> <regex> [<group>]");
ToolRunner.printGenericCommandUsage(System.out);
return -1;
}
Path tempDir =
new Path("grep-temp-"+
Integer.toString(new Random().nextInt(Integer.MAX_VALUE)));
JobConf grepJob = new JobConf(getConf(), Grep.class);
try {
grepJob.setJobName("grep-search");
FileInputFormat.setInputPaths(grepJob, args[0]);
grepJob.setMapperClass(RegexMapper.class);//设置系统自带的mapper类来查找
grepJob.set("mapred.mapper.regex", args[2]);
if (args.length == 4)
grepJob.set("mapred.mapper.regex.group", args[3]);
grepJob.setCombinerClass(LongSumReducer.class);//设置系统自带的reducer来做合并
grepJob.setReducerClass(LongSumReducer.class);
//设置系统自带的reducer。
FileOutputFormat.setOutputPath(grepJob, tempDir);
grepJob.setOutputFormat(SequenceFileOutputFormat.class);//设置输出格式是二进制文件
grepJob.setOutputKeyClass(Text.class);//输出的key是Text类型
grepJob.setOutputValueClass(LongWritable.class);//输出的value是long类型
JobClient.runJob(grepJob);
JobConf sortJob = new JobConf(Grep.class);
sortJob.setJobName("grep-sort");
FileInputFormat.setInputPaths(sortJob, tempDir);
sortJob.setInputFormat(SequenceFileInputFormat.class);//设置输入的文件格式二进制文件
sortJob.setMapperClass(InverseMapper.class);//设置自带的排序mapper
sortJob.setNumReduceTasks(1); // write a single file
FileOutputFormat.setOutputPath(sortJob, new Path(args[1]));
sortJob.setOutputKeyComparatorClass // 输出结果是降序排列sort by decreasing freq
(LongWritable.DecreasingComparator.class);
JobClient.runJob(sortJob);
}
finally {
FileSystem.get(grepJob).delete(tempDir, true);
}
return 0;
}
public static void main(String[] args) throws Exception {
int res = ToolRunner.run(new Configuration(), new Grep(), args);
System.exit(res);
}
}
分享到:
相关推荐
gg是grep的Go语言源代码它将搜索限制为指定的Go令牌类,例如标识符,程序包名称,数字,注释,关键字等。 由于gg知道要搜索的内容,因此可以进行智能匹配。 例如: 通过值而不是正则表达式搜索数字:使用“ gg v 255...
可通过从源代码进行编译(请参见下文)或下载来使用MetaEuk。 它要求至少具有SSE4.1指令集(通过在Linux上执行cat /proc/cpuinfo | grep sse4_1或在sysctl -a | grep machdep.cpu.features | grep SSE4.1上进行检查...
10-3-5 版本控制支持和别名 10-3-6 使用ModelMaker生成自动化(Intrumentation)代码 10-4 导入源代码 10-4-1 背景知识 10-4-2 从源代码中更新已存在的类 10-5 源文件注释 10-5-1 概 述 10-5-2 生成源文件注释 10-5-...
Github用于文档和源代码控制。 在所有服务器上运行Linux Ubuntu(Amazon内部版本),用于通过命令dpkg安装的DEB软件包 安全通讯的 在服务器上安装软件包。 所有服务器上的器将日志条目流定向到收集器。 JMeter...
源代码及结果: 真实用户ID和有效用户ID的区别: 真实用户ID:这个ID就是我们登陆unix系统时的身份ID。 有效用户ID:定义了操作者的权限。有效用户ID是进程的属性,决定了该进程对文件的访问权限。 阅读如下程序,...
如图2所示,首先输入你想查找的内容,然后在选项中可以选择一下内容:是否大小写敏感、是否忽略注释内容、是否整字模式、是否搜索DFM文件、是否使用表达式进行搜索(表达式搜索是Grep内含的一项强大的功能);...
不工作的东西在源代码中用“NOTWORKING”注释标记,以便更容易地搜索;-) cd wc-interop grep -lr ' NOTWORKING ' src/ * 你为什么这样做,唯一的? 这是为我的 jQuery UK 2015 演讲而构建的。
◆ 文件处理命令:file、mkdir、grep、dd、find、mv、ls、diff、cat、ln; ◆ 系统管理相关命令:df、top、free、quota、at、lp、adduser、groupadd、kill、crontab; ◆ 网络操作命令:ifconfig、ip、ping、...
在许多人的无私奉献下以及这种语言本身的源代码自由性质,它演变成为一种特点丰富的语言,而且现在还在成长中。 PHP虽然很容易学习,但是速度上比mod_perl(植入web服务器的perl模块)慢。现在有了可以与mod_perl...
有的用户登录后就长时间空闲,有可能导致安全上的问题,通过打开 /etc/profile 中 TMOUT 注释,将在设置的时间到达后自动logout用户 例如: export TMOUT=120 那么, 用户两分钟没有击键,将自动logout //AIX系统中如何...
在目录的所有文件中查找源字串 10-10. 列出目录中所有的符号连接文件 10-11. 将目录中的符号连接文件名保存到一个文件中 10-12. 一个C风格的for循环 10-13. 在batch mode中使用efax 10-14. 简单的while循环 10-15. ...
在目录的所有文件中查找源字串 10-10. 列出目录中所有的符号连接文件 10-11. 将目录中的符号连接文件名保存到一个文件中 10-12. 一个C 风格的for 循环 10-13. 在batch mode 中使用efax 10-14. 简单的while 循环 10-...
Linux 是在 1991 年发展起来的与 UNIX兼容的操作系统,可以免费使用,它的源代码 可以自由传播且可任人修改、充实、发展,开发者的初衷是要共同创造一个完美、理想并 可以免费使用的操作系统。 我们并不能使同学...
了解 U-Boot-1.3.1 的代码结构,掌握其移植方法。 【实验环境】 1、Ubuntu 7.0.4发行版 2、u-boot-1.3.1 3、FS2410平台 4、交叉编译器 arm-softfloat-linux-gnu-gcc-3.4.5 【实验步骤】 一、建立自己的平台...
查看函数和过程的源代码 SQL>select text from all_source where owner=user and name=upper('&plsql_name'); 三、查看数据库的SQL 1、查看表空间的名称及大小 select t.tablespace_name, round(sum(bytes/...