搜狗网站seo成品网页大全下载
web/
2025/9/27 22:43:13/
文章来源:
搜狗网站seo,成品网页大全下载,关键词优化价格,东莞市手机网站建设平台本身求平均数很简单的#xff0c;必须用到combine的话我在两个地方废了很多时间#xff0c;一是combine的输入不仅仅是map的输出#xff0c;还有可能是combine的输出#xff0c;所以对value的处理得分两种情况吧#xff1b;二是结果要保留4位有效数字。。。噗#xff0c;…本身求平均数很简单的必须用到combine的话我在两个地方废了很多时间一是combine的输入不仅仅是map的输出还有可能是combine的输出所以对value的处理得分两种情况吧二是结果要保留4位有效数字。。。噗注意保留4位有效数字不等于小数点后面有四位第二不能用parse它只能转成整形。第三代码写的实在比较挫哎。import java.io.IOException;import java.math.BigDecimal;import java.math.MathContext;import java.util.StringTokenizer;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.DoubleWritable;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.Mapper;import org.apache.hadoop.mapreduce.Reducer;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;public class Avg {public static class Map extendsMapper{protected void map(LongWritablekey, Text value, Context context)throwsIOException, InterruptedException {String line value.toString();StringTokenizerwords new StringTokenizer(line, \n);//对每一行进行分割while(words.hasMoreElements()) {StringTokenizertokenizerLine new StringTokenizer(words.nextToken());StringstrAlp tokenizerLine.nextToken();StringstrData tokenizerLine.nextToken();Textalphabet new Text(strAlp);Textscore new Text(strData);context.write(alphabet,score);}}}public static class Combine extendsReducer{//将部分和以及出现次数共同作为valueprivate Text tmp newText();public void reduce(Text key,Iterable values, Context context)throwsIOException, InterruptedException {//统计某字母出现次数和部分和int sum 0;int count 0;String[]val_input null;for (Text val: values) {if((val_inputval.toString().split(:)).length2){sum Integer.parseInt(val_input[0].toString());countInteger.parseInt(val_input[1].toString()); }else{sum Integer.parseInt(val.toString());count;}} // 设置value值为部分和以及出现次数tmp.set(sum : count);context.write(key, tmp);}}public static class Reduce extends Reducer{private String tmp_sum newString();private String tmp_count newString();public void reduce(Text key,Iterable values, Context context)throws IOException, InterruptedException {int sum_all 0;int count_all0; for (Text val: values) {Stringstr_tmpval.toString();//将部分和以及出现次数分割分别求和intsplitIndex str_tmp.indexOf(:);tmp_sum str_tmp.substring(0, splitIndex);tmp_count str_tmp.substring(splitIndex1);intint_sum Integer.parseInt(tmp_sum);intint_count Integer.parseInt(tmp_count); sum_all int_sum;count_all int_count;} double average (sum_all * 1.0) / (count_all *1.0); MathContext mathContext new MathContext(4);BigDecimal c new BigDecimal(average);BigDecimal aver_final c.round(mathContext);double aver_4 aver_final.doubleValue();String str_aver String.valueOf(aver_4);Text aver new Text();aver.set(str_aver);context.write(key, aver);}}public static void main(String[] args) throwsException {Configuration conf newConfiguration();Job job new Job(conf,Avg_use_combine);job.setJarByClass(Avg.class);job.setMapperClass(Map.class);job.setCombinerClass(Combine.class);job.setReducerClass(Reduce.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);FileInputFormat.addInputPath(job,new Path(args[0]));FileOutputFormat.setOutputPath(job,new Path(args[1]));System.exit(job.waitForCompletion(true)? 0 : 1);}}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/83008.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!