WritableComparable 案例部分排序
2021-03-28 18:24
标签:task ati 开头 return NPU 部分 apr nump writable 一、需求分析 1、需求 手机号136、137、138、139开头都分别放到一个独立的4个文件中,其它开头的放到一个文件中 2、分析 a、分区 继承 Partitioner b、排序 实现 WritableComparable 二、具体代码(结合上篇博客) 1、自定义Haoop序列化类、排序 2、Mapper 3、自定义分区 4、Reducer 5、Driver WritableComparable 案例部分排序 标签:task ati 开头 return NPU 部分 apr nump writable 原文地址:https://www.cnblogs.com/wt7018/p/13625355.htmlpackage com.sort;
import org.apache.hadoop.io.WritableComparable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
public class FlowBean implements WritableComparable
package com.sort;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
public class SortMapper extends Mapper
package com.sort;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;
public class PhonePartitioner extends Partitioner
package com.sort;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class SortReducer extends Reducer
package com.sort;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
public class SortDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
args = new String[]{"E:\\a\\output", "E:\\a\\output2"};
// 1.获取job
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
// 2.设置jar
job.setJarByClass(SortDriver.class);
// 3.关联mapper和reducer
job.setMapperClass(SortMapper.class);
job.setReducerClass(SortReducer.class);
// 4.设置mapper输出的k v
job.setMapOutputKeyClass(FlowBean.class);
job.setMapOutputValueClass(Text.class);
// 5.设置整体输出的 k, v
job.setOutputKeyClass(Text.class);
job.setOutputKeyClass(FlowBean.class);
// 8. 设置分区
job.setPartitionerClass(PhonePartitioner.class);
// 9.设置 NumReduceTask
job.setNumReduceTasks(5);
// 6.设置输入输出路径
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
// 7.提交job
boolean wait = job.waitForCompletion(true);
System.exit(wait? 0: 1);
}
}
文章标题:WritableComparable 案例部分排序
文章链接:http://soscw.com/index.php/essay/69154.html