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的更多相关文章

  1. hadoop系列四:mapreduce的使用(二)

    转载请在页首明显处注明作者与出处 一:说明 此为大数据系列的一些博文,有空的话会陆续更新,包含大数据的一些内容,如hadoop,spark,storm,机器学习等. 当前使用的hadoop版本为2.6 ...

  2. MapReduce(四)

    MapReduce(四) 1.shuffle过程 2.map中setup,map,cleanup的作用. 一.shuffle过程 https://blog.csdn.net/techchan/arti ...

  3. mapreduce (五) MapReduce实现倒排索引 修改版 combiner是把同一个机器上的多个map的结果先聚合一次

    (总感觉上一篇的实现有问题)http://www.cnblogs.com/i80386/p/3444726.html combiner是把同一个机器上的多个map的结果先聚合一次现重新实现一个: 思路 ...

  4. mapreduce (二) MapReduce实现倒排索引(一) combiner是把同一个机器上的多个map的结果先聚合一次

    1 思路:0.txt MapReduce is simple1.txt MapReduce is powerfull is simple2.txt Hello MapReduce bye MapRed ...

  5. MapReduce:详解Shuffle(copy,sort,merge)过程(转)

    Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方.要想理解MapReduce, Shuffle是必须要了解的.我看过很多相关的资料,但每次看完都云里雾里的绕着,很难理清大致的逻辑, ...

  6. MapReduce中的Shuffle和Sort分析

    MapReduce 是现今一个非常流行的分布式计算框架,它被设计用于并行计算海量数据.第一个提出该技术框架的是Google 公司,而Google 的灵感则来自于函数式编程语言,如LISP,Scheme ...

  7. Hadoop : MapReduce中的Shuffle和Sort分析

    地址 MapReduce 是现今一个非常流行的分布式计算框架,它被设计用于并行计算海量数据.第一个提出该技术框架的是Google 公司,而Google 的灵感则来自于函数式编程语言,如LISP,Sch ...

  8. MapReduce(四) 典型编程场景(二)

    一.MapJoin-DistributedCache 应用 1.mapreduce join 介绍 在各种实际业务场景中,按照某个关键字对两份数据进行连接是非常常见的.如果两份数据 都比较小,那么可以 ...

  9. Linux管线命令 - cut,grep,sort,uniq,wc,tee,tr,col,join,paste,expand,split,xargs

    在每个管线后面接的第一个数据必定是『命令』喔!而且这个命令必须要能够接受 standard input 的数据才行,这样的命令才可以是为『管线命令』,例如 less, more, head, tail ...

随机推荐

  1. 给iphone模拟器添加照片

    http://blog.csdn.net/StudyRecord/archive/2011/04/06/6305271.aspx 由于模拟器上没有照相机,要向Photos应用程序添加照片,必须按照以下 ...

  2. Android ListView的背景和黑色边缘化的问题

    解决方法1:给listview加上android:scrollingCache=”false”属性 解决方法2:给listview加上android:cacheColorHint="#000 ...

  3. Python基础 1----Python语言基础和Python环境准备与安装

    引导语: 人们学习Python是因为他们爱这门语言,因为他们追寻美,如果我雇用一个Python程序员,那么他们的技术将非常的棒 1 Python是一门跨平台的语言,是一种面向对象的动态编程语言 发布时 ...

  4. 【ES6】Set和Map中的NaN

    在JavaScript中,有个有意思的的式子:NaN !== NaN.在Set中的元素的重复检查或者Map键的定位过程中,都是用的类似恒等的检查逻辑.该逻辑和恒等检查的主要区别就是:NaN等于自身.

  5. Java基础知识强化之集合框架笔记39:Set集合之HashSet存储字符串并遍历

    1. HashSet类的概述: (1)不保证set的迭代顺序 (2)特别是它不保证该顺序恒久不变 HashSet底层数据结构是哈希表,哈希表依赖于哈希值存储,通过哈希值来确定元素的位置,  而保证元素 ...

  6. 配置Nginx 1.8支持PHP 5.6

    启动PHP和Nginx 修改Nginx配置文件/usr/local/nginx/conf/nginx.conf server { listen ; server_name localhost; loc ...

  7. Python开发【第二十二篇】:Web框架之Django【进阶】

    Python开发[第二十二篇]:Web框架之Django[进阶]   猛击这里:http://www.cnblogs.com/wupeiqi/articles/5246483.html 博客园 首页 ...

  8. tj

    --统计set @collSql='select sum(case Ca_IssueType when 0 then 1 else 0 end) as IssueCount,sum(case when ...

  9. jQuery作用

    jquery是前端里面比较总要的,是很强大的一个选择器. 表单: 1.$(":input") 查找所有的input元素 2.$("text")    匹配所有的 ...

  10. C# - Sql数据类型的对应关系

    <Language From="SQL" To="C#"> <Type From="bigint" To="lo ...