Changes between Initial Version and Version 1 of waue/2010/0115-WordCountV1


Ignore:
Timestamp:
Jan 19, 2010, 7:49:29 PM (14 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • waue/2010/0115-WordCountV1

    v1 v1  
     1 = Word Count V1 =
     2
     3{{{
     4#!java
     5
     6import java.io.IOException;
     7import java.util.StringTokenizer;
     8
     9import org.apache.hadoop.conf.Configuration;
     10import org.apache.hadoop.fs.Path;
     11import org.apache.hadoop.io.IntWritable;
     12import org.apache.hadoop.io.Text;
     13import org.apache.hadoop.mapreduce.Job;
     14import org.apache.hadoop.mapreduce.Mapper;
     15import org.apache.hadoop.mapreduce.Reducer;
     16import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
     17import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
     18import org.apache.hadoop.util.GenericOptionsParser;
     19//WordCount
     20//說明:
     21//      用於字數統計
     22//
     23//測試方法:
     24//      將此程式運作在hadoop 0.20 平台上,執行:
     25//      ---------------------------
     26//      hadoop jar WordCount.jar <input> <output>
     27//      ---------------------------
     28//
     29//注意:
     30//1.    在hdfs 上來源檔案的路徑為 你所指定的 <input>
     31//請注意必須先放資料到此hdfs上的資料夾內,且此資料夾內只能放檔案,不可再放資料夾
     32//2.    運算完後,程式將執行結果放在hdfs 的輸出路徑為 你所指定的 <output>
     33//     
     34public class WordCount {
     35
     36        public static class TokenizerMapper extends
     37                        Mapper<Object, Text, Text, IntWritable> {
     38
     39                private final static IntWritable one = new IntWritable(1);
     40                private Text word = new Text();
     41
     42                public void map(Object key, Text value, Context context)
     43                                throws IOException, InterruptedException {
     44                        StringTokenizer itr = new StringTokenizer(value.toString());
     45                        while (itr.hasMoreTokens()) {
     46                                word.set(itr.nextToken());
     47                                context.write(word, one);
     48                        }
     49                }
     50        }
     51
     52        public static class IntSumReducer extends
     53                        Reducer<Text, IntWritable, Text, IntWritable> {
     54                private IntWritable result = new IntWritable();
     55
     56                public void reduce(Text key, Iterable<IntWritable> values,
     57                                Context context) throws IOException, InterruptedException {
     58                        int sum = 0;
     59                        for (IntWritable val : values) {
     60                                sum += val.get();
     61                        }
     62                        result.set(sum);
     63                        context.write(key, result);
     64                }
     65        }
     66
     67        public static void main(String[] args) throws Exception {
     68                // debug using
     69//              String[] argv = { "input", "output-wc" };
     70//              args = argv;
     71               
     72                Configuration conf = new Configuration();
     73               
     74                String[] otherArgs = new GenericOptionsParser(conf, args)
     75                                .getRemainingArgs();
     76                if (otherArgs.length != 2) {
     77                        System.err
     78                                        .println("Usage: hadoop jar WordCount.jar <input> <output>");
     79                        System.exit(2);
     80                }
     81
     82                Job job = new Job(conf, "Word Count");
     83                job.setJarByClass(WordCount.class);
     84                job.setMapperClass(TokenizerMapper.class);
     85                job.setCombinerClass(IntSumReducer.class);
     86                job.setReducerClass(IntSumReducer.class);
     87                job.setOutputKeyClass(Text.class);
     88                job.setOutputValueClass(IntWritable.class);
     89                FileInputFormat.addInputPath(job, new Path(args[0]));
     90                FileOutputFormat.setOutputPath(job, new Path(args[1]));
     91                CheckAndDelete.checkAndDelete(args[1], conf);
     92                System.exit(job.waitForCompletion(true) ? 0 : 1);
     93        }
     94}
     95
     96}}}