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. mysql_DML_select_聚合join

    聚合函数: select avg(salary)//平均值 from wsb; select sum(salary)//总和 from wsb; select max(salary)//最大 from ...

  2. 由length、length()、size()引发的挖掘

    在java中可能会经常用到这几个属性或者方法, 那么今天就来仔细研究一下 length——数组的属性: length()——String的方法: size()——集合的方法: 1.数组.length属 ...

  3. Dapper(一) 简介和性能

    Dapper的简介 Dapper是.NET下一个micro的ORM,它和Entity Framework或Nhibnate不同,属于轻量级的,并且是半自动的.Dapper只有一个代码文件,完全开源,你 ...

  4. Vue过滤器:全局过滤器

    Vue.js 允许你自定义过滤器,可被用于一些常见的文本格式化. 过滤器可以用在两个地方:双花括号插值和 v-bind 表达式 (后者从 2.1.0+ 开始支持). 过滤器应该被添加在 JavaScr ...

  5. 如何根据字典值的大小,对字典中的项排序---Python数据结构与算法相关问题与解决技巧

    实际案例: 某班英语成绩以字典形式存储为: { 'LiLei' : 90, 'Jim' : 88, 'Lucy': 92 } 如何根据成绩高低,计算学生排名 -- 根据分数,进行排名,并且把排名信息添 ...

  6. Java thread(1)

    这一部分主要讨论 java多线程的基本相关概念以及两种java线程的实现方式: 线程与进程: 这个操作系统书上介绍得很详细,这里就列出一些比较主要的: 线程: 线程本身有很少的资源,因为所拥有的资源较 ...

  7. mysql内存数据淘汰机制和大查询会不会把内存打爆?

    首先我们说一下大查询会不会把内存打爆? 比如说主机内存有5g,但是我们一个大查询的数据有10g,这样会不会把内存打爆呢? 答案:不会 为什么? 因为mysql读取数据是采取边读边发的策略 select ...

  8. java创建线程的两种方式及源码解析

    创建线程的方式有很多种,下面我们就最基本的两种方式进行说明.主要先介绍使用方式,再从源码角度进行解析. 继承Thread类的方式 实现Runnable接口的方式 这两种方式是最基本的创建线程的方式,其 ...

  9. mybatis学习笔记2

    1.获得插入语句执行之后的自增主键 <insert id="insertUser" parameterType="com.mybatis.po.User" ...

  10. 禁止html复制文本

    <body class="content" oncontextmenu="return false" onselectstart="return ...