Hadoop基于WordCount的Mapper、Reducer、Combiner、Partitioner和自定义多文件输出

1    Mapper的代码

public class WorldMapper extends Mapper<Object, Text, Text, IntWritable>{

	public Text textvalue = new Text("test");
	public IntWritable intwrite = new IntWritable(1);
	
	@Override
	protected void map(Object key, Text value,Context context)
			throws IOException, InterruptedException {
		StringTokenizer stringTokenizer = new StringTokenizer(value.toString());
		while(stringTokenizer.hasMoreTokens())
		{
			textvalue.set(stringTokenizer.nextToken());
			context.write(textvalue,intwrite);
		}
	}
}

2    Reducer的代码

public class WordReduce extends Reducer<Text,IntWritable,Text, IntWritable>{

	public IntWritable value = new IntWritable();
	
	@Override
	protected void reduce(Text text, Iterable<IntWritable> values, Context context)
			throws IOException, InterruptedException {
		
		int sum = 0;
		while(values.iterator().hasNext())
		{
			sum += values.iterator().next().get();
		}
		value.set(sum + 10);
		context.write(text, value);		
	}
}

3    Combiner的代码

public class WordCombiner extends Reducer<Text, IntWritable, Text, IntWritable> {

	private IntWritable value = new IntWritable();
	@Override
	protected void reduce(Text text, Iterable<IntWritable> values, Context context)
			throws IOException, InterruptedException {
		
		int sum = 0;
		while(values.iterator().hasNext())
		{
			sum += values.iterator().next().get();
		}
		value.set(sum + 10);
		context.write(text, value);
	}
}

4    Partitioner的代码

public class WordPartioner extends Partitioner<Text, IntWritable> {

	@Override
	public int getPartition(Text text, IntWritable value, int numPartion) {
		return text.toString().hashCode() & Integer.MAX_VALUE % numPartion;
	}
}

5     自定义多文件输出

public class WordMultipleOutputFormat extends MultipleOutputFormat<Text, IntWritable>{

	@Override
	protected String generateFileNameForKeyValue(Text key, IntWritable value,
			String name) {
		char c = key.toString().toLowerCase().charAt(0);
		if(c >= 'a' && c <= 'z')
			return c+".txt";
		return "result.txt";	
	}

	@Override
	protected RecordWriter<Text, IntWritable> getBaseRecordWriter(
			FileSystem arg0, JobConf arg1, String arg2, Progressable arg3)
			throws IOException {
		// TODO Auto-generated method stub
		return null;
	}
}

6    驱动程序

public class WordCounter {
	public static void main(String[] args) throws IOException{
		
		Configuration conf = new Configuration();
		String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
		if(otherArgs.length !=2 )
		{
			System.err.println("Usage:wordcount <int><out>");
			System.exit(2);
		}
		Job job = new Job(conf,"word count");
		job.setJarByClass(WordCounter.class);
		job.setMapperClass(WorldMapper.class);
		job.setPartitionerClass(WordPartioner.class);
		job.setReducerClass(WordReduce.class);
		job.setCombinerClass(WordCombiner.class);
		job.setNumReduceTasks(4);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		//job.setOutputFormatClass(WordMultipleOutputFormat.class);
	    	FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
	    	FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
	    	try {
			System.exit(job.waitForCompletion(true) ? 0 : 1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}

7    运行及效果

 

 

为了显示使用了自定义的Combiner,为每个Key的值加10

问题:setOutPutFormat总是找不到自定义的多文件输出类

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值