需求:

以上三个文件,用MapReduce进行处理,最终输出以下格式:

hello c.txt-->2 b.txt-->2 a.txt-->3
jerry c.txt-->1 b.txt-->3 a.txt-->1
tom c.txt-->1 b.txt-->1 a.txt-->2

思考:

我们需要进行两个步骤:

1.就是之前的统计单词个数的练习,只不过现在需要加上文件名而已。得到如下效果

hello-->a.txt 3
hello-->b.txt 2
hello-->c.txt 2
jerry-->a.txt 1
jerry-->b.txt 3
jerry-->c.txt 1
tom-->a.txt 2
tom-->b.txt 1
tom-->c.txt 1

2.将key由hello-->a.txt这种形式转化成hello这种形式,然后进行分组。得到如下效果:

hello c.txt-->2 b.txt-->2 a.txt-->3
jerry c.txt-->1 b.txt-->3 a.txt-->1
tom c.txt-->1 b.txt-->1 a.txt-->2

文件目录如下:

InverseIndexStepOne.java:

package cn.darrenchan.hadoop.mr.ii;

import java.io.IOException;

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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class InverseIndexStepOne {
public static class StepOneMapper extends
Mapper<LongWritable, Text, Text, LongWritable> {
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
// 拿到一行数据
String line = value.toString();
// 切分出各个单词
String[] fields = line.split("\t");
// 获取这一行数据所在的文件切片
FileSplit inputSplit = (FileSplit) context.getInputSplit();
// 从文件切片中获取文件名
String fileName = inputSplit.getPath().getName();
for (String field : fields) {
// 封装kv输出 , k : hello-->a.txt v: 1
context.write(new Text(field + "-->" + fileName),
new LongWritable(1));
}
}
} public static class StepOneReducer extends
Reducer<Text, LongWritable, Text, LongWritable> {
@Override
protected void reduce(Text key, Iterable<LongWritable> values,
Context context) throws IOException, InterruptedException {
int count = 0;
for (LongWritable value : values) {
count += value.get();
}
// <hello-->a.txt,{1,1,1....}>
context.write(key, new LongWritable(count));
}
} public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf); job.setJarByClass(InverseIndexStepOne.class); job.setMapperClass(StepOneMapper.class);
job.setReducerClass(StepOneReducer.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class); //检查一下参数所指定的输出路径是否存在,如果已存在,先删除
Path outputPath = new Path(args[1]);
FileSystem fileSystem = FileSystem.get(conf);
if (fileSystem.exists(outputPath)) {
fileSystem.delete(outputPath, true);
} FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, outputPath); System.exit(job.waitForCompletion(true) ? 0 : 1);
} }

InverseIndexStepTwo.java:

package cn.darrenchan.hadoop.mr.ii;

import java.io.IOException;

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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class InverseIndexStepTwo {
// k: 行起始偏移量 v: {hello-->a.txt 3}
// map输出的结果是这个形式 : <hello,a.txt-->3>
public static class StepTwoMapper extends
Mapper<LongWritable, Text, Text, Text> {
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
String[] fields = line.split("-->");
String[] strings = fields[1].split("\t");
context.write(new Text(fields[0]), new Text(strings[0] + "-->"
+ strings[1]));
}
} // 拿到的数据 <hello,{a.txt-->3,b.txt-->2,c.txt-->1}>
// 输出的结果就是 k: hello v: a.txt-->3 b.txt-->2 c.txt-->1
public static class StepTwoReducer extends Reducer<Text, Text, Text, Text> {
@Override
protected void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
String result = " ";
for (Text value : values) {
result += value + " ";
}
context.write(key, new Text(result));
}
} public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf); job.setJarByClass(InverseIndexStepTwo.class); job.setMapperClass(StepTwoMapper.class);
job.setReducerClass(StepTwoReducer.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); // 检查一下参数所指定的输出路径是否存在,如果已存在,先删除
Path outputPath = new Path(args[1]);
FileSystem fileSystem = FileSystem.get(conf);
if (fileSystem.exists(outputPath)) {
fileSystem.delete(outputPath, true);
} FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, outputPath); System.exit(job.waitForCompletion(true) ? 0 : 1);
} }

首先将三个文件传到HDFS的/ii/srcdata目录下面,执行指令:

hadoop jar ii.jar cn.darrenchan.hadoop.mr.ii.InverseIndexStepOne /ii/srcdata /ii/output1

打印运行信息:

17/03/01 17:55:38 INFO client.RMProxy: Connecting to ResourceManager at weekend110/192.168.230.134:8032
17/03/01 17:55:38 WARN mapreduce.JobSubmitter: Hadoop command-line option parsing not performed. Implement the Tool interface and execute your application with ToolRunner to remedy this.
17/03/01 17:55:39 INFO input.FileInputFormat: Total input paths to process : 3
17/03/01 17:55:39 INFO mapreduce.JobSubmitter: number of splits:3
17/03/01 17:55:40 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1488372977056_0001
17/03/01 17:55:41 INFO impl.YarnClientImpl: Submitted application application_1488372977056_0001
17/03/01 17:55:41 INFO mapreduce.Job: The url to track the job: http://weekend110:8088/proxy/application_1488372977056_0001/
17/03/01 17:55:41 INFO mapreduce.Job: Running job: job_1488372977056_0001
17/03/01 17:55:52 INFO mapreduce.Job: Job job_1488372977056_0001 running in uber mode : false
17/03/01 17:55:52 INFO mapreduce.Job: map 0% reduce 0%
17/03/01 17:56:11 INFO mapreduce.Job: map 33% reduce 0%
17/03/01 17:56:12 INFO mapreduce.Job: map 100% reduce 0%
17/03/01 17:56:18 INFO mapreduce.Job: map 100% reduce 100%
17/03/01 17:56:18 INFO mapreduce.Job: Job job_1488372977056_0001 completed successfully
17/03/01 17:56:18 INFO mapreduce.Job: Counters: 49
File System Counters
FILE: Number of bytes read=382
FILE: Number of bytes written=372665
FILE: Number of read operations=0
FILE: Number of large read operations=0
FILE: Number of write operations=0
HDFS: Number of bytes read=402
HDFS: Number of bytes written=138
HDFS: Number of read operations=12
HDFS: Number of large read operations=0
HDFS: Number of write operations=2
Job Counters
Launched map tasks=3
Launched reduce tasks=1
Data-local map tasks=3
Total time spent by all maps in occupied slots (ms)=51196
Total time spent by all reduces in occupied slots (ms)=3018
Total time spent by all map tasks (ms)=51196
Total time spent by all reduce tasks (ms)=3018
Total vcore-seconds taken by all map tasks=51196
Total vcore-seconds taken by all reduce tasks=3018
Total megabyte-seconds taken by all map tasks=52424704
Total megabyte-seconds taken by all reduce tasks=3090432
Map-Reduce Framework
Map input records=8
Map output records=16
Map output bytes=344
Map output materialized bytes=394
Input split bytes=312
Combine input records=0
Combine output records=0
Reduce input groups=9
Reduce shuffle bytes=394
Reduce input records=16
Reduce output records=9
Spilled Records=32
Shuffled Maps =3
Failed Shuffles=0
Merged Map outputs=3
GC time elapsed (ms)=1077
CPU time spent (ms)=6740
Physical memory (bytes) snapshot=538701824
Virtual memory (bytes) snapshot=1450766336
Total committed heap usage (bytes)=379793408
Shuffle Errors
BAD_ID=0
CONNECTION=0
IO_ERROR=0
WRONG_LENGTH=0
WRONG_MAP=0
WRONG_REDUCE=0
File Input Format Counters
Bytes Read=90
File Output Format Counters
Bytes Written=138

运行结果如下:

hello-->a.txt 3
hello-->b.txt 2
hello-->c.txt 2
jerry-->a.txt 1
jerry-->b.txt 3
jerry-->c.txt 1
tom-->a.txt 2
tom-->b.txt 1
tom-->c.txt 1

执行指令:

hadoop jar ii.jar cn.darrenchan.hadoop.mr.ii.InverseIndexStepTwo /ii/output1 /ii/output2

打印运行信息:

17/03/01 18:03:31 INFO client.RMProxy: Connecting to ResourceManager at weekend110/192.168.230.134:8032
17/03/01 18:03:31 WARN mapreduce.JobSubmitter: Hadoop command-line option parsing not performed. Implement the Tool interface and execute your application with ToolRunner to remedy this.
17/03/01 18:03:31 INFO input.FileInputFormat: Total input paths to process : 1
17/03/01 18:03:31 INFO mapreduce.JobSubmitter: number of splits:1
17/03/01 18:03:32 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1488372977056_0003
17/03/01 18:03:32 INFO impl.YarnClientImpl: Submitted application application_1488372977056_0003
17/03/01 18:03:32 INFO mapreduce.Job: The url to track the job: http://weekend110:8088/proxy/application_1488372977056_0003/
17/03/01 18:03:32 INFO mapreduce.Job: Running job: job_1488372977056_0003
17/03/01 18:03:38 INFO mapreduce.Job: Job job_1488372977056_0003 running in uber mode : false
17/03/01 18:03:38 INFO mapreduce.Job: map 0% reduce 0%
17/03/01 18:03:43 INFO mapreduce.Job: map 100% reduce 0%
17/03/01 18:03:47 INFO mapreduce.Job: map 100% reduce 100%
17/03/01 18:03:48 INFO mapreduce.Job: Job job_1488372977056_0003 completed successfully
17/03/01 18:03:48 INFO mapreduce.Job: Counters: 49
File System Counters
FILE: Number of bytes read=162
FILE: Number of bytes written=185553
FILE: Number of read operations=0
FILE: Number of large read operations=0
FILE: Number of write operations=0
HDFS: Number of bytes read=249
HDFS: Number of bytes written=112
HDFS: Number of read operations=6
HDFS: Number of large read operations=0
HDFS: Number of write operations=2
Job Counters
Launched map tasks=1
Launched reduce tasks=1
Data-local map tasks=1
Total time spent by all maps in occupied slots (ms)=2605
Total time spent by all reduces in occupied slots (ms)=2725
Total time spent by all map tasks (ms)=2605
Total time spent by all reduce tasks (ms)=2725
Total vcore-seconds taken by all map tasks=2605
Total vcore-seconds taken by all reduce tasks=2725
Total megabyte-seconds taken by all map tasks=2667520
Total megabyte-seconds taken by all reduce tasks=2790400
Map-Reduce Framework
Map input records=9
Map output records=9
Map output bytes=138
Map output materialized bytes=162
Input split bytes=111
Combine input records=0
Combine output records=0
Reduce input groups=3
Reduce shuffle bytes=162
Reduce input records=9
Reduce output records=3
Spilled Records=18
Shuffled Maps =1
Failed Shuffles=0
Merged Map outputs=1
GC time elapsed (ms)=138
CPU time spent (ms)=820
Physical memory (bytes) snapshot=218480640
Virtual memory (bytes) snapshot=726454272
Total committed heap usage (bytes)=137433088
Shuffle Errors
BAD_ID=0
CONNECTION=0
IO_ERROR=0
WRONG_LENGTH=0
WRONG_MAP=0
WRONG_REDUCE=0
File Input Format Counters
Bytes Read=138
File Output Format Counters
Bytes Written=112

运行结果如下:

hello c.txt-->2 b.txt-->2 a.txt-->3
jerry c.txt-->1 b.txt-->3 a.txt-->1
tom c.txt-->1 b.txt-->1 a.txt-->2

MapReduce实战(四)倒排索引的实现的更多相关文章

  1. coreseek实战(四):php接口的使用,完善php脚本代码

    coreseek实战(四):php接口的使用,完善php脚本代码 在上一篇文章 coreseeek实战(三)中,已经能够正常搜索到结果,这篇文章主要是把 index.php 文件代码写得相对完整一点点 ...

  2. Python爬虫实战四之抓取淘宝MM照片

    原文:Python爬虫实战四之抓取淘宝MM照片其实还有好多,大家可以看 Python爬虫学习系列教程 福利啊福利,本次为大家带来的项目是抓取淘宝MM照片并保存起来,大家有没有很激动呢? 本篇目标 1. ...

  3. SpringSecurity权限管理系统实战—四、整合SpringSecurity(上)

    目录 SpringSecurity权限管理系统实战-一.项目简介和开发环境准备 SpringSecurity权限管理系统实战-二.日志.接口文档等实现 SpringSecurity权限管理系统实战-三 ...

  4. gRPC学习之四:实战四类服务方法

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  5. miniFTP项目实战四

    项目简介: 在Linux环境下用C语言开发的Vsftpd的简化版本,拥有部分Vsftpd功能和相同的FTP协议,系统的主要架构采用多进程模型,每当有一个新的客户连接到达,主进程就会派生出一个ftp服务 ...

  6. 恶意代码分析实战四:IDA Pro神器的使用

    目录 恶意代码分析实战四:IDA Pro神器的使用 实验: 题目1:利用IDA Pro分析dll的入口点并显示地址 空格切换文本视图: 带地址显示图形界面 题目2:IDA Pro导入表窗口 题目3:交 ...

  7. MapReduce实战--倒排索引

    本文地址:http://www.cnblogs.com/archimedes/p/mapreduce-inverted-index.html,转载请注明源地址. 1.倒排索引简介 倒排索引(Inver ...

  8. 《OD大数据实战》MapReduce实战

    一.github使用手册 1. 我也用github(2)——关联本地工程到github 2. Git错误non-fast-forward后的冲突解决 3. Git中从远程的分支获取最新的版本到本地 4 ...

  9. [置顶] MapReduce 编程之 倒排索引

    本文调试环境: ubuntu 10.04 , hadoop-1.0.2 hadoop装的是伪分布模式,就是只有一个节点,集namenode, datanode, jobtracker, tasktra ...

随机推荐

  1. 分享一个基于ligerui的系统应用案例ligerRM V2(权限管理系统)(提供下载)

    阅读目录 简介 系统特色 系统介绍 - 首页 系统介绍 - 列表页 系统介绍 - 明细页(表单) 系统介绍 - 菜单/按钮 系统介绍 - 权限中心 系统介绍 - 数据权限 系统介绍 - 字段权限 系统 ...

  2. Android之旅七 Service简介

    1.          Service是什么:它是一个应用程序组件.没有图形化界面.通常用来处理一些耗时比较长的操作(例如下载.播放MP3等等).可以使用Service更新ContentProvide ...

  3. centos6.8服务器配置之vsftpd配置

    vsftpd: version 2.2.2一.安装:因对版本要求不高,所以采用yum安装 yum install -y vsftpdckconfig vsftpd on 二.配置: 1.建立ftp用户 ...

  4. (转)IntelliJ IDEA下的使用git

    1.git简介 Git是目前流行的分布式版本管理系统.它拥有两套版本库,本地库和远程库,在不进行合并和删除之类的操作时这两套版本库互不影响.也因此其近乎所有的操作都是本地执行,所以在断网的情况下任然可 ...

  5. Echarts 获取后台数据 使用后台数据展示 饼装图

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head& ...

  6. java 合并排序算法、冒泡排序算法、选择排序算法、插入排序算法、快速排序算法的描述

    算法是在有限步骤内求解某一问题所使用的一组定义明确的规则.通俗点说,就是计算机解题的过程.在这个过程中,无论是形成解题思路还是编写程序,都是在实施某种算法.前者是推理实现的算法,后者是操作实现的算法. ...

  7. qq邮箱、qq空间点击后以word方式打开解决办法

    解决办法: Internet--工具--Internet选项--程序--设为默认值

  8. 算法笔记_165:算法提高 道路和航路(Java)

    目录 1 问题描述 2解决方案   1 问题描述 问题描述 农夫约翰正在针对一个新区域的牛奶配送合同进行研究.他打算分发牛奶到T个城镇(标号为1..T),这些城镇通过R条标号为(1..R)的道路和P条 ...

  9. python——关于Python Profilers性能分析器

    1. 介绍性能分析器 profiler是一个程序,用来描述运行时的程序性能,并且从不同方面提供统计数据加以表述.Python中含有3个模块提供这样的功能,分别是cProfile, profile和ps ...

  10. webDriver API——第8部分Utilities

    The Utils methods. selenium.webdriver.common.utils.free_port() Determines a free port using sockets. ...