hadoop实战之排序Sort源码

Sort:

package com.test.sorted;

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;

public class Sorted {
	public static class TokenizerMapper extends Mapper {
		private static IntWritable data = new IntWritable();

		public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
			String str = value.toString().trim();

			data.set(Integer.parseInt(str));
			context.write(data, new IntWritable(1));
		}
	}

	public static class TokenizerReducer extends Reducer {
		private static IntWritable linenum = new IntWritable(0);

		public void reduce(IntWritable key, Iterable values, Context context)
				throws IOException, InterruptedException {
			for (IntWritable val : values) {
				context.write(linenum, key);
				linenum =new IntWritable(linenum.get() + 1);
			}
			// context.write(key,new Text(""));
		}

	}

public static class Partition extends Partitioner{

	@Override
	public int getPartition(IntWritable key, IntWritable value, int partition) {
		int max=65535;
		int fenshu=max/partition+1;
		int keynum=key.get();
		for(int i=1;i<=partition;i++)
		{
			if(i*fenshu>keynum&&(i-1)*fenshu
源码出自:《hadoop实战》

你可能感兴趣的:(大数据)