Mapreduce代码疑点(1)
一、Hadoop MultipleInputs.addInputPath 读取多个路径
https://blog.csdn.net/t1dmzks/article/details/76473905
MultipleInputs.addInputPath
作用
可以指定多个输入路径,每个路径都可以指定相应的map方法
使用方法
MultipleInputs.addInputPath
(Job job, Path path, Class<? extends InputFormat> inputFormatClass, Class<? extends Mapper> mapperClass)
举例
使用wordcount来举例
F:\hadooptest\wordcount\input1下有个word.txt,单词用空格分割
aa bb cc dd ee ff aa bb ff
F:\hadooptest\wordcount\input2下有个word.txt。单词用 ## 分割
aa##bb##cc
ee##gg##kk
代码
package com.myhadoop.multiple; import com.myhadoop.mapreduce.test.WordCount;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; import java.io.IOException;
import java.util.StringTokenizer; /**
* Created by kaishun on 2017/7/31.
*/
public class TestMultipleInputs {
public static class MapA extends Mapper<LongWritable, Text, Text, IntWritable>
{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException
{
String lines = value.toString();
String strs[] = lines.split("\\s+");
for (int i = 0; i <strs.length ; i++) {
word.set(strs[i]);
context.write(word, one);
}
}
} public static class MapB extends Mapper<LongWritable, Text, Text, IntWritable>
{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException
{
String lines = value.toString();
String strs[] = lines.split("##");
for (int i = 0; i <strs.length ; i++) {
word.set(strs[i]);
context.write(word, one);
}
}
} public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable>
{
public void reduce(Text key,Iterable<IntWritable> values,Context context) throws IOException,InterruptedException
{
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
context.write(key, new IntWritable(sum));
}
} public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJobName("MultipleWordCount");
job.setJarByClass(WordCount.class);
//多个输入,分别对应不同的map
MultipleInputs.addInputPath(job,new Path("F:\\hadooptest\\wordcount\\input1"),TextInputFormat.class,WordCount.MapA.class);
MultipleInputs.addInputPath(job,new Path("F:\\hadooptest\\wordcount\\input2"),TextInputFormat.class,WordCount.MapB.class); job.setNumReduceTasks(1);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
//分到一个reduce
job.setReducerClass(WordCount.Reduce.class); FileOutputFormat.setOutputPath(job, new Path(args[0]));
System.exit(job.waitForCompletion(true) ? 0 : 1); }
}
输出
aa 3
bb 3
cc 2
dd 1
ee 2
ff 2
gg 1
kk 1
二、hadoop中的job.setOutputKeyClass与job.setMapOutputKeyClass
mr程序中一般都会有hadoop中的job.setOutputKeyClass(theClass)与job.setOutputValueClass(theClass),
但是有的程序处理以上两个外还有job.setMapOutputKeyClass(theClass)与job.setMapOu
tputValueClass(Text.class),一直没弄懂是怎么回事,网上查了下,原来当mapper与reducer
的输出类型一致时可以用 job.setOutputKeyClass(theClass)与job.setOutputValueClass
(theClass)这两个进行配置就行,但是当mapper用于reducer两个的输出类型不一致的时候就需
要分别进行配置了。
Mapreduce代码疑点(1)的更多相关文章
- Centos下命令行编译MapReduce代码(Java)并打包在Hadoop中执行
前提条件:搭建好Hadoop系统 新建文件夹:input 和 output hdfs dfs -mkdir /inputhdfs dfs -mkdir /output 查看文件系统 hdfs df ...
- mapreduce代码实现入门
mapreduce代码主要包括三个类,map类.reduce类以及测试类! 以wordcount为例, map类为: static class WordMapper extends Mapper< ...
- 【甘道夫】官方网站MapReduce代码注释具体实例
引言 1.本文不描写叙述MapReduce入门知识,这类知识网上非常多.请自行查阅 2.本文的实例代码来自官网 http://hadoop.apache.org/docs/current/hadoop ...
- 吴裕雄--天生自然HADOOP操作实验学习笔记:mapreduce代码编程
实验目的 深入了解mapreduce的底层 了解IDEA的使用 学会通过本地和集群环境提交程序 实验原理 1.回忆mapreduce模型 前面进行了很多基础工作,本次实验是使用mapreduce的AP ...
- [大牛翻译系列]Hadoop(15)MapReduce 性能调优:优化MapReduce的用户JAVA代码
6.4.5 优化MapReduce用户JAVA代码 MapReduce执行代码的方式和普通JAVA应用不同.这是由于MapReduce框架为了能够高效地处理海量数据,需要成百万次调用map和reduc ...
- 使用mapreduce计算环比的实例
最近做了一个小的mapreduce程序,主要目的是计算环比值最高的前5名,本来打算使用spark计算,可是本人目前spark还只是简单看了下,因此就先改用mapreduce计算了,今天和大家分享下这个 ...
- Hadoop学习笔记(2) 关于MapReduce
1. 查找历年最高的温度. MapReduce任务过程被分为两个处理阶段:map阶段和reduce阶段.每个阶段都以键/值对作为输入和输出,并由程序员选择它们的类型.程序员还需具体定义两个函数:map ...
- 用Map-Reduce的思维处理数据
在很多人的眼里,Map-Reduce等于Hadoop,没有Hadoop谈Map-Reduce犹如自上谈兵,实则不然,Map-Reduce是一种计算模型,只是非常适合在并行的环境下运行,Hadoop是M ...
- [翻译]MapReduce: Simplified Data Processing on Large Clusters
MapReduce: Simplified Data Processing on Large Clusters MapReduce:面向大型集群的简化数据处理 摘要 MapReduce既是一种编程模型 ...
随机推荐
- HDOJ 5383 Yu-Gi-Oh! 最大费用最大流
网络流裸题: 分两部分建图,求不要求满流的最大费用最大流..... Yu-Gi-Oh! Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- struct结构体在c和c++中的差别
非常多次遇到这个struct的问题,今天在这里简单总结一下我的理解 一.struct在C 中的使用 1.单独使用struct定义结构体类型 struct Student { int id; int n ...
- 安装 openCV 2.4.10
近期试验了一下 ubuntu 12.06 (x86) 安装.openCV 安装脚本 最好的文章是 https://help.ubuntu.com/community/OpenCV. 它提供一个脚本( ...
- LeetCode 976. Largest Perimeter Triangle (三角形的最大周长)
题目标签:Array 题目给了我们一个 边长的 array, 让我们找出 最大边长和的三角形,当然前提得是这三条边能组成三角形.如果array 里得边长组成不了三角形,返回0. 最直接的理解就是,找到 ...
- Luogu3674小清新人渣的本愿
https://zybuluo.com/ysner/note/1109536 题面 给你一个序列a,长度为n,有m次操作,每次询问一个区间 是否可以选出两个数它们的差为x 是否可以选出两个数它们的和为 ...
- IBatis异常: Cannot find class: VARCHAR
今天再项目里添加新功能时,突然爆出 org.springframework.beans.factory.BeanCreationException: Error creating bean with ...
- 确定比赛名次(toposort)
http://acm.hdu.edu.cn/showproblem.php?pid=1285 #include <stdio.h> #include <string.h> ; ...
- 题解报告:hdu 1285 确定比赛名次
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 Problem Description 有N个比赛队(1<=N<=500),编号依次 ...
- 【Leetcode 3】Longest Substring Without Repeating Characters0
Description: Given a string, find the length of the longest substring without repeating characters. ...
- Android:用签名打包后微信分享失效
刚开始使用微信分享,申请的微信appid也可以在直接使用,分享成功! 当我使用自己的签名打包分享时却分享失败,一闪而过,好郁闷的说,为什么之前没有打包就可以,签名打包后就不可以了... 开始查找各种资 ...