| 1 | {{{ |
| 2 | #!java |
| 3 | |
| 4 | package org.nchc.hadoop; |
| 5 | |
| 6 | import java.io.IOException; |
| 7 | import java.util.StringTokenizer; |
| 8 | |
| 9 | import org.apache.hadoop.conf.Configuration; |
| 10 | import org.apache.hadoop.filecache.DistributedCache; |
| 11 | import org.apache.hadoop.fs.Path; |
| 12 | import org.apache.hadoop.io.IntWritable; |
| 13 | import org.apache.hadoop.io.Text; |
| 14 | import org.apache.hadoop.mapreduce.Job; |
| 15 | import org.apache.hadoop.mapreduce.Mapper; |
| 16 | import org.apache.hadoop.mapreduce.Reducer; |
| 17 | import org.apache.hadoop.mapreduce.Mapper.Context; |
| 18 | import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; |
| 19 | import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; |
| 20 | import org.apache.hadoop.util.GenericOptionsParser; |
| 21 | import org.apache.hadoop.util.StringUtils; |
| 22 | |
| 23 | public class WordCountV { |
| 24 | |
| 25 | public static class TokenizerMapper extends |
| 26 | Mapper<Object, Text, Text, IntWritable> { |
| 27 | |
| 28 | private final static IntWritable one = new IntWritable(1); |
| 29 | private Text word = new Text(); |
| 30 | |
| 31 | |
| 32 | public void setup(Context context) { |
| 33 | Configuration conf = context.getConfiguration(); |
| 34 | System.err.println(conf.get("mapred.job.tracker")); |
| 35 | |
| 36 | } |
| 37 | |
| 38 | public void map(Object key, Text value, Context context) |
| 39 | throws IOException, InterruptedException { |
| 40 | StringTokenizer itr = new StringTokenizer(value.toString()); |
| 41 | while (itr.hasMoreTokens()) { |
| 42 | word.set(itr.nextToken()); |
| 43 | context.write(word, one); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | public static class IntSumReducer extends |
| 49 | Reducer<Text, IntWritable, Text, IntWritable> { |
| 50 | private IntWritable result = new IntWritable(); |
| 51 | |
| 52 | public void reduce(Text key, Iterable<IntWritable> values, |
| 53 | Context context) throws IOException, InterruptedException { |
| 54 | int sum = 0; |
| 55 | for (IntWritable val : values) { |
| 56 | sum += val.get(); |
| 57 | } |
| 58 | result.set(sum); |
| 59 | context.write(key, result); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public static void main(String[] args) throws Exception { |
| 64 | String[] argv = { "/home/nchc/input", "/home/nchc/output-wc3" }; |
| 65 | args = argv; |
| 66 | Configuration conf = new Configuration(); |
| 67 | conf.set("mapred.job.tracker", "local"); // for single |
| 68 | conf.set("fs.default.name", "file:///"); // for single |
| 69 | |
| 70 | String[] otherArgs = new GenericOptionsParser(conf, args) |
| 71 | .getRemainingArgs(); |
| 72 | if (otherArgs.length != 2) { |
| 73 | System.err |
| 74 | .println("Usage: hadoop jar WordCount.jar <input> <output>"); |
| 75 | System.exit(2); |
| 76 | } |
| 77 | CheckAndDelete.checkAndDelete(args[1], conf); |
| 78 | Job job = new Job(conf, "Word Count"); |
| 79 | job.setJarByClass(WordCountV.class); |
| 80 | job.setMapperClass(TokenizerMapper.class); |
| 81 | job.setCombinerClass(IntSumReducer.class); |
| 82 | job.setReducerClass(IntSumReducer.class); |
| 83 | job.setOutputKeyClass(Text.class); |
| 84 | job.setOutputValueClass(IntWritable.class); |
| 85 | FileInputFormat.addInputPath(job, new Path(args[0])); |
| 86 | FileOutputFormat.setOutputPath(job, new Path(args[1])); |
| 87 | |
| 88 | System.exit(job.waitForCompletion(true) ? 0 : 1); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | }}} |