mapreduce (四) MapReduce实现Grep+sort
1.txt
dong xi cheng
xi dong cheng
wo ai beijing
tian an men
qiche
dong
dong
dong
2.txt
dong xi cheng
xi dong cheng
wo ai beijing
tian an men
qiche
dong
dong
dong import java.io.IOException;
import java.util.Random; import org.apache.hadoop.conf.Configuration;
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.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.map.InverseMapper;
import org.apache.hadoop.mapreduce.lib.map.RegexMapper;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;
import org.apache.hadoop.mapreduce.lib.reduce.LongSumReducer; public class IGrep { public static void main(String[] args) throws IOException,
ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration(); String dir_in = "hdfs://localhost:9000/input_grep";
String dir_out = "hdfs://localhost:9000/output_grep";
String reg = ".ng";//匹配三个字符的字符串,且以ng结尾。 conf.set(RegexMapper.PATTERN, reg);
conf.setInt(RegexMapper.GROUP, 0); Path in = new Path(dir_in);
Path tmp = new Path("grep-temp-"
+ Integer.toString(new Random().nextInt(Integer.MAX_VALUE)));
Path out = new Path(dir_out); try {
Job grepJob = new Job(conf, "grep-search"); grepJob.setJarByClass(IGrep.class); grepJob.setInputFormatClass(TextInputFormat.class);
grepJob.setMapperClass(RegexMapper.class);
grepJob.setCombinerClass(LongSumReducer.class);
grepJob.setPartitionerClass(HashPartitioner.class); grepJob.setMapOutputKeyClass(Text.class);
grepJob.setMapOutputValueClass(LongWritable.class);
FileInputFormat.addInputPath(grepJob, in); grepJob.setReducerClass(LongSumReducer.class);
// job.setNumReduceTasks(1);
grepJob.setOutputFormatClass(SequenceFileOutputFormat.class); grepJob.setOutputKeyClass(Text.class);
grepJob.setOutputValueClass(LongWritable.class);
FileOutputFormat.setOutputPath(grepJob, tmp); grepJob.waitForCompletion(true); Job sortJob = new Job(conf, "grep-sort"); sortJob.setJarByClass(IGrep.class); sortJob.setInputFormatClass(SequenceFileInputFormat.class);
sortJob.setMapperClass(InverseMapper.class);
FileInputFormat.addInputPath(sortJob, tmp); sortJob.setNumReduceTasks(1);【全局排序】
sortJob.setSortComparatorClass(LongWritable.DecreasingComparator.class);//逆序 FileOutputFormat.setOutputPath(sortJob, out); sortJob.waitForCompletion(true); } finally {
FileSystem.get(conf).delete(tmp, true);
}
}
} 输出结果:
10 ong
4 eng
2 ing
mapreduce (四) MapReduce实现Grep+sort的更多相关文章
- hadoop系列四:mapreduce的使用(二)
转载请在页首明显处注明作者与出处 一:说明 此为大数据系列的一些博文,有空的话会陆续更新,包含大数据的一些内容,如hadoop,spark,storm,机器学习等. 当前使用的hadoop版本为2.6 ...
- MapReduce(四)
MapReduce(四) 1.shuffle过程 2.map中setup,map,cleanup的作用. 一.shuffle过程 https://blog.csdn.net/techchan/arti ...
- mapreduce (五) MapReduce实现倒排索引 修改版 combiner是把同一个机器上的多个map的结果先聚合一次
(总感觉上一篇的实现有问题)http://www.cnblogs.com/i80386/p/3444726.html combiner是把同一个机器上的多个map的结果先聚合一次现重新实现一个: 思路 ...
- mapreduce (二) MapReduce实现倒排索引(一) combiner是把同一个机器上的多个map的结果先聚合一次
1 思路:0.txt MapReduce is simple1.txt MapReduce is powerfull is simple2.txt Hello MapReduce bye MapRed ...
- MapReduce:详解Shuffle(copy,sort,merge)过程(转)
Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方.要想理解MapReduce, Shuffle是必须要了解的.我看过很多相关的资料,但每次看完都云里雾里的绕着,很难理清大致的逻辑, ...
- MapReduce中的Shuffle和Sort分析
MapReduce 是现今一个非常流行的分布式计算框架,它被设计用于并行计算海量数据.第一个提出该技术框架的是Google 公司,而Google 的灵感则来自于函数式编程语言,如LISP,Scheme ...
- Hadoop : MapReduce中的Shuffle和Sort分析
地址 MapReduce 是现今一个非常流行的分布式计算框架,它被设计用于并行计算海量数据.第一个提出该技术框架的是Google 公司,而Google 的灵感则来自于函数式编程语言,如LISP,Sch ...
- MapReduce(四) 典型编程场景(二)
一.MapJoin-DistributedCache 应用 1.mapreduce join 介绍 在各种实际业务场景中,按照某个关键字对两份数据进行连接是非常常见的.如果两份数据 都比较小,那么可以 ...
- Linux管线命令 - cut,grep,sort,uniq,wc,tee,tr,col,join,paste,expand,split,xargs
在每个管线后面接的第一个数据必定是『命令』喔!而且这个命令必须要能够接受 standard input 的数据才行,这样的命令才可以是为『管线命令』,例如 less, more, head, tail ...
随机推荐
- 帮助招聘程序员的自动考试网站:Codility
Automated tests of programming skills. Assessment of software developers. Recruitment software. Codi ...
- Linux查看系统资源占用
Linux查看系统资源占用 在系统维护的过程中,随时可能有需要查看 CPU和内存的使用率,并根据相应信息分析系统状况的需求.本文介绍一下几种常见的Linux系统资源查看命令. 1.总体内存占用的查看 ...
- React Links
Tutorial: https://hulufei.gitbooks.io/react-tutorial/content/jsx-in-depth.html
- mssql 创建触发器
MS-SMS里创建触发器: 首先右击表内的触发器文件夹图标 然后输入触发器创建指令,一下案例:(添加创建时间) 创建: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFI ...
- 使用Spring简化JDBC操作数据库
Spring的开发初衷是为了减轻企业级开发的复杂度,其对数据库访问的支持亦如此,使用Spring访问数据库能带来以下好处: 1.1 简化代码 使用原生的JDBC访问数据库,一般总是要执行以下步 ...
- android EventBus 的使用
今天简单的介绍 一下啊 android EventBus 的使用 EventBus 在官方介绍中是订阅......什么的 一大堆 , 在我android 菜鸟眼里 就是用来代替android 广 ...
- OC中的SEL解析
OC中的SEL对象即selector对象,用来保存一个方法的地址.下面通过一个Demo来解析SEL的原理.创建一个Person类,Person.h中: #import <Foundation/F ...
- SpringMVC4+thymeleaf3的一个简单实例(篇三:页面参数获取)
本篇将通过示例介绍页面参数是如何传递到后台的.我们继续沿用之前搭好的程序结构,如果你不知道,请参照前两篇.为方便跳转页面,我们在首页以及zoolist.html页面都加上彼此地址的链接:首页: zoo ...
- 【转】WF4.0 (基础篇)
转自:http://www.cnblogs.com/foundation/category/215023.html 作者:WXWinter —— 兰竹菊梅★春夏秋冬☆ —— wxwinter@16 ...
- POJ刷题记录 (。・`ω´・)(Progress:6/50)
1743:前后作差可以转化成不可重叠最长公共字串问题,运用后缀数组解决(参考罗穗骞神犇的论文) #include <cstdio> #include <cstring> #in ...