MapReduce-定制Partitioner-求文件奇偶数行之和
来源:互联网 发布:nginx配置文件server 编辑:程序博客网 时间:2024/05/16 23:35
这篇博客说明Partioner定制的问题,partion发生在map阶段的最后,会先调用job.setPartitionerClass对这个List进行分区,每个分区映射到一个reducer。每个分区内又调用job.setSortComparatorClass设置的key比较函数类排序。前面的几篇博客的实例都是用的一个reducer,这个实例的完成将使用二个reducer的情况,至于多reducer的测试将在全局排序的实例中演示。
下面是本篇博客的实例的需求:
测试数据:
324
654
23
34
78
2
756
134
32
需求:求出数据的奇数行和偶数行之和
这里主要是用到定制partitioner,以下是如何自定义分区函数类。
只要继承Partitioner<T,T>
public class MyPartitioner extends Partitioner<T,T>
然后去实现其中的getPartition()方法就行了,在其中完成分区的逻辑以及一些对于需求的对象key-value对的修改
下面为实现代码:
map阶段:reduce阶段:启动函数:运行结果:
为了说明某一个知识点的作用,博客都是以尽可以只涉及要讲的点的运行,到后面会有一些综合一点的,一些点结合起来的例子,mapreduce框架很灵活,可以定制的功能也很多,后面会一一的说明,比如自定义InputFormat、RecordReader、OutputFormat、RecordWriter,后面还会说明难一点的实例--使用mapreduce处理xml以及json格式的文件来分别说明这些扩展点。对于大文件被分成多个spilt而用多个map计算奇偶数行之和,参见《MapReduce-定制Partitioner-使用NLineInputFormat处理大文件-求文件奇偶数行之和》
下面是本篇博客的实例的需求:
测试数据:
324
654
23
34
78
2
756
134
32
需求:求出数据的奇数行和偶数行之和
这里主要是用到定制partitioner,以下是如何自定义分区函数类。
只要继承Partitioner<T,T>
public class MyPartitioner extends Partitioner<T,T>
然后去实现其中的getPartition()方法就行了,在其中完成分区的逻辑以及一些对于需求的对象key-value对的修改
下面为实现代码:
自定义partitioner:
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.LongWritable;
- import org.apache.hadoop.mapreduce.Partitioner;
- public class MyPartitioner extends Partitioner<LongWritable, IntWritable> {
- @Override
- public int getPartition(LongWritable key, IntWritable value, int arg2) {
- /**
- * 根据行号进行分区,把行号为的偶数的分区到0号reduce
- * 把行号为奇数的分区到1号reduce,并把key的值设置为0或1
- * 目的是为了在进入reduce时奇数和偶数能被分别放到同一个
- * 迭代器中以便求和操作
- */
- if( key.get() % 2 == 0) {
- key.set(0);
- return 0;
- } else {
- key.set(1);
- return 1;
- }
- }
- }
- 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;
- public class MyMapper extends Mapper<LongWritable, Text, LongWritable, IntWritable> {
- private long lineNum = 0;
- private LongWritable okey = new LongWritable();
- @Override
- protected void map(LongWritable key, Text value, Context context)
- throws IOException, InterruptedException {
- lineNum ++;
- okey.set(lineNum);
- /**
- * 输出行号作为key,并把行的值作为value,这里只是简单的说明的patitioner的定制
- * 不考虑多mapper情况下行号控制,这里只关注partitioner的使用就行
- */
- context.write(okey, new IntWritable(Integer.parseInt(value.toString())));
- }
- }
- 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.Reducer;
- public class MyReducer extends Reducer<LongWritable, IntWritable, Text, IntWritable> {
- @Override
- protected void reduce(LongWritable key, Iterable<IntWritable> value, Context context)
- throws IOException, InterruptedException {
- int sum = 0;
- for( IntWritable val : value) {
- sum += val.get();
- }
- if( key.get() == 0 ) {
- context.write(new Text("偶数行之和为:"), new IntWritable(sum));
- } else if ( key.get() == 1) {
- context.write(new Text("奇数行之和为:"), new IntWritable(sum));
- }
- }
- }
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.FileSystem;
- 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.lib.input.FileInputFormat;
- import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
- public class JobMain {
- public static void main(String[] args) throws Exception {
- Configuration configuration = new Configuration();
- Job job = new Job(configuration, "partitioner-job");
- job.setJarByClass(JobMain.class);
- job.setMapperClass(MyMapper.class);
- job.setMapOutputKeyClass(LongWritable.class);
- job.setMapOutputValueClass(IntWritable.class);
- //设置自定义的Partitioner对map输出进行分区
- job.setPartitionerClass(MyPartitioner.class);
- job.setReducerClass(MyReducer.class);
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(IntWritable.class);
- //设置job的reducer的个数为2
- job.setNumReduceTasks(2);
- FileInputFormat.addInputPath(job, new Path(args[0]));
- Path outputDir = new Path(args[1]);
- FileSystem fs = FileSystem.get(configuration);
- if( fs.exists(outputDir)) {
- fs.delete(outputDir ,true);
- }
- FileOutputFormat.setOutputPath(job, outputDir);
- System.exit(job.waitForCompletion(true) ? 0: 1);
- }
- }
为了说明某一个知识点的作用,博客都是以尽可以只涉及要讲的点的运行,到后面会有一些综合一点的,一些点结合起来的例子,mapreduce框架很灵活,可以定制的功能也很多,后面会一一的说明,比如自定义InputFormat、RecordReader、OutputFormat、RecordWriter,后面还会说明难一点的实例--使用mapreduce处理xml以及json格式的文件来分别说明这些扩展点。对于大文件被分成多个spilt而用多个map计算奇偶数行之和,参见《MapReduce-定制Partitioner-使用NLineInputFormat处理大文件-求文件奇偶数行之和》
0 0
- MapReduce-定制Partitioner-求文件奇偶数行之和
- MapReduce-定制Partitioner-求文件奇偶数行之和
- MapReduce-定制Partitioner-使用NLineInputFormat处理大文件-求文件奇偶数行之和
- MapReduce-使用NLineInputFormat处理大文件-求文件奇偶数行之和
- 求奇偶数
- 求两个数之和
- MapReduce之自定义partitioner
- MapReduce之自定义partitioner
- MapReduce 进阶:Partitioner 组件
- [Hadoop]MapReduce中的Partitioner
- MapReduce 进阶:Partitioner 组件
- Mapreduce中的分区Partitioner
- Hadoop MapReduce中的Partitioner
- MapReduce编程之Partitioner
- Mapreduce中的分区Partitioner
- 求奇偶数的简单方法
- 求一个数各位数之和
- MapReduce中的分区方法Partitioner
- 图的最短路径
- Codeforces Problem 707A Brain's Photos(水题)
- SCP命令的简单使用及在两台LINUX机器传输文件不用输密码的设置
- C++ STL学习——string
- ava中extends 与 implement 区别(转载)
- MapReduce-定制Partitioner-求文件奇偶数行之和
- #198 Permutation Index II
- JAVA 学习模块一:基础知识
- maven简记
- 博弈
- 黄阶低级 - 《 Java 核心》- 4.8 类路径 - 修改0次
- CentOS服务器安全设置
- MapReduce-使用NLineInputFormat处理大文件-求文件奇偶数行之和
- mui 图标显示不出来解决方案