倒排索引

/**
*
*
* <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. 使用GetThumbnailImage进行图片缩放操作

    /// <summary>        /// 获取等比例缩放图片的方法        /// </summary>        /// <param name=&q ...

  2. RabbitMQ和SpringBoot的简单整合列子

    一 思路总结 1 主要用spring-boot-starter-amqp来整合RabbitMQ和SpringBoot 2 使用spring-boot-starter-test来进行单元测试 3编写配置 ...

  3. Java 中实现方法重试的一种机制

    有这样一个需求,当调用某个方法抛出异常,比如通过 HttpClient 调用远程接口时由于网络原因报 TimeOut 异常:或者所请求的接口返回类似于"处理中"这样的信息,需要重复 ...

  4. 封装一个通过class获取元素的方法--我的JS原生库(1)

    function getByClass(oParent,sClass){ var aEle = oParent.getElementsByTagName('*'); var result = []; ...

  5. MyBatis《2》

    MyBatis入参考文档:http://mybatis.org/mybatis-3/zh/   1.properties 属性 1.在MyBatis配置文件中引用属性文件     MyBatis允许在 ...

  6. GItCandy版本库搬迁步骤

    1.编译GitCandy,源码地址http://git.newlifex.com/NewLife/GitCandy 2.发布网站到文件夹 3.git网站停止运行 4.拷贝发布的网站到服务器目录&quo ...

  7. opencv摄像头捕获图像

    #include <iostream> #include <opencv2/opencv.hpp> using namespace cv; using namespace st ...

  8. HTML 使用jQuery选中复选框 简易版

    <html><head>   <meta charset="utf-8">   <script src="jquery-1.7. ...

  9. selenium定位tr及td,并获取其文本及属性

    #获取所有的trtrlist=brower.find_elements_by_tag_name("tr")for tr in trlist: #获取tr中的所有td tdlist= ...

  10. 理解OC“属性”这一概念

    1.定义一个属性,编译器会自动编写相关的存取方法和实例变量,如果不想使用系统默认的实例变量,可以使用@synthesize(合成)语法来指实例变量的名字,如果不想编译器自动合成存取方法,则可以自己实现 ...