mapreduce排序(自定义Partition)

import java.io.IOException;

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;

/**
 * mapreduce排序
 * 
 * @author asheng file1:1.txt 2 32 654 32 15 756 65223
 * 
 *         file2:2.txt 5956 22 650 92
 * 
 *         file3:3.txt 26 54 6
 *         对file1,file2,file3进行排序,能够第一想到的便是mapreduce自动排序,但是这里面有问题:
 *         Reduce排序只是对发送到自己所在的节点的数据进行排序,不能保证整体的顺序
 *         所以这里要自定义Partition,保证Partition后,Reduce上的数据在整体上是有序的,然后在reduce内进行排序
 */
public class Sort {
	public static class Map extends
			Mapper<Object, Text, IntWritable, IntWritable> {
		private IntWritable data = new IntWritable();

		public void map(Object key, Text value, Context context)
				throws IOException, InterruptedException {
			data.set(Integer.parseInt(value.toString()));
			context.write(data, new IntWritable(1));
		}
	}

	public static class Reduce extends
			Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {
		private IntWritable data = new IntWritable(1);

		public void reduce(IntWritable key, Iterable<IntWritable> values,
				Context context) throws IOException, InterruptedException {
			for (IntWritable v : values) {
				System.out.println(v);
				context.write(data, key);
				data = new IntWritable(data.get() + 1);
			}
		}
	}

	public static class Partition extends Partitioner<IntWritable, IntWritable> {
		@Override
		public int getPartition(IntWritable key, IntWritable value,
				int numPartitions) {
			int Maxnumber = 65223;
			int bound = Maxnumber / numPartitions + 1;
			int keynumber = key.get();
			for (int i = 0; i < numPartitions; i++) {
				if (keynumber < bound * i && keynumber >= bound * (i - 1)) {
					return i - 1;
				}
			}
			return 0;
		}

	}

	public static void main(String[] args) throws IOException,
			InterruptedException, ClassNotFoundException {
		Configuration conf = new Configuration();
		Job job = new Job(conf, "sort");
		job.setJarByClass(Sort.class);
		job.setMapperClass(Map.class);
		job.setReducerClass(Reduce.class);
		job.setPartitionerClass(Partition.class);
		job.setOutputKeyClass(IntWritable.class);
		job.setOutputValueClass(IntWritable.class);
		FileInputFormat.setInputPaths(job, "/home/asheng/hadoop/in");
		FileOutputFormat
				.setOutputPath(job, new Path("/home/asheng/hadoop/out"));
		job.waitForCompletion(true);
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值