学习日志---partitioner和采样器-创新互联

Mapreduce中:

站在用户的角度思考问题,与客户深入沟通,找到天宁网站设计与天宁网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、成都网站制作、企业官网、英文网站、手机端网站、网站推广、域名与空间、虚拟空间、企业邮箱。业务覆盖天宁地区。

shuffle阶段是在map和reduce之间,可以自定义排序,自定义分区和自定义分组!

Mapreduce中,map出的数据是键值对,默认的是hashPatitionner来对map出的数据进行分区;

分区的方法还有其他几个:

RandomSampler sampler = 
                     new InputSampler.RandomSampler(0.5, 3000, 10);
IntervalSampler sampler2 = 
                    new InputSampler.IntervalSampler(0.333, 10);
SplitSampler sampler3 = 
                    new InputSampler.SplitSampler(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 sampler = new InputSampler.RandomSampler(1, 3000, 10);
        IntervalSampler sampler2 = new InputSampler.IntervalSampler(0.333, 10);
        SplitSampler sampler3 = new InputSampler.SplitSampler(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的输出格式,以备后面使用。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前标题:学习日志---partitioner和采样器-创新互联
分享链接:http://myzitong.com/article/dppese.html