生活随笔
收集整理的这篇文章主要介绍了
Hadoop大数据——mapreduce的排序机制之total排序
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
(1)设置一个reduce task ,全局有序,但是并发度太低,单节点负载太大
(2)设置分区段partitioner,设置相应数量的reduce task,可以实现全局有序,但难以避免数据分布不均匀——数据倾斜问题,有些reduce task负载过大,而有些则过小;
(3)可以通过编写一个job来统计数据分布规律,获取合适的区段划分,然后用分区段partitioner来实现排序;但是这样需要另外编写一个job对整个数据集运算,比较费事
(4)利用hadoop自带的取样器,来对数据集取样并划分区段,然后利用hadoop自带的TotalOrderPartitioner分区来实现全局排序
public class TotalSort {static class TotalSortMapper extends Mapper<Text, Text, Text, Text> {OrderBean bean
= new OrderBean();@Overrideprotected void map(Text key
, Text value
, Context context
) throws IOException
, InterruptedException
{context
.write(key
, value
);}}static class TotalSortReducer extends Reducer<Text, Text, Text, Text> {@Overrideprotected void reduce(Text key
, Iterable
<Text> values
, Context context
) throws IOException
, InterruptedException
{for (Text v
: values
) {context
.write(key
, v
);}}}public static void main(String
[] args
) throws Exception
{Configuration conf
= new Configuration();Job job
= Job
.getInstance(conf
);job
.setJarByClass(TotalSort
.class);job
.setMapperClass(TotalSortMapper
.class);job
.setReducerClass(TotalSortReducer
.class);
job
.setInputFormatClass(SequenceFileInputFormat
.class);FileInputFormat
.setInputPaths(job
, new Path(args
[0]));FileOutputFormat
.setOutputPath(job
, new Path(args
[1]));job
.setPartitionerClass(TotalOrderPartitioner
.class);RandomSampler randomSampler
= new InputSampler.RandomSampler<Text,Text>(0.1,100,10);InputSampler
.writePartitionFile(job
, randomSampler
);Configuration conf2
= job
.getConfiguration();String partitionFile
= TotalOrderPartitioner
.getPartitionFile(conf2
);job
.addCacheFile(new URI(partitionFile
));job
.setNumReduceTasks(3);job
.waitForCompletion(true);}
}
总结
以上是生活随笔为你收集整理的Hadoop大数据——mapreduce的排序机制之total排序的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。