下面是四个文件及其内容。

代码实现:

Mapper:
package cn.tedu.invert;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit; public class InvertMapper extends Mapper<LongWritable, Text, Text, Text> { @Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
// 获取文件名
FileSplit fileSplit = (FileSplit)context.getInputSplit();
String pathName = fileSplit.getPath().getName(); // 将文件中的内容提取
String[] words = value.toString().split(" "); // 每一个单词都对应着自己所在文件的文件名
for(String word:words){
context.write(new Text(word), new Text(pathName));
}
}
}
Reducer:
package cn.tedu.invert;

import java.io.IOException;
import java.util.HashSet; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class InvertReducer extends Reducer<Text, Text, Text, Text> { public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { // 哈希表不存重复元素,将重复的文件名去掉
HashSet<String> set = new HashSet<>();
for (Text text : values) {
set.add(text.toString());
} StringBuilder sb = new StringBuilder();
for (String str : set) {
sb.append(str.toString()).append(" ");
} context.write(key, new Text(sb.toString()));
}
}

Driver:

package cn.tedu.invert;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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 InvertDriver { public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "JobName");
job.setJarByClass(cn.tedu.invert.InvertDriver.class);
job.setMapperClass(InvertMapper.class);
job.setReducerClass(InvertReducer.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); FileInputFormat.setInputPaths(job, new Path("hdfs://192.168.74.129:9000/text/invert"));
FileOutputFormat.setOutputPath(job, new Path("hdfs://192.168.74.129:9000/result/invert_result")); if (!job.waitForCompletion(true))
return;
}
}

结果:

hadoop MapReduce —— 输出每个单词所对应的文件的更多相关文章

  1. Hadoop MapReduce编程 API入门系列之小文件合并(二十九)

    不多说,直接上代码. Hadoop 自身提供了几种机制来解决相关的问题,包括HAR,SequeueFile和CombineFileInputFormat. Hadoop 自身提供的几种小文件合并机制 ...

  2. Hadoop MapReduce编程 API入门系列之压缩和计数器(三十)

    不多说,直接上代码. Hadoop MapReduce编程 API入门系列之小文件合并(二十九) 生成的结果,作为输入源. 代码 package zhouls.bigdata.myMapReduce. ...

  3. hadoop拾遗(五)---- mapreduce 输出到多个文件 / 文件夹

    今天要把HBase中的部分数据转移到HDFS上,想根据时间戳来自动输出到以时间戳来命名的每个文件夹下.虽然以前也做过相似工作,但有些细节还是忘记了,所以这次写个随笔记录一下. package com. ...

  4. Hadoop MapReduce编程学习

    一直在搞spark,也没时间弄hadoop,不过Hadoop基本的编程我觉得我还是要会吧,看到一篇不错的文章,不过应该应用于hadoop2.0以前,因为代码中有  conf.set("map ...

  5. 使用Python实现Hadoop MapReduce程序

    转自:使用Python实现Hadoop MapReduce程序 英文原文:Writing an Hadoop MapReduce Program in Python 根据上面两篇文章,下面是我在自己的 ...

  6. Hadoop Mapreduce运行流程

    Mapreduce的运算过程为两个阶段: 第一个阶段的map task相互独立,完全并行: 第二个阶段的reduce task也是相互独立,但依赖于上一阶段所有map task并发实例的输出: 这些t ...

  7. hadoop mapreduce 基础实例一记词

    mapreduce实现一个简单的单词计数的功能. 一,准备工作:eclipse 安装hadoop 插件: 下载相关版本的hadoop-eclipse-plugin-2.2.0.jar到eclipse/ ...

  8. 三.hadoop mapreduce之WordCount例子

    目录: 目录见文章1 这个案列完成对单词的计数,重写map,与reduce方法,完成对mapreduce的理解. Mapreduce初析 Mapreduce是一个计算框架,既然是做计算的框架,那么表现 ...

  9. MapReduce编程:单词去重

    编程实现单词去重要用到NullWritable类型. NullWritable: NullWritable 是一种特殊的Writable 类型,由于它的序列化是零长度的,所以没有字节被写入流或从流中读 ...

随机推荐

  1. HDU 1069:Monkey and Banana(DP)

    Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  2. zzuli2226:神奇的薯条

    题目描述 小明拿了n元钱去买薯条,薯条小份3元,大份7元.现在小明想知道如果只买薯条,自己的钱是否可以刚好花完,请你设计一个程序帮他计算一下. 输入 第一行输入一个整数T,表示实例数量.(1<= ...

  3. [Codeforces Round #492 (Div. 1) ][B. Suit and Tie]

    http://codeforces.com/problemset/problem/995/B 题目大意:给一个长度为2*n的序列,分别有2个1,2,3,...n,相邻的位置可以进行交换,求使所有相同的 ...

  4. hdu4336 Card Collector 容斥原理

    In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, fo ...

  5. SPOJ NSUBSTR Substrings

    题意 dt { font-weight: bold; margin-top: 20px; padding-left: 35px; } dd { box-shadow: 3px 3px 6px #888 ...

  6. Oracle数据类型与.NET中的对应关系

    Oracle连接添加的引用不同,会存在数据类型不同以及其他一些差别,就工作中遇到的问题暂时总结下. 两种不同的添加引用语句: (1)System.Data.OracleClient; (2)Oracl ...

  7. TypeScript 之 书写.d.ts文件

    https://m.runoob.com/manual/gitbook/TypeScript/_book/doc/handbook/Writing%20Definition%20Files.html ...

  8. 几种always块的形态

    几种always块的形态 1.时钟沿触发与复位触发 2.使能触发 3.预设触发 4.时序寄存器与锁存触发 5.组合逻辑

  9. 在Win32程序中显示Dos调试窗口

    在很多程序中,都可以看到程序运行中,会有一个Dos窗口,实时显示一些运行信息,这里就告诉大家是如何实现的,我们做个简单的,其实对控制台的操作还有很多,有兴趣的可以去查资料. 用到的API函数如下: / ...

  10. WebService的几种验证方式

    转 http://www.cnblogs.com/yoshiki1895/archive/2009/06/03/1495440.html WebService的几种验证方式 1.1      WebS ...