前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MapReduce当中Combiner的用法

MapReduce当中Combiner的用法

作者头像
马克java社区
修改2021-07-05 10:21:51
1940
修改2021-07-05 10:21:51
举报
文章被收录于专栏:java大数据

马克-to-win @ 马克java社区:防盗版实名手机尾号:73203。在上一章的helloworld例子中,每一个map都可能会产生大量的本地输出,这些输出会通过网络到达reducer端,这样会非常浪费带宽。解决这个问题可以通过Combiner。Combiner的作用就是对map端的输出先做一次合并,是MapReduce的一种优化手段之一。

package com;

import java.io.IOException;

import java.util.StringTokenizer;

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.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.GenericOptionsParser;

public class WordCountMark_to_win {

public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {

private IntWritable one = new IntWritable(1);

private Text word = new Text();

public void map(Object key, Text value, Context context) throws IOException, InterruptedException {

System.out.println("key is 马克-to-win @ 马克java社区:防盗版实名手机尾号:73203"+key.toString()+" value is "+value.toString());

StringTokenizer itr = new StringTokenizer(value.toString());

while (itr.hasMoreTokens()) {

word.set(itr.nextToken());

context.write(word, one);

}

}

}

public static class PartitionClass extends Partitioner<Text, IntWritable>

{

/*

int com.WordCount.PartitionClass.getPartition(Text key, IntWritable value, int numPartitions)

Get the partition number for a given key (hence record) given the total number of partitions i.e. number of reduce-tasks for the job.

Parameters:key the key to be partioned.value the entry value.numPartitions the total number of partitions.

Returns:the partition number for the key.

*/

更多请见下节:https://blog.csdn.net/qq_44594249/article/details/96327542

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
大数据
全栈大数据产品,面向海量数据场景,帮助您 “智理无数,心中有数”!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档