MapReduce练习二:ChainMapper和ChainReducer的使用

练习的题目如图:


解决这类问题的思路是:使用一个Mapper来过滤掉不需要的英文呢,然后就是常规的MapReduce程序,这里使用ChainMapper和ChainReducer,所用到的资源http://pan.baidu.com/s/1i502dIt

具体代码如下:

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.KeyValueTextInputFormat;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextOutputFormat;
import org.apache.hadoop.mapred.lib.ChainMapper;
import org.apache.hadoop.mapred.lib.ChainReducer;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;


public class ChainMapReduce extends Configured implements Tool {
	
	public static class MapFilter extends MapReduceBase implements Mapper<Text,Text,Text,Text>
	{

		public void map(Text key, Text value, OutputCollector<Text, Text> collector,
				Reporter reporter) throws IOException {
			String s=key.toString();
			if(s.matches("^[0-9]*$"))
			{
				collector.collect(value, key);
			}
		}
		
	}

	public static class MapClass extends MapReduceBase implements Mapper<Text,Text,IntWritable,Text>
	{

		public void map(Text key, Text value, OutputCollector<IntWritable, Text> collect,
				Reporter report) throws IOException {
			collect.collect(new IntWritable(Integer.parseInt(key.toString())), key);
		}
		
	}
	
	
	public static class ReducerClass extends  MapReduceBase implements Reducer<IntWritable, Text, IntWritable, IntWritable>
	{

		public void reduce(IntWritable key, Iterator<Text> values,
				OutputCollector<IntWritable, IntWritable> collect, Reporter report)
				throws IOException {
			int count=0;
			while(values.hasNext())
			{
				values.next();
				count++;
			}
			collect.collect(key, new IntWritable(count));
		}

	}
	
	
	//核心
	public int run(String[] arg0) throws Exception {
		//创建一个作业
		Configuration conf=getConf();
		JobConf jobConf=new JobConf(conf,ChainMapReduce.class);
		//设置输入输出路径
		Path in=new Path(arg0[0]);
		Path out=new Path(arg0[1]);
		FileInputFormat.setInputPaths(jobConf,in);
		FileOutputFormat.setOutputPath(jobConf, out);
		//设置mapper、reducer对象
		jobConf.setJobName("ChainMapReduce");
		
		//指定K1、V1的数据格式
		jobConf.setInputFormat(KeyValueTextInputFormat.class);
		jobConf.setOutputFormat(TextOutputFormat.class);
		jobConf.set("key.value.separator.in.input.line", ",");
		
		JobConf map1=new JobConf(false);
		ChainMapper.addMapper(jobConf, MapFilter.class, Text.class, Text.class, Text.class, Text.class, true,map1);
		
		JobConf map2=new JobConf(false);
		ChainMapper.addMapper(jobConf, MapClass.class, Text.class, Text.class, IntWritable.class, Text.class, true, map2);
		
		JobConf reduce=new JobConf(false);
		ChainReducer.setReducer(jobConf, ReducerClass.class, IntWritable.class, Text.class, IntWritable.class, IntWritable.class, true, reduce);
		//启动MapReduce作业
		JobClient.runJob(jobConf);
		
		return 0;
	}
	
	public static void main(String[] args) throws Exception {
		int res=ToolRunner.run(new Configuration(), new ChainMapReduce(), args);
		System.exit(res);
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值