倒排索引

/**
*
*
* <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. hadoop启动name失败

    namenode失败十分的常见, 1.java.io.EOFException; Host Details : local host is: "hadoop1/192.168.41.134& ...

  2. JS--我发现,原来你是这样的JS:面向对象编程OOP[3]--(JS继承)

    一.面向对象编程(继承) 这篇博客是面向对象编程的第三篇,JS继承.继承顾名思义,就是获取父辈的各种"财产"(属性和方法). 怎么实现继承? 我们的JavaScript比较特别了, ...

  3. tornado之子模板

    #!/usr/bin/env python26 #-*- coding:utf8 -*- import tornado.httpserver import tornado.ioloop import ...

  4. 【Java入门提高篇】Day2 接口

    上一篇讲完了抽象类,这一篇主要讲解比抽象类更加抽象的内容--接口. 什么是接口呢?先来看个栗子: /** * @author Frank * @create 2017/11/22 * @descrip ...

  5. JavaScript学习笔记(十)——高阶函数之map,reduce,filter,sort

    在学习廖雪峰前辈的JavaScript教程中,遇到了一些需要注意的点,因此作为学习笔记列出来,提醒自己注意! 如果大家有需要,欢迎访问前辈的博客https://www.liaoxuefeng.com/ ...

  6. Struts2-052验证脚本

    下面分享一下Struts2-052验证的python脚本 #-*- coding:utf-8 -*- import requests url_list_path ="/home/d0ll4r ...

  7. ERP中文档权限设置:只能浏览不能下载?如何实现

    文档中心的文件夹授权只能是对岗位或者用户授权(这个跟我们的[[url=]用户及权限[/url]]下面的授权方式还不太一样)比如:要将文档中心的文件夹[公司文档]授权给用户A和用户B 授权逻辑: 软件界 ...

  8. Python案例分享

    1.过桥(爬金字塔): 1 i = 1 2 while i <= 9: 3 if i < 6: 4 j = 0 5 while j < i: 6 print('*',end=' ') ...

  9. swaggerui在asp.net web api core 中的应用

    Swaggerui 可以为我们的webapi提供美观的在线文档,如下图: 实现步骤: NuGet Packages  Install-Package Swashbuckle.AspNetCore 在s ...

  10. 查看Page结构

    SQL Server存储数据的基本单元是Page,每一个Page的大小是8KB,数据文件是由Page构成的.在同一个数据库上,每一个Page都有一个唯一的资源标识,标识符由三部分组成:db_id,fi ...