20,809
社区成员
发帖
与我相关
我的任务
分享
package org.apache.hadoop.examples;
import java.io.IOException;
//导入各种Hadoop包
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;
import org.apache.hadoop.util.GenericOptionsParser;
// 主类
public class Sort {
// Mapper类
public static class Map extends Mapper<Object, Text, IntWritable, IntWritable>{
// new一个值为空的IntWritable对象
private static IntWritable data = new IntWritable();
// 实现map函数
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
// 将切分后的value作为中间输出的key,然后value值为1。
String line = value.toString();
data.set(Integer.parseInt(line));
context.write(data, new IntWritable(1));
}
}
// Reducer类
public static class Reduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {
// new一个值为1的IntWritable对象
private static IntWritable linenum = new IntWritable(1);
// 实现reduce函数
public void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
// 写入结果键值对
for (IntWritable val : values) {
context.write(linenum, key);
linenum = new IntWritable(linenum.get()+1);
}
}
}
public static class Partition extends Partitioner <IntWritable, IntWritable> {
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+1) && keynumber >= bound*i) {
return i;
}
}
return -1;
}
}
// 主函数
public static void main(String[] args) throws Exception {
// 获取配置参数
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
// 检查命令语法
if (otherArgs.length != 2) {
System.err.println("Usage: Dedup <in> <out>");
System.exit(2);
}
// 定义作业对象
Job job = new Job(conf, "Sort");
// 注册分布式类
job.setJarByClass(Sort.class);
// 注册Mapper类
job.setMapperClass(Map.class);
// 注册Reducer类
job.setReducerClass(Reduce.class);
// 注册Partition类
//job.setPartitionerClass(Partition.class);
// 注册输出格式类
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class);
// 设置输入输出路径
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
// 运行程序
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
// Reducer类
public static class Reduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {
// new一个值为空的IntWritable对象
private static IntWritable linenum = new IntWritable(1);
// 实现reduce函数
public void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
// 写入结果键值对
for (IntWritable val : values) {
context.write(linenum, key);
linenum = new IntWritable(linenum.get()+1);
}
}
}