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. linux 复制到远程服务器

    scp 文件路径 root@192.168.0.1:文件夹路径 会提示你输入远程服务器密码

  2. Oracle--索引视图序列等对象

    ---恢复内容开始--- 索引 与表类似,不仅需要在DD中保存索引的定义,还需要在表空间为它分配实际的存储空间. 将索引和对应的表分别存放在不同硬盘的不同表空间中能够提高查询的速度,因为Oracle能 ...

  3. jmeter之关联的使用(正则、json)

    部分接口的测试中,一个接口会依赖上一个接口的响应信息,但上一个接口的响应信息又不是固定不变的,这时候,需要提取上一个接口的响应信息,将二者每一次的信息关联起来 目录 1.应用场景 2.jmeter正则 ...

  4. sourcetree for mac 使用

    1.sourceTree clone 仓库 打开sourceTree, 点击 新仓库(1) -> 从url克隆(2), 如下图 如下图所示, 粘贴源url路径, 自动补全或者手动选择目标路径和名 ...

  5. Python入门习题10.河内塔(汉诺塔)问题

    例10 共n个圆盘,a,b,c三根柱子 #汉诺塔问题.py def Hanoi(n): #定义n阶汉诺塔问题移动次数函数 if n == 1: return 1 else: return 2*Hano ...

  6. Codeforces 1114B (贪心)

    题面 传送门 分析 答案很好看出,显然是选最大的m*k个数 那么如何构造方案呢 我们把最大的m*k个数的位置标记为1,其他标记为0 从左到右维护一个ptr,记录有标记的数的个数,如果当前有m个有标记的 ...

  7. redis 不可重入分布式锁(setNx()和getset()方法实现)

    通常如果在单机环境,使用synchronized或juc ReentrantLock 实现锁机制,但如果是分布式系统,则需要借助第三方工具实现,比如redis.zookeeper等.redis为单进程 ...

  8. 2018CCPC吉林赛区(重现赛)

    http://acm.hdu.edu.cn/contests/contest_show.php?cid=867 A题,直接分块,不知道正解是什么. #include<bits/stdc++.h& ...

  9. JavaScript中的反柯里化

    转载自:https://www.cnblogs.com/zztt/p/4152147.html 柯里化 柯里化又称部分求值,其含义是给函数分步传递参数,每次传递参数后部分应用参数,并返回一个更具体的函 ...

  10. ubuntu下安装c man文档

    http://www.mirrorservice.org/sites/sourceware.org/pub/gcc/libstdc%2b%2b/doxygen/ 下载 http://www.mirro ...