倒排索引

/**
*
*
* <pre>
*file1.txt:
*hello ketty
*hello tomcat
*
*file2.txt:
*hello hadoop
*
*map1:
*hello:file1.txt 1
*hello:file1.txt 1
*ketty:file1.txt 1
*tomcat:file1.txt 1
*hello:file2.txt 1
*hadoop:file2.txt 1
*
*reduce1:
*hello:file1.txt 2
*ketty:file1.txt 1
*tomcat:file1.txt 1
*hello:file2.txt 1
*hadoop:file2.txt 1
*
*reduce2:
*hello file1.txt 2,file2.txt 1
*ketty file1.txt 1
*tomcat file1.txt 1
*hadoop file2.txt 1
*</pre>
* @author huqiao
*/
public class InvertedIndex { /**
* input:files to be inverted index<br/>
* output: someword:filename count
* @author huqiao
*/
static class WordInFileCountMapper extends Mapper<LongWritable,Text,Text,LongWritable>{ @Override
protected void map(LongWritable key, Text value,Context ctx)
throws IOException, InterruptedException {
String line = value.toString();
String[] words = line.split(" "); FileSplit fileSplit = (FileSplit)ctx.getInputSplit();
String fileName = fileSplit.getPath().getName();
for(String word : words) {
ctx.write(new Text(word + ":" + fileName), new LongWritable(1));
}
} } /**
* output:
* <pre>
*hello:file1.txt 2
*ketty:file1.txt 1
*tomcat:file1.txt 1
*hello:file2.txt 1
*hadoop:file2.txt 1
*</pre>
* @author huqiao
*/
static class WordInFileCountReducer extends Reducer<Text,LongWritable,Text,LongWritable>{ @Override
protected void reduce(Text key, Iterable<LongWritable> values, Context ctx) throws IOException, InterruptedException {
int total = 0;
for(LongWritable value : values) {
total += value.get();
}
ctx.write(key, new LongWritable(total));
} } /**
* output:
* <pre>
* hello-->WordCountRecord{fileName:file1.txt,count:2}
* ...
* </pre>
* @author huqiao
*/
static class InvertedIndexMapper extends Mapper<LongWritable,Text,Text,WordCountRecord>{ @Override
protected void map(LongWritable key, Text value,Context ctx)
throws IOException, InterruptedException {
String line = value.toString();
String[] lineArray = line.split("\t");
String[] wordAndFileName = lineArray[0].split(":");
String word = wordAndFileName[0];
String fileName = wordAndFileName[1];
Long count = Long.parseLong(lineArray[1]); ctx.write(new Text(word), new WordCountRecord(fileName, count)); } } /**
* output:
* <pre>
* hello-->file1.txt 2,file2.txt 1
* ...
* </pre>
* @author huqiao
*/
static class InvertedIndexReducer extends Reducer<Text,WordCountRecord,Text,Text>{ @Override
protected void reduce(Text key, Iterable<WordCountRecord> values, Context ctx) throws IOException, InterruptedException {
StringBuffer output = new StringBuffer();
for(WordCountRecord value : values) {
output.append(value.getFileName() + " " + value.getCount()+",");
}
ctx.write(key, new Text(output.toString()));
} } public static void main(String[] args) throws Exception{ String inputPath = args[0];
String outputPath = args[1];
String phase = args[2]; FileSystem fs = FileSystem.get(new URI("hdfs://vcentos1:9000"),new Configuration(),"root"); //delete output path when it existed
Path output = new Path(outputPath);
if(fs.exists(output)) {
fs.delete(output,true);
} if("phase1".equals(phase)) {
doPhase1(inputPath,outputPath);
}else {
doPhase2(inputPath,outputPath);
} } private static void doPhase1(String inputPath,String outputPath)throws Exception {
Job job = Job.getInstance(); job.setJarByClass(InvertedIndex.class); job.setMapperClass(WordInFileCountMapper.class);
job.setReducerClass(WordInFileCountReducer.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class); FileInputFormat.setInputPaths(job, new Path(inputPath));
FileOutputFormat.setOutputPath(job, new Path(outputPath)); boolean success = job.waitForCompletion(true); System.exit(success ? 0 : 1);
} private static void doPhase2(String inputPath,String outputPath)throws Exception {
Job job = Job.getInstance(); job.setJarByClass(InvertedIndex.class); job.setMapperClass(InvertedIndexMapper.class);
job.setReducerClass(InvertedIndexReducer.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(WordCountRecord.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); FileInputFormat.setInputPaths(job, new Path(inputPath));
FileOutputFormat.setOutputPath(job, new Path(outputPath)); boolean success = job.waitForCompletion(true); System.exit(success ? 0 : 1);
} }

执行时分两个阶段:

 hadoop jar mr.jar me.huqiao.hadoop.demo_code.invertedsort.InvertedIndex /invertedindex/input /invertedindex/phase-a-output/ phase1

然后以第一个阶段的输出作为第二个阶段的输入:

hadoop jar mr.jar me.huqiao.hadoop.demo_code.invertedsort.InvertedIndex /invertedindex/phase-a-output /invertedindex/phase-b-output/ phase2

最终效果类似于:

about   logs.txt ,
are text.txt ,
hadoop file1.txt ,
hdfs file1.txt ,
hello text.txt ,logs.txt ,file1.txt ,
how logs.txt ,text.txt ,
kitty logs.txt ,
today logs.txt ,
tom text.txt ,
you text.txt ,

找出价格最贵的商品

共同QQ好友

大数据学习(6)MapReduce应用的更多相关文章

  1. 大数据学习系列之四 ----- Hadoop+Hive环境搭建图文详解(单机)

    引言 在大数据学习系列之一 ----- Hadoop环境搭建(单机) 成功的搭建了Hadoop的环境,在大数据学习系列之二 ----- HBase环境搭建(单机)成功搭建了HBase的环境以及相关使用 ...

  2. 大数据学习系列之五 ----- Hive整合HBase图文详解

    引言 在上一篇 大数据学习系列之四 ----- Hadoop+Hive环境搭建图文详解(单机) 和之前的大数据学习系列之二 ----- HBase环境搭建(单机) 中成功搭建了Hive和HBase的环 ...

  3. 大数据学习系列之六 ----- Hadoop+Spark环境搭建

    引言 在上一篇中 大数据学习系列之五 ----- Hive整合HBase图文详解 : http://www.panchengming.com/2017/12/18/pancm62/ 中使用Hive整合 ...

  4. 大数据学习系列之七 ----- Hadoop+Spark+Zookeeper+HBase+Hive集群搭建 图文详解

    引言 在之前的大数据学习系列中,搭建了Hadoop+Spark+HBase+Hive 环境以及一些测试.其实要说的话,我开始学习大数据的时候,搭建的就是集群,并不是单机模式和伪分布式.至于为什么先写单 ...

  5. 大数据学习系列之九---- Hive整合Spark和HBase以及相关测试

    前言 在之前的大数据学习系列之七 ----- Hadoop+Spark+Zookeeper+HBase+Hive集群搭建 中介绍了集群的环境搭建,但是在使用hive进行数据查询的时候会非常的慢,因为h ...

  6. 大数据学习系列之—HBASE

    hadoop生态系统 zookeeper负责协调 hbase必须依赖zookeeper flume 日志工具 sqoop 负责 hdfs dbms 数据转换 数据到关系型数据库转换 大数据学习群119 ...

  7. 大数据学习之Hadoop快速入门

    1.Hadoop生态概况 Hadoop是一个由Apache基金会所开发的分布式系统集成架构,用户可以在不了解分布式底层细节情况下,开发分布式程序,充分利用集群的威力来进行高速运算与存储,具有可靠.高效 ...

  8. 大数据学习(一) | 初识 Hadoop

    作者: seriouszyx 首发地址:https://seriouszyx.top/ 代码均可在 Github 上找到(求Star) 最近想要了解一些前沿技术,不能一门心思眼中只有 web,因为我目 ...

  9. 大数据学习路线,来qun里分享干货,

    一.Linux lucene: 全文检索引擎的架构 solr: 基于lucene的全文搜索服务器,实现了可配置.可扩展并对查询性能进行了优化,并且提供了一个完善的功能管理界面. 推荐一个大数据学习群 ...

  10. 大数据篇:MapReduce

    MapReduce MapReduce是什么? MapReduce源自于Google发表于2004年12月的MapReduce论文,是面向大数据并行处理的计算模型.框架和平台,而Hadoop MapR ...

随机推荐

  1. ajax事件请求

    首先,ajax是什么? ajax是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. ajax是一种用于创建的快速动态网页的技术. 当async:true时,表示异步执行ajax代码:当as ...

  2. C#2.0中使用yield关键字简化枚举器的实现

    我们知道要使用foreach语句从客户端代码中调用迭代器,必需实现IEnumerable接口来公开枚举器,IEnumerable是用来公开枚举器的,它并不实现枚举器,要实现枚举器必需实现IEnumer ...

  3. 预加载(图片,css ,js)

    图片预加载 new Image().src = 'http://img1.t.sinajs.cn/t35/skin/skin_008/skin.css'; //新浪(4) 非ie下预加载(js,css ...

  4. python学习笔记 函数

    形式: def function(a,b,c=0,*args,**kw)#a,b必选参数,*args可变参数,**kw关键字参数 1.函数的返回值可以是多个参数.多个参数时,实际上返回的是一个tupl ...

  5. bootstrap_table_class表格样式实例

    <div class="container"> <h2>表格</h2> <!--table标签级别的样式说明如下: ----.table ...

  6. 2715:谁拿了最多奖学金-poj

    总时间限制:  1000ms 内存限制:  65536kB 描述 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同: 1) 院士奖学金,每人8000元,期末平均成 ...

  7. azure上连续部署web

    连续部署web   连续部署web,可以在第一次部署完web应用后,方便修改和自动提交代码部署新版本的web应用.其中自动提交使用github中的webhook,使代码在master上提交修改后可以自 ...

  8. Java语句语法

    一.流程控制语句 1.分支语句 1)if else 语法:(写法1) if(条件1){     //写一次 满足条件1执行在代码 }else if(条件2){    //写0或n次 不满足条件1,满足 ...

  9. 【ASP.NET Core】运行原理之启动WebHost

    ASP.NET Core运行原理之启动WebHost 本节将分析WebHost.CreateDefaultBuilder(args).UseStartup<Startup>().Build ...

  10. 《Linux命令行与shell脚本编程大全》第十九章 初识sed和gawk

    这两个工具能够极大简化需要进行的数据处理任务. 19.1 文本处理 能轻松实现自动格式化.插入.修改或删除文本元素的简单命令行编辑. sed和gawk就具备上述功能 19.1.1 sed编辑器 被称为 ...