当前位置: 首页 > news >正文

网页升级访问中每天正常更新中系统优化软件哪个好

网页升级访问中每天正常更新中,系统优化软件哪个好,做网站公司未来的发展方向,wordpress 主题无法更换先简单介绍一下partitioner 和 combiner Partitioner类 用于在Map端对key进行分区 默认使用的是HashPartitioner 获取key的哈希值使用key的哈希值对Reduce任务数求模决定每条记录应该送到哪个Reducer处理自定义Partitioner 继承抽象类Partitioner,重写getPartiti…

先简单介绍一下partitioner 和 combiner 

Partitioner类

  • 用于在Map端对key进行分区
    • 默认使用的是HashPartitioner
      • 获取key的哈希值
      • 使用key的哈希值对Reduce任务数求模
    • 决定每条记录应该送到哪个Reducer处理
  • 自定义Partitioner
    • 继承抽象类Partitioner,重写getPartition方法
    • job.setPartitionerClass(MyPartitioner.class)

Combiner类

  • Combiner相当于本地化的Reduce操作
    • 在shuffle之前进行本地聚合
    • 用于性能优化,可选项
    • 输入和输出类型一致
  • Reducer可以被用作Combiner的条件
    • 符合交换律和结合律
  • 实现Combiner
    • job.setCombinerClass(WCReducer.class)

我们进入案例来看这两个知识点

一 案例需求

一个存放电话号码的文本,我们需要136 137,138 139和其它开头的号码分开存放统计其每个数字开头的号码个数

效果

 二 PhoneMapper 类

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;import java.io.IOException;public class PhoneMapper extends Mapper<LongWritable, Text,Text, IntWritable> {@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {String phone = value.toString();Text text = new Text(phone);IntWritable intWritable = new IntWritable(1);context.write(text,intWritable);}
}

三 PhoneReducer 类

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;import java.io.IOException;public class PhoneReducer extends Reducer<Text, IntWritable,Text,IntWritable> {@Overrideprotected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {int count = 0;for (IntWritable intWritable : values){count += intWritable.get();}context.write(key, new IntWritable(count));}
}

四 PhonePartitioner 类

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;public class PhonePartitioner extends Partitioner<Text, IntWritable> {@Overridepublic int getPartition(Text text, IntWritable intWritable, int i) {//136,137   138,139     其它号码放一起if("136".equals(text.toString().substring(0,3)) || "137".equals(text.toString().substring(0,3))){return 0;}else if ("138".equals(text.toString().substring(0,3)) || "139".equals(text.toString().substring(0,3))){return 1;}else {return 2;}}
}

五 PhoneCombiner 类

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;import java.io.IOException;public class PhoneCombiner extends Reducer<Text, IntWritable,Text,IntWritable> {@Overrideprotected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {int count = 0;for(IntWritable intWritable : values){count += intWritable.get();}context.write(new Text(key.toString().substring(0,3)), new IntWritable(count));}
}

六 PhoneDriver 类

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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 PhoneDriver {public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {Configuration conf = new Configuration();Job job = Job.getInstance(conf);job.setJarByClass(PhoneDriver.class);job.setMapperClass(PhoneMapper.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);job.setCombinerClass(PhoneCombiner.class);job.setPartitionerClass(PhonePartitioner.class);job.setNumReduceTasks(3);job.setReducerClass(PhoneReducer.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);Path inPath = new Path("in/demo4/phone.csv");FileInputFormat.setInputPaths(job, inPath);Path outPath = new Path("out/out6");FileSystem fs = FileSystem.get(outPath.toUri(),conf);if (fs.exists(outPath)){fs.delete(outPath, true);}FileOutputFormat.setOutputPath(job, outPath);job.waitForCompletion(true);}
}

七 小结

该案例新知识点在于分区(partition)和结合(combine)

这次代码的流程是 

driver——》mapper——》partitioner——》combiner——》reducer

map 每处理一条数据都经过一次 partitioner 分区然后存到环形缓存区中去,然后map再去处理下一条数据以此反复直至所有数据处理完成

combine 则是将环形缓存区溢出的缓存文件合并,并提前进行一次排序和计算(对每个溢出文件计算后再合并)最后将一个大的文件给到 reducer,这样大大减少了 reducer 的计算负担

http://www.jinmujx.cn/news/109364.html

相关文章:

  • 企业网站建设技上海百度推广方案
  • 深圳做网站建设月薪多少郑州seo竞价
  • wordpress翻译插件下载优化方案官方网站
  • 设计大师网站搜索引擎推广方式有哪些
  • 佛山网站建设怎么做百度pc端提升排名
  • 网站建设的目的和作用百度惠生活怎么优化排名
  • 怎么做赌钱网站代理海外市场推广方案
  • 宁德网站开发公司千万不要去电商公司上班
  • wordpress用户中心制作seo是什么公司
  • 广州开发网站哪家专业海外推广营销平台
  • 烟台哪儿有可以做淘宝网站的官网优化包括什么内容
  • ps网站设计全程绝密企业信息查询
  • 工业互联网平台评价方法培训seo去哪家机构最好
  • 个人网站转为企业网站搜索引擎优化排名
  • 3d演示中国空间站建造历程网络热词大全
  • 做网站的盈利模式搜索引擎下载入口
  • 如何在国外网站开发新客人宁德市教育局官网
  • 做购物网站的外挂需要自己搭建服务器吗外国人b站
  • 织梦的网站数据还原怎么做seo排名计费系统
  • 男女做那个的免费视频网站hyein seo官网
  • 网站开发专业的武汉seo推广
  • 网站怎么做qq客服推广普通话内容100字
  • 做环氧地坪工程网站成都公司网站seo
  • 阿里巴巴做特卖的网站广告联盟论坛
  • 山西省城乡住房和建设厅网站首页全网网络营销
  • 网站建设留言板怎么做铁岭网站seo
  • 商业网站案例灰色词seo排名
  • 怎样在手机做自己的网站6深圳网站seo优化
  • 网站地址栏厦门最好的seo公司
  • 百度h5可以做网站吗网站浏览器