自定义combiner实现文件倒排索引
package com.zuoyan.hadoop; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
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; /**
*
* @author root
* 1:输入文件中并没有地址的输入,那么我们需要在mapper端读取数据的时候,插入其地址。
* 按“”空格分割字符串,mapper的输出 <key,value>=<值 地址,1>或者<值 地址,(1,1)>
* 2:利用mapper和reducer之间一个极其重要的组件combiner进行首次的处理,
* 并且分离key中的值与地址,此时的输出结果<key,value>=<值,地址 1>或者<值,地址 2>
* 注意:此组件是属于mapper端阶段的。
* 3:reducer开始进行最后的处理。
*/
public class CombinerTest { // main
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(CombinerTest.class);
//1
job.setMapperClass(LastSearchMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
//2
job.setCombinerClass(LastSearchComb.class);
//3
job.setReducerClass(LastSearchReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
boolean x = job.waitForCompletion(true);
System.out.println(x);
} // mapper
public class LastSearchMapper extends Mapper<LongWritable, Text, Text, Text> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String words[] = line.split(" ");
InputSplit input = context.getInputSplit();
String pathname = ((FileSplit) input).getPath().getName();// 得到此时数据的地址
for (String word : words) {
String word1 = word + " " + pathname;
context.write(new Text(word1), new Text("1"));
}
}
} // combiner
public class LastSearchComb extends Reducer<Text, Text, Text, Text> {
@Override
protected void reduce(Text arg0, Iterable<Text> arg1, Context arg2) throws IOException, InterruptedException {
int sum = 0;
for (Text arg : arg1) {
String word = arg.toString();
int wordINT = Integer.parseInt(word);
sum = wordINT + sum;
}
String line = arg0.toString();
String word[] = line.split(" ");
arg2.write(new Text(word[0]), new Text(word[1] + ":" + sum));
}
} // reducer
public class LastSearchReducer extends Reducer<Text, Text, Text, Text> {
@Override
protected void reduce(Text arg0, Iterable<Text> arg1, Context arg2) throws IOException, InterruptedException {
String newword = new String();
for (Text word : arg1) {
String wordString = word.toString();
newword = newword + wordString + " ";
}
arg2.write(arg0, new Text(newword));
}
} }
pom导入hadoop-Client即可
自定义combiner实现文件倒排索引的更多相关文章
- (Unity)Unity自定义Debug日志文件,利用VS生成Dll文件并使用Dotfuscated进展混淆,避免被反编译
Unity自定义Debug日志文件,利用VS生成Dll文件并使用Dotfuscated进行混淆,避免被反编译. 1.打开VS,博主所用版本是Visual Studio 2013. 2.新建一个VC项目 ...
- Delphi 7使用自定义图标关联文件类型
Delphi 7使用自定义图标关联文件类型 5.2 Delphi编程(40) 版权声明:本文为博主原创文章,未经博主允许不得转载. 在开发过程中,我们经常需要属于自己的文件类型,自定义的后缀名不仅可 ...
- Java自定义日志输出文件
Java自定义日志输出文件 日志的打印,在程序中是必不可少的,如果需要将不同的日志打印到不同的地方,则需要定义不同的Appender,然后定义每一个Appender的日志级别.打印形式和日志的输出路径 ...
- 在SSIS中使用自定义的DLL文件
原文:在SSIS中使用自定义的DLL文件 步骤1.开发dll(需要签名) using System;using System.Collections.Generic;using System.Text ...
- 转载 jQuery和js自定义函数和文件的方法(全网最全)
jQuery和js自定义函数和文件的方法(全网最全) 版权声明:本文为像雾像雨又像风_http://blog.csdn.net/topdandan的原创文章,未经允许不得转载. https:// ...
- Struts2学习:struts.xml引入自定义的xml文件
随着项目代码的增多,用一个struts.xml来管理所有功能模块的Action未免显得臃肿且结构不清晰,因此可以根据实际的功能划分,将各模块的Action放在自定义的xml文件中,再引入struts. ...
- gSoap使用入门(2)----自定义接口头文件
摘自:http://blog.csdn.net/zhuzhihai1988/article/details/8131556 接口头文件的格式在向导中没有看到明确的说明性的内容,但通过看开发包中示例程序 ...
- Springboot读取自定义的yml文件中的List对象
Yml文件(novellist.xml)如下: novellist: list: - name: 笑傲江湖 type: 武侠 master: 令狐冲 a ...
- Django day30 自定义配置settings文件,分页器,版本控制
一:自定义配置settings文件 1.有两套配置文件,默认配置,用户的配置 2.如果某个字段,用户配置了,就用用户的,如果没配置,就用默认的 二:分页器 1.三种分页: # 普通分页 from re ...
随机推荐
- noi.ac #227 random
分析 我们发现实际只要计算a[i]>b[j]和a[i]<b[j]哪种多即可 代码 #include<bits/stdc++.h> using namespace std; ], ...
- git subtree模块化代码管理
Git Subtree 的原理 首先,你有两个伟大的项目——我们叫他P1项目.P2项目,还有一个牛逼的要被多个项目共用的项目——我们叫他S项目.我们通过简要讲解使用Subtree来同步代码的过程来解释 ...
- Linux实用技巧--隧道
平时开发过程中,可能会遇到一些网络问题,比如npm install 一些依赖包.本地电脑是可以,没有问题.但是测试环境服务器,由于公司内部网络安全限制,不可以随意访问外部网络.因此下载一个依赖包就变得 ...
- 004-URL编码转换函数:escape()、encodeURI()、encodeURIComponent()
一.概述 函数出现时间: escape() javascript 1.0 ...
- vue项目运行时出现的问题(less、vue poackages version)
今天运行项目,项目一直好好的却突然运行时报错,如下: 是引入文件报错问题,回头查看了一下文件在main.js的引入: import '@/assets/styles/custom.less'; 文件引 ...
- MSSQL sql常用判断语句
.判断数据库是否存在 if exists (select * from sys.databases where name = '数据库名') drop database [数据库名] 2 判断 ...
- 本站CSS代码
body { /*字体样式*/ font-family: "youyuan",幼圆,"MicrosoftJhengHei",华文细黑,STHeiti,MingL ...
- linux下rpm包安装、配置和卸载mysq
l WIN10下虚拟机:VMware workstation 12 PRO 安装 # 1.查看系统版本 [root@vm-xiluhua][/home/xiluhua]$ cat /etc/red ...
- Filter 和Listener
Filter 和Listener Filter : 过滤器 1.概念 浏览器发出请求访问服务器的资源,过滤器将请求拦截,完成一些特殊的功能. 作用:一般用于完成通过的操作.如:登陆验证.统一编码处理. ...
- Java语言的特点与工作原理
Java语言的特点 1.简单性 Java语言与我们常听到的C++语言很像,但是没有C++那么繁琐.因为Java就是在C++之上设计出来的,设计者把C++的一些特性去掉了,这些特性在实际开发中,程序员也 ...