提示
输入文件是一个目录,处理一个目录下面的所有文件
指定目录的话,会处理目录下的所有文件
package scoreCount;
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 maxMapper extends Mapper<LongWritable,Text, Text, IntWritable> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] s = value.toString().split(" ");
context.write(new Text(s[0]),new IntWritable(Integer.valueOf(s[1])));
}
}
package scoreCount;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class maxReducer extends Reducer<Text, IntWritable,Text,IntWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for(IntWritable i : values){
sum += i.get();
}
context.write(key,new IntWritable(sum));
}
}
package scoreCount;
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;
import java.io.IOException;
public class maxDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(maxDriver.class);
job.setMapperClass(maxMapper.class);
job.setReducerClass(maxReducer.class);
//设置mapper的输出类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//设置reducer的输出类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
//文件输入路径
FileInputFormat.addInputPath(job,new Path("hdfs://hadoop01:9000/txt/score2/"));
// 设置输出路径
// 要求输出路径在HDFS上不存在
FileOutputFormat.setOutputPath(job, new Path("hdfs://hadoop01:9000/result/scoresum"));
// 提交任务
job.waitForCompletion(true);
}
}