MapReduce-多个输出(使用MultipleOutput,不指定reduce任务个数)
多个输出
FileOutputFormat及其子类产生的文件放在输出目录下。每个reduce一个文件并且文件由分区号命名:part-r-00000,part-r-00001,等等。有时可能需要对输出的文件名进行控制或让每个reducer输出多个文件。MapReduce为此提供了MultipleOutputFormat类。
案例:数据分割
按气象站来区分气象数据。这需要运行一个作业,作业的输出时每个气象站一个文件,此文件包含该气象站的所有数据记录。
一种方法是每个气象站对应一个reducer。为此,我们需要做两件事。第一,写一个partitioner,把同一个气象站的数据放到同一个分区。第二,把作业的reducer数设为气象站的个数。
这样做有两个缺点。
第一,需要在作业运行之前知道分区数和气象站的个数。
第二,让应用程序来严格限定分区数并不好,因为可能导致分区数少或分区不均。让很多reducer做少量工作不是一个高效的作业组织方法,比较好的办法是使用更少reducer做更多的事情,因为运行任务的额外开销减少了。分区不均的情况也是很难避免的。不同气象站的数据量差异很大:有些气象站是一年前刚投入使用的,有的则已经工作了好久。如有其中一些reduce任务运行时间远远超过另一些,作业执行时间将由他们决定,从而导致作业的运行时间超过预期。
代码如下
package com.zhen.mapreduce.multipleOutput; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
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.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; /**
* @author FengZhen
* @date 2018年8月25日
* hadoop jar MultipleOutput.jar com.zhen.mapreduce.multipleOutput.SimpleMoreReduce
*/
public class SimpleMoreReduce extends Configured implements Tool{ static class SimplePartitioner extends Partitioner<Text, Text>{ @Override
public int getPartition(Text key, Text value, int numPartitions) {
if (key.toString().equals("000001")) {
return 0;
}else if (key.toString().equals("000002")) {
return 1;
}else if (key.toString().equals("000003")) {
return 2;
}
return 0;
} } static class SimpleMapper extends Mapper<LongWritable, Text, Text, Text>{
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
// 000001`yiduishuju
String[] values = value.toString().split("`");
context.write(new Text(values[0]), new Text(values[1]));
}
} static class SimpleReducer extends Reducer<Text, Text, Text, Text>{
@Override
protected void reduce(Text key, Iterable<Text> value, Reducer<Text, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
for (Text text : value) {
context.write(key, text);
}
}
} public int run(String[] args) throws Exception {
Configuration configuration = new Configuration(); Job job = Job.getInstance(configuration);
job.setJobName("SimpleMoreReduce");
job.setJarByClass(SimpleMoreReduce.class); job.setNumReduceTasks(3); job.setMapperClass(SimpleMapper.class);
job.setReducerClass(SimpleReducer.class);
job.setPartitionerClass(SimplePartitioner.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); return job.waitForCompletion(true) ? 0 : 1;
} public static void main(String[] args) {
try {
String[] params = {"hdfs://fz/user/hdfs/MapReduce/data/multipleOutput/temperature", "hdfs://fz/user/hdfs/MapReduce/data/multipleOutput/output"};
int exitCode = ToolRunner.run(new SimpleMoreReduce(), params);
System.exit(exitCode);
} catch (Exception e) {
e.printStackTrace();
}
}
}
在以下两种特殊情况下,让应用程序来设定分区数是有好处的
0个reducer,这是一个很罕见的情况:没有分区,因为应用只需要map任务
1个reducer,可以很方便的运行若干个小作业,从而把以前作业的输出合并成单个文件。前提是数据量足够小,以便一个reducer能轻松处理。
最好让集群为作业决定分区数:集群的reducer任务槽越多,任务完后就越快。这就是默认HashPartitioner表现如此出色的原因,因为它处理的分区数不限,并且确保每个分区都有一个很好的键组合使分区更均匀。
如果使用HashPartitioner,每个分区就会包含多个气象站,因此,要实现每个气象站输出一个文件,必须安排一个reducer写多个文件,由此就有了MultipleOutput.
MultipleOutput
MultipleOutputFormat类可以将数据写到多个文件,这些文件的名称源于输出的键和值或者任意字符串。这允许每个reducer(或者只有map作业的mapper)创建多个文件。采用name-m-nnnnn形式的文件名用于map输出,name-r-nnnnn形式的文件名用于reduce输出,其中name是由程序设定的任意名字,nnnnn是一个指明块号的整数(从0开始)。块号保证从不同块写的输出在相同名字情况下不会冲突。
代码如下
package com.zhen.mapreduce.multipleOutput; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
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;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; /**
* @author FengZhen
* @date 2018年8月25日
*
* hadoop jar MultipleOutput.jar com.zhen.mapreduce.multipleOutput.MultipleOutputTest
*/
public class MultipleOutputTest extends Configured implements Tool{ static class MultipleOutputMapper extends Mapper<LongWritable, Text, Text, Text>{
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
// 000001`yiduishuju
String[] values = value.toString().split("`");
context.write(new Text(values[0]), new Text(values[1]));
}
} static class MultipleOutputReducer extends Reducer<Text, Text, NullWritable, Text>{
private MultipleOutputs<NullWritable, Text> multipleOutputs;
@Override
protected void setup(Reducer<Text, Text, NullWritable, Text>.Context context) throws IOException, InterruptedException {
multipleOutputs = new MultipleOutputs<NullWritable, Text>(context);
}
@Override
protected void reduce(Text key, Iterable<Text> value, Reducer<Text, Text, NullWritable, Text>.Context context)
throws IOException, InterruptedException {
for (Text text : value) {
multipleOutputs.write(NullWritable.get(), text, key.toString());
}
}
@Override
protected void cleanup(Reducer<Text, Text, NullWritable, Text>.Context context)
throws IOException, InterruptedException {
multipleOutputs.close();
}
} public int run(String[] args) throws Exception {
Configuration configuration = new Configuration(); Job job = Job.getInstance(configuration);
job.setJobName("MultipleOutputTest");
job.setJarByClass(MultipleOutputTest.class); job.setMapperClass(MultipleOutputMapper.class);
job.setReducerClass(MultipleOutputReducer.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); job.setOutputKeyClass(NullWritable.class);
job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); return job.waitForCompletion(true) ? 0 : 1;
}
public static void main(String[] args) {
try {
String[] params = {"hdfs://fz/user/hdfs/MapReduce/data/multipleOutput/temperature", "hdfs://fz/user/hdfs/MapReduce/data/multipleOutput/output"};
int exitCode = ToolRunner.run(new MultipleOutputTest(), params);
System.exit(exitCode);
} catch (Exception e) {
e.printStackTrace();
}
}
}
数据文件如下
000001`yiduishuju
000002`yiduishuju
000003`yiduishuju
000001`yiduishuju1
000002`yiduishuju1
000003`yiduishuju1
000001`yiduishuju2
000002`yiduishuju2
000003`yiduishuju2
输出文件名如下
000001-r-00000
000002-r-00000
000003-r-00000
MapReduce-多个输出(使用MultipleOutput,不指定reduce任务个数)的更多相关文章
- mapreduce多文件输出的两方法
mapreduce多文件输出的两方法 package duogemap; import java.io.IOException; import org.apache.hadoop.conf ...
- [Demo_03] MapReduce 实现多类型输出
0. 说明 MapReduce 实现将最高气温统计数据输出为文本格式和 SequenceFile 格式 在最高气温统计的基础上进行操作 1. 核心代码 // 多输出格式设置 MultipleOutpu ...
- MapReduce在Map端的Combiner和在Reduce端的Partitioner
1.Map端的Combiner. 通过单词计数WordCountApp.java的例子,如何在Map端设置Combiner... 只附录部分代码: /** * 以文本 * hello you * he ...
- 输出不大于N的素数的个数
输出不大于N的素数的个数 Sieve of Eratosthenes 方法 素数的性质: 非素数可以分解为素数乘积. 证明 (1)n = 2 成立,n = 3 成立: (2)若 n = k 时成立, ...
- my @unpacking_list = values %map_function; print "\n".@unpacking_list; 输出是3 把 @unpacking_list 当做一个数 输出了
my %map_function = ( 88 "OK_func" => "open_statement", 89 & ...
- Java查找指定文件中指定字符的个数
package lwl.youweb2.test; import java.io.BufferedReader; import java.io.FileReader; import java.io.I ...
- 将MapReduce的结果输出至Mysql数据库
package com.sun.mysql;import java.io.DataInput;import java.io.DataOutput;import java.io.IOException; ...
- log4j日志输出到web项目指定文件夹
感谢 eric2500 的这篇文章:http://www.cxyclub.cn/n/27860/ 摘要:尝试将log4j的文件日志输出到web工程制定目录,遇到了很多问题,最终在eric2500的指导 ...
- MapReduce JOB 的输出与输出笔记。
提高 MapReduce 价值,自定义输入和输出. 比如跳过存储到 HDFS 中这个耗时的布置. 而只是从原始数据源接受数据,或者直接将数据发送给某些处理程序. 这些处理程序在 MapReduce 作 ...
随机推荐
- 1、aritcMS-环境搭建-设置布局-布局测试
在开始项目之前,准备开发工具等请参考easyUI章节. 首先.在获取broPHP框架的时候(可以在网上下载).在brophp文件夹同级目录下新建admin.php,以及index.php admin. ...
- win10 IoT开发 SerialDevice 返回 null
树莓派3,win10 Iot,串口开发,抄例子,串口获取返回老是null,例子却可以,代码一样,上网查询结果如下: https://stackoverflow.com/questions/341603 ...
- Palindrome Function
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Total Submissio ...
- <2013 08 26> 雅思听力相关
近两日开始接触雅思题型,初步做了6套剑桥雅思题的听力部分,完成情况还可以,这里做个总结. 1.听力总共约40左右道题目,30min左右完成,结束后有十分钟把答案写到答题卷上.所有听力材料都只播放一遍! ...
- setlocale(LC_ALL, ""); 取值为空字符串" "(注意,不是NULL),则locale与本地环境所使用的编码方式相同(在本地化时,应该很有用);
在C运行库提供的多字节字符-宽字符转换函数:mbstowcs()/wcstombs()中,需要用到全局变量locale( locale encoding ),以指定多字节字符的编码类型 1. 功能: ...
- PAT 1070. 结绳(25)
给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下图所示套接在一起.这样得到的绳子又被当成是另一段绳子,可以再次对折去跟另一段绳子串连.每次串连后,原来两段绳子的长度 ...
- 1、Python request(爬虫-百度翻译)
#encoding=utf-8 import requests def fanyi(): while True: context = input("请输入翻译的内容(退出q):") ...
- Java替换字符串中的占位符
在开发中,会有动态配置字符串其中的某些字符,如何使用字符中的占位符,并且在代码动态替换占位符实现动态配置字符串! 1.定义字符串时,再string文件添加字符串: 注意!记得要在字符文件中加上这些: ...
- 剑指offer 面试51题
面试51题: 题目:数组中的逆序对 题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007 ...
- mysql只能连接localhost解决
grant all privileges on *.* to 'root'@'%' identified by 'root';flush privileges;