mapreduce 平均成绩

map处理的是一个纯文本文件,文件中存放的是数据时每一行标识一个学生的姓名和他相应一科成绩。mapper处理的数据由inputFormat分解过的数据集,其中inputFormat的作用是将数据集切割成小数据集inputsplit,每一个inputsplit将有 一个mapper负责处理,此外inputFormat中还提供了recordreader的实现,并将一个inputsplit解析成

import java.io.IOException
import java.util.Iterator
import java.util.StringTokenier;
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.Path
import org.apache.hadoop.io.Intwritable
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.Readucer
import org.apache.hadoop.mapreduc.lib.input.FileInputFormat
import org.apache.hadoop.maoreduce.lib.input.TextInputFormat
import org.apache.hadoop.....
public static class Map extends Mapper<LongWritable,Text,Text,IntWritable>{
//实现map函数

public void map(LongWritable key,Text value,Context context){
String ling=value.toString();
StringTokenizer tokenizerArticle =new StringTokenizer(line,"\n");
while(tokenierArticle.hashMoreElements()){
StringTokenizer tokenizerLine=new StringTokenizer(tokenizerArticle.nextToken());
String strName=tokenizerLine.nextToken();
String strScore=tokenizerLine.nextToken();
Text name=new Text(strName);
int socreInt=Integer.parseInt(strScore);
context.write(name,new IntWritable(scoreInt));
}
}
}
public static class Reduce extends Reducer <Text,IntWritable,Text,IntWritable>{
public void reduce(Text key,Itertor<IntWritable> values,Context context){
int sum=0;
int count=0;
Iterator <IntWritable> iterator =values.iterator();
while(iterator.hashNext()){
sum+=iterator.next().get();
count++;
}
int average=(int)sum/count;
context.write(key,new IntWritable(average));

}
}
public static void main(String args[]){
Configuration conf=new Configuration();
conf.set("mapred.job.tracker","192.168.224.100");
String [] ioArgs=new String []{"score_in","score_out"};
String [] otherArgs=new GenericOptionParser(conf,ioArgs).getRemainingArgs();
if (otherArgs.length != 2) {

            System.err.println("Usage: Score Average <in> <out>");

            System.exit(2);

        }



        Job job = new Job(conf, "Score Average");

        job.setJarByClass(Score.class);



        // 设置Map、Combine和Reduce处理类

        job.setMapperClass(Map.class);

        job.setCombinerClass(Reduce.class);

        job.setReducerClass(Reduce.class);



        // 设置输出类型

        job.setOutputKeyClass(Text.class);

        job.setOutputValueClass(IntWritable.class);



        // 将输入的数据集分割成小数据块splites,提供一个RecordReder的实现

        job.setInputFormatClass(TextInputFormat.class);

        // 提供一个RecordWriter的实现,负责数据输出

        job.setOutputFormatClass(TextOutputFormat.class);



        // 设置输入和输出目录

        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));

        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));

        System.exit(job.waitForCompletion(true) ? 0 : 1);

    }

} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值