myMapper

  1. public class myMapper extends Mapper<LongWritable, Text,Text, IntWritable> {
  2. Text text = new Text();
  3. IntWritable vOut = new IntWritable(1);
  4. @Override
  5. protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  6. // 切割
  7. // atguigu
  8. // atguigu
  9. String[] s = value.toString().split(" ");
  10. for (String s1 : s) {
  11. text.set(s1);
  12. context.write(text,vOut);
  13. }
  14. }
  15. }

myReducer

  1. public class myReducer extends Reducer<Text, IntWritable, Text,IntWritable> {
  2. IntWritable vFinalOut = new IntWritable();
  3. @Override
  4. protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
  5. // context将map的结果处理成key和Iterable集合
  6. // atguigu (1,1)
  7. int sum = 0;
  8. for (IntWritable value : values) {
  9. sum += value.get();
  10. }
  11. vFinalOut.set(sum);
  12. context.write(key,vFinalOut);
  13. }
  14. }

myDriver

  1. public class myDriver {
  2. public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
  3. Job job = Job.getInstance();
  4. job.setMapperClass(myMapper.class);
  5. job.setReducerClass(myReducer.class);
  6. job.setMapOutputKeyClass(Text.class);
  7. job.setMapOutputValueClass(IntWritable.class);
  8. job.setOutputKeyClass(Text.class);
  9. job.setMapOutputValueClass(IntWritable.class);
  10. FileInputFormat.setInputPaths(job, new Path("C:\\tmp\\input2\\aa.txt"));
  11. FileOutputFormat.setOutputPath(job, new Path("C:\\tmp\\output99"));
  12. //提交任务
  13. boolean result = job.waitForCompletion(true);
  14. // 模仿官方文档退出程序
  15. System.exit(result ? 0 : 1);
  16. }
  17. }

Debug

image.png
点击查看【bilibili】