Mapreduce中:
shuffle阶段是在map和reduce之间,可以自定义排序,自定义分区和自定义分组!
Mapreduce中,map出的数据是键值对,默认的是hashPatitionner来对map出的数据进行分区;
分区的方法还有其他几个:
RandomSampler<Text, Text> sampler =
new InputSampler.RandomSampler<Text, Text>(0.5, 3000, 10);
IntervalSampler<Text, Text> sampler2 =
new InputSampler.IntervalSampler<Text, Text>(0.333, 10);
SplitSampler<Text, Text> sampler3 =
new InputSampler.SplitSampler<Text, Text>(reduceNumber);
实现和细节
public class TotalSortMR {
@SuppressWarnings("deprecation")
public static int runTotalSortJob(String[] args) throws Exception {
Path inputPath = new Path(args[0]);
Path outputPath = new Path(args[1]);
Path partitionFile = new Path(args[2]);
int reduceNumber = Integer.parseInt(args[3]);
//三种采样器
RandomSampler<Text, Text> sampler = new InputSampler.RandomSampler<Text, Text>(1, 3000, 10);
IntervalSampler<Text, Text> sampler2 = new InputSampler.IntervalSampler<Text, Text>(0.333, 10);
SplitSampler<Text, Text> sampler3 = new InputSampler.SplitSampler<Text, Text>(reduceNumber);
//任务初始化
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJobName("Total-Sort");
job.setJarByClass(TotalSortMR.class);
job.setInputFormatClass(KeyValueTextInputFormat.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setNumReduceTasks(reduceNumber);
//设置所有的分区类
job.setPartitionerClass(TotalOrderPartitioner.class);
//分区类参考的分区文件
TotalOrderPartitioner.setPartitionFile(conf, partitionFile);
//分区使用哪种采样器
InputSampler.writePartitionFile(job, sampler);
//job的输入和输出路径
FileInputFormat.setInputPaths(job, inputPath);
FileOutputFormat.setOutputPath(job, outputPath);
outputPath.getFileSystem(conf).delete(outputPath, true);
return job.waitForCompletion(true)? 0 : 1;
}
public static void main(String[] args) throws Exception{
System.exit(runTotalSortJob(args));
}
}
job默认的输入格式是TextInputFormat,这个是key-value的形式,key是每行的行标,value是每行的内容。可以更改
job.setInputFormatClass(,....)
一般要设置mapper的输出格式,以备后面使用。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。