MapReduce 编程 系列九 Reducer数目
本篇介绍怎样控制reduce的数目。前面观察结果文件,都会发现通常是以part-r-00000 形式出现多个文件,事实上这个reducer的数目有关系。reducer数目多,结果文件数目就多。
在初始化job的时候。是能够设置reducer的数目的。example4在example的基础上做了改动。改动了pom.xml。使得结束一个參数作为reducer的数目。改动了LogJob.java的代码,作为设置reducer数目。
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.freebird</groupId>
<artifactId>mr1_example4</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>mr1_example4</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>hadoop</executable>
<arguments>
<argument>jar</argument>
<argument>target/mr1_example4-1.0-SNAPSHOT.jar</argument>
<argument>org.freebird.LogJob</argument>
<argument>/user/chenshu/share/logs</argument>
<argument>1</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
LogJob.java代码:
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.freebird.reducer.LogReducer;
import org.freebird.mapper.LogMapper;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.fs.FileSystem;
import java.io.IOException; public class LogJob { public static void main(String[] args) throws Exception {
String inputPath = args[0];
if (inputPath.endsWith("/")) {
inputPath = inputPath.substring(0, inputPath.length() -1);
}
System.out.println("args[0] indicates input folder path, the last / will be removed if it exists:" + inputPath);
String outputPath = inputPath + "/output";
System.out.println("output folder path is:" + outputPath); int numReducer = Integer.parseInt(args[1]);
System.out.println("reducer number is: " + args[1]); Configuration conf = new Configuration();
Job job = new Job(conf, "sum_did_from_log_file");
job.setJarByClass(LogJob.class); job.setMapperClass(org.freebird.mapper.LogMapper.class); job.setReducerClass(org.freebird.reducer.LogReducer.class);
job.setNumReduceTasks(numReducer); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); Path path1 = new Path(inputPath);
Path path2 = new Path(outputPath); removeFolder(path2, conf); MultipleOutputs.addNamedOutput(job, "result", TextOutputFormat.class, Text.class, IntWritable.class); FileInputFormat.addInputPath(job, path1);
FileOutputFormat.setOutputPath(job, path2); System.exit(job.waitForCompletion(true) ? 0 : 1);
} private static void removeFolder(Path path, Configuration conf) throws IOException {
FileSystem fs = path.getFileSystem(conf);
if (fs.exists(path)) {
fs.delete(path);
}
}
}
执行结果,通过观察jobtracker。的确reducer数目为1了。
而且结果文件也变成了仅仅有一个:
[chenshu@hadoopMaster example4]$ hdfs dfs -ls /user/chenshu/share/logs/output/
14/10/03 14:18:35 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Found 4 items
-rw-r--r-- 3 chenshu chenshu 0 2014-10-03 12:53 /user/chenshu/share/logs/output/_SUCCESS
drwxr-xr-x - chenshu chenshu 0 2014-10-03 12:52 /user/chenshu/share/logs/output/_logs
-rw-r--r-- 3 chenshu chenshu 0 2014-10-03 12:53 /user/chenshu/share/logs/output/part-r-00000
-rw-r--r-- 3 chenshu chenshu 4391668 2014-10-03 12:53 /user/chenshu/share/logs/output/result-r-00000
MapReduce 编程 系列九 Reducer数目的更多相关文章
- 学习ASP.NET Core Razor 编程系列九——增加查询功能
学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...
- 学习ASP.NET Core Blazor编程系列九——服务器端校验
学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...
- 【原创】MapReduce编程系列之二元排序
普通排序实现 普通排序的实现利用了按姓名的排序,调用了默认的对key的HashPartition函数来实现数据的分组.partition操作之后写入磁盘时会对数据进行排序操作(对一个分区内的数据作排序 ...
- MapReduce编程系列 — 6:多表关联
1.项目名称: 2.程序代码: 版本一(详细版): package com.mtjoin; import java.io.IOException; import java.util.Iterator; ...
- MapReduce编程系列 — 5:单表关联
1.项目名称: 2.项目数据: chile parentTom LucyTom JackJone LucyJone JackLucy MaryLucy Ben ...
- MapReduce编程系列 — 4:排序
1.项目名称: 2.程序代码: package com.sort; import java.io.IOException; import org.apache.hadoop.conf.Configur ...
- MapReduce编程系列 — 3:数据去重
1.项目名称: 2.程序代码: package com.dedup; import java.io.IOException; import org.apache.hadoop.conf.Configu ...
- MapReduce编程系列 — 2:计算平均分
1.项目名称: 2.程序代码: package com.averagescorecount; import java.io.IOException; import java.util.Iterator ...
- MapReduce编程系列 — 1:计算单词
1.代码: package com.mrdemo; import java.io.IOException; import java.util.StringTokenizer; import org.a ...
随机推荐
- 1.1 Introduction中 Producers官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Producers 生产者(Producers) Producers publish ...
- Weblogic问题汇总
1. weblogic unable to get file lock问题 在项目使用过程中,非正常结束Weblogic进程导致Weblogic无法启动,出现以下错误: <BEA-141281& ...
- 深拷贝&浅拷贝
1.区别 浅拷贝:只拷贝了基本数据类型,引用数据类型只复制了引用,没有复制实体. 深拷贝:拷贝所有的层级属性 2.浅拷贝 (1) 直接赋值 拷贝之后,所有层级属性仍然公用了地址,会被影响 var a ...
- VC error link
错误1:LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main在project-setting-link里找到pro ...
- html 代码
1.结构性定义 文件类型 <HTML></HTML> (放在档案的开头与结尾) 文件主题 <TITLE></TITLE> (必须放在「文头」区块内) 文 ...
- Altium Designer布局移动原件的问题
- DOM相关知识总结
DOM相关: 1.获取DOM元素 document.getElementById document.getElementsByName document.getElementsByTagName do ...
- HDU 1215 七夕节 数学题~
http://acm.hdu.edu.cn/showproblem.php?pid=1215 题目大意: 找对象的题...汗..将你的编号(唯一)的所有因子加起来,所得到的的另一个编号的主人就是你的另 ...
- 18、IIC总线驱动程序
i2c_s3c2410.c是内核自带dev层(adapt)驱动程序,知道怎么发收数据,不知道含义 在与i2c_s3c2410.c(在其probe函数中的s3c24xx_i2c_init函数会初始化ii ...
- BAPC2014 C&&HUNNU11583:Citadel Construction(几何)
题意: 给出一系列的点,要求寻找最多4个点.使得组成一个面积最大的多边形 思路: 非常显然仅仅有两种情况.要么是三角形,要么是四边形 首先不难想到的是.先要把最外面的点都找出来,事实上就是找凸包 可是 ...