MapReduce排序程序

1 输入数据

import java.io.DataOutputStream;
import java.util.Random;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

/**
 * 随机生成一批32位长的有符号整数
 * 用法:INTs <生成整数的数量> <输出文件的路径>
 */
public class INTs {

	public static void main(String[] args)/*----*/throws Exception {
		long num = Long.parseLong(args[0]);
		Random random = new Random(1234567890);
		FileSystem fileSystem = FileSystem.get(new Configuration());
		DataOutputStream out = fileSystem.create(new Path(args[1]));
		try {
			for (long i = 0; i < num; ++i) {
				Integer value = random.nextInt();
				out.writeBytes(value.toString());
				out.write('\n');
			}
		} finally {
			out.close();
		}
	}

}

2 排序程序

import java.io.IOException;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
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.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Partitioner;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;

/**
 * 对一批32位长的有符号整数排序
 * 用法:Sort <输入文件的路径> <输出目录的路径> <生成结果文件的数量>
 */
public class Sort {

	public static void main(String[] args)/*----*/throws Exception {
		JobConf conf = new JobConf();
		conf.setJobName("Sort INTs");
		conf.setJarByClass(Sort.class);
		conf.setMapOutputKeyClass(IntWritable.class);
		conf.setMapOutputValueClass(NullWritable.class);
		conf.setMapperClass(SortMapper.class);
		conf.setPartitionerClass(SortPartitioner.class);
		conf.setInputFormat(TextInputFormat.class);
		FileInputFormat.setInputPaths(conf, new Path(args[0]));
		conf.setOutputFormat(TextOutputFormat.class);
		FileOutputFormat.setOutputPath(conf, new Path(args[1]));
		conf.setNumReduceTasks(Integer.parseInt(args[2]));
		JobClient.runJob(conf);
	}

	public static class SortMapper extends MapReduceBase implements
	/*     */Mapper<LongWritable, Text, IntWritable, NullWritable> {

		public void map(LongWritable key, Text value,
				OutputCollector<IntWritable, NullWritable> output,
				Reporter reporter) throws IOException {
			int i = Integer.parseInt(value.toString());
			output.collect(new IntWritable(i), NullWritable.get());
		}

	}

	public static class SortPartitioner implements
	/*                    */Partitioner<IntWritable, NullWritable> {

		private int[] rangeUpperLimits;

		public void configure(JobConf job) {
			int numRanges = job.getNumReduceTasks();
			rangeUpperLimits = new int[numRanges - 1];
			long perRangeSize = (1L << 32) / numRanges;
			long upperLimit = Integer.MIN_VALUE;
			for (int i = 0; i < numRanges - 1; ++i) {
				upperLimit += perRangeSize;
				rangeUpperLimits[i] = (int) upperLimit;
			}
		}

		public int getPartition(IntWritable key,
		/*                */NullWritable value, int numPartitions) {
			int n = rangeUpperLimits.length;
			if (n == 0)
				return 0;
			int x = key.get();
			if (x >= rangeUpperLimits[n - 1])
				return n;
			int l = 0, h = n - 1;
			while (l < h) {
				int i = (l + h) / 2;
				int y = rangeUpperLimits[i];
				if (x >= y)
					l = i + 1;
				else
					h = i;
			}
			return l;
		}
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值