myMapper
public class myMapper extends Mapper<LongWritable, Text,Text, IntWritable> { Text text = new Text(); IntWritable vOut = new IntWritable(1); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { // 切割 // atguigu // atguigu String[] s = value.toString().split(" "); for (String s1 : s) { text.set(s1); context.write(text,vOut); } }}
myReducer
public class myReducer extends Reducer<Text, IntWritable, Text,IntWritable> { IntWritable vFinalOut = new IntWritable(); @Override protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { // context将map的结果处理成key和Iterable集合 // atguigu (1,1) int sum = 0; for (IntWritable value : values) { sum += value.get(); } vFinalOut.set(sum); context.write(key,vFinalOut); }}
myDriver
public class myDriver { public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException { Job job = Job.getInstance(); job.setMapperClass(myMapper.class); job.setReducerClass(myReducer.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); job.setOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); FileInputFormat.setInputPaths(job, new Path("C:\\tmp\\input2\\aa.txt")); FileOutputFormat.setOutputPath(job, new Path("C:\\tmp\\output99")); //提交任务 boolean result = job.waitForCompletion(true); // 模仿官方文档退出程序 System.exit(result ? 0 : 1); }}
Debug

点击查看【bilibili】