package com.simple.mr;import java.io.IOException;import java.util.StringTokenizer;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Mapper;public class TotalScoreMapper extends Mapper<LongWritable, Text, Text, IntWritable> {@Overrideprotected void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException {// 读取一行数据String val = value.toString();// 把读取的数据以换行作为分割符StringTokenizer stringTokenizer = new StringTokenizer(val, "\t");while(stringTokenizer.hasMoreElements()) {StringTokenizer tmp = new StringTokenizer(stringTokenizer.nextToken());// 对读取的一行的名称和成绩进行切分并写入到context对象中String username = tmp.nextToken();String score = tmp.nextToken();context.write(new Text(username), new IntWritable(Integer.valueOf(score)));}}}
package com.simple.mr;import java.io.IOException;import java.util.Iterator;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Reducer;public class TotalScoreReducer extends Reducer<Text, IntWritable, Text, IntWritable>{@Overrideprotected void reduce(Text key, Iterable<IntWritable> values,Context context)throws IOException, InterruptedException {// 获取对键值集合遍历对象Iterator<IntWritable> iterator = values.iterator();int sum =0;// 循环获取相同键的所有值并计算和while(iterator.hasNext()){int v = iterator.next().get();sum += v;}context.write(key, new IntWritable(sum));}}
package com.simple.mr;import org.apache.hadoop.conf.Configuration;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;public class TotalScoreDriver {public static void main(String[] args) throws Exception{//获取作业对象Job job = Job.getInstance(new Configuration());//设置主类job.setJarByClass(TotalScoreDriver.class);//设置job参数job.setMapperClass(TotalScoreMapper.class);job.setReducerClass(TotalScoreReducer.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);//设置job输入输出FileInputFormat.setInputPaths(job, new Path("hdfs://localhost:9000/data/dataset/input/score.txt"));FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:9000/data/dataset/output"));System.exit(job.waitForCompletion(true) ? 0 : 1);}}
1528





