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

代码实现:

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. NYOJ 85:有趣的数(打表,规律)

    85-有趣的数 内存限制:64MB 时间限制:3000ms 特判: No 通过数:8 提交数:12 难度:2 题目描述: 把分数按下面的办法排成一个数表. 1/1 1/2 1/3 1/4- 2/1 2 ...

  2. TP thinkphp 权限管理 权限认证 功能

    (如有打扰,请忽略)阿里云ECS大羊群,2U4G低至1.4折,限实名新用户,需要的点吧https://promotion.aliyun.com/ntms/act/vm/aliyun-group/tea ...

  3. 2017.4.28 KVM 内存虚拟化及其实现

    概述 KVM(Kernel Virtual Machine) , 作为开源的内核虚拟机,越来越受到 IBM,Redhat,HP,Intel 等各大公司的大力支持,基于 KVM 的开源虚拟化生态系统也日 ...

  4. .closest 样例收集

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. fastdfs(https://www.jianshu.com/p/1c71ae024e5e)

    参考 官方网站:https://github.com/happyfish100/ 配置文档:https://github.com/happyfish100/fastdfs/wiki/ 参考资料:htt ...

  6. MySQL--批量KILL连接

    ============================================== 使用SELECT INTO OUTFILE方式获取到要删除的连接ID并保存为文件,在通过SOURCE执行 ...

  7. airflow-operator 可以管理airflow 的kuberntes 自定义controller && crd

    使用airflow-operator 我们可以让airflow 可以很方便的运行在k8s集群环境中,当前还在开发中 主要分为两部分:airflowbbase && airfowclus ...

  8. 填充整个区间(fill,fill_n,generate和generate_n)

    fill 将value值填充整个区间,不能为OutputIterator,因为fill会用到first和last,outputIterator无法做相等的测试 template <class F ...

  9. Windows git 初始设置

    主要布署在 Linux 服务器上时,将全局设置 为提交自动转为 LF,签出不转换.git config --global core.autocrlf input(无效了,按默认即可) 设置全局用户名.

  10. day7大纲

    01 昨日内容回顾 数据类型补充: str <---> list split join list <---> set set(list) list(set()) list &l ...