19 | | public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> { |
20 | | public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException { |
21 | | |
22 | | while (tokenizer.hasMoreTokens()) { |
23 | | private final static IntWritable one = new IntWritable(1); |
24 | | private Text word = new Text(); |
25 | | output.collect(word, one); |
26 | | |
| 19 | #!java |
| 20 | 14. public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> { |
| 21 | 15. private final static IntWritable one = new IntWritable(1); |
| 22 | 16. private Text word = new Text(); |
| 23 | 17. |
| 24 | 18. public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException { |
| 25 | 19. String line = value.toString(); |
| 26 | 20. StringTokenizer tokenizer = new StringTokenizer(line); |
| 27 | 21. while (tokenizer.hasMoreTokens()) { |
| 28 | 22. word.set(tokenizer.nextToken()); |
| 29 | 23. output.collect(word, one); |
| 30 | 24. } |
| 31 | 25. } |
| 32 | 26. } |
| 33 | 27. |
| 34 | 28. public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> { |
| 35 | 29. public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException { |
| 36 | 30. int sum = 0; |
| 37 | 31. while (values.hasNext()) { |
| 38 | 32. sum += values.next().get(); |
| 39 | 33. } |
| 40 | 34. output.collect(key, new IntWritable(sum)); |
| 41 | 35. } |
| 42 | 36. } |
28 | | |
29 | | {{{ |
30 | | public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> { |
31 | | public void reduce(Text key, Iterator<IntWritable> values,OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException { |
32 | | int sum = 0; |
33 | | while (values.hasNext()) { |
34 | | sum += values.next().get(); |
35 | | } |
36 | | output.collect(key, new IntWritable(sum)); |
37 | | |
38 | | }}} |