资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

学习日志---partitioner和采样器

Mapreduce中:

成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、成都网站制作、盘龙网络推广、重庆小程序开发、盘龙网络营销、盘龙企业策划、盘龙品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联公司为所有大学生创业者提供盘龙建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com

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的输出格式,以备后面使用。


文章名称:学习日志---partitioner和采样器
文章来源:http://cdkjz.cn/article/psphsc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220