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实现文件倒排索引的更多相关文章

  1. (Unity)Unity自定义Debug日志文件,利用VS生成Dll文件并使用Dotfuscated进展混淆,避免被反编译

    Unity自定义Debug日志文件,利用VS生成Dll文件并使用Dotfuscated进行混淆,避免被反编译. 1.打开VS,博主所用版本是Visual Studio 2013. 2.新建一个VC项目 ...

  2. Delphi 7使用自定义图标关联文件类型

    Delphi 7使用自定义图标关联文件类型 5.2 Delphi编程(40)  版权声明:本文为博主原创文章,未经博主允许不得转载. 在开发过程中,我们经常需要属于自己的文件类型,自定义的后缀名不仅可 ...

  3. Java自定义日志输出文件

    Java自定义日志输出文件 日志的打印,在程序中是必不可少的,如果需要将不同的日志打印到不同的地方,则需要定义不同的Appender,然后定义每一个Appender的日志级别.打印形式和日志的输出路径 ...

  4. 在SSIS中使用自定义的DLL文件

    原文:在SSIS中使用自定义的DLL文件 步骤1.开发dll(需要签名) using System;using System.Collections.Generic;using System.Text ...

  5. 转载 jQuery和js自定义函数和文件的方法(全网最全)

    jQuery和js自定义函数和文件的方法(全网最全)    版权声明:本文为像雾像雨又像风_http://blog.csdn.net/topdandan的原创文章,未经允许不得转载. https:// ...

  6. Struts2学习:struts.xml引入自定义的xml文件

    随着项目代码的增多,用一个struts.xml来管理所有功能模块的Action未免显得臃肿且结构不清晰,因此可以根据实际的功能划分,将各模块的Action放在自定义的xml文件中,再引入struts. ...

  7. gSoap使用入门(2)----自定义接口头文件

    摘自:http://blog.csdn.net/zhuzhihai1988/article/details/8131556 接口头文件的格式在向导中没有看到明确的说明性的内容,但通过看开发包中示例程序 ...

  8. Springboot读取自定义的yml文件中的List对象

    Yml文件(novellist.xml)如下: novellist:   list:     - name: 笑傲江湖       type: 武侠       master: 令狐冲       a ...

  9. Django day30 自定义配置settings文件,分页器,版本控制

    一:自定义配置settings文件 1.有两套配置文件,默认配置,用户的配置 2.如果某个字段,用户配置了,就用用户的,如果没配置,就用默认的 二:分页器 1.三种分页: # 普通分页 from re ...

随机推荐

  1. 手机端 ios 浏览器访问报错

    原因: ios 隐私模式下 会限制 localstorage 和 sessionstorage 的使用 测试的时候最好看下当前浏览器所处的模式.代码里也要判断 当前是否隐私模式 .告知用户切换模式才能 ...

  2. Postman 测试Xfire webservice

    权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/u013177381/article/det ...

  3. springMvc中自定义bean转换接收前台传的参数

    转载:https://blog.csdn.net/u013476435/article/details/81538099 因前端整体传参时,参数名都不是驼峰写法,类似 music_name,music ...

  4. 线程局部存储tls的使用

    线程局部存储(Thread Local Storage,TLS)主要用于在多线程中,存储和维护一些线程相关的数据,存储的数据会被关联到当前线程中去,并不需要锁来维护.. 因此也没有多线程间资源竞争问题 ...

  5. selenium python 报错“ unable to find binary in default location”

    selenium python 报错如下: raise exception_class(message, screen, stacktrace)selenium.common.exceptions.W ...

  6. HTML5基本标签<搬运>

    HTML语言基本标签: 创建一个HTML文档<html></html> 设置文档标题以及其他不在WEB网页上显示的信息<head></head> 设置文 ...

  7. stl(优先队列操作)

    http://codeforces.com/gym/101911/problem/C Recently Monocarp has created his own mini-laboratory! Th ...

  8. MongoDB数据库-基础篇

    一使用mongodb 1.常用的命令 show dbs    显示数据库列表 use dbname    进入dbname数据库,大小写敏感,没有这个数据库也不要紧 show collections ...

  9. Java加密与解密的艺术 读书心得

    现在项目中加密与解密的方式很多,很早就想整理一下Java中加密与解密的方式,读完<<Java加密与解密的艺术>>一书.借此机会梳理一下这方面的知识点 一.基础普及 安全技术目标 ...

  10. nginx日志切割脚本shell

    nginx-log-rotate.sh: #!/bin/bash#---------------------------------------------# Comment:Used for rot ...