Changes between Initial Version and Version 1 of NCHCCloudCourse100928_4_EXM6


Ignore:
Timestamp:
Sep 24, 2010, 6:31:58 PM (14 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • NCHCCloudCourse100928_4_EXM6

    v1 v1  
     1{{{
     2#!html
     3<div style="text-align: center; color:#151B8D"><big style="font-weight: bold;"><big><big>
     4Hadoop 進階課程
     5</big></big></big></div> <div style="text-align: center; color:#7E2217"><big style="font-weight: bold;"><big>
     6範例練習
     7</big></big></div>
     8}}}
     9
     10[wiki:NCHCCloudCourse100928_4_EXM5 上一關 < ] 第六關 [wiki:NCHCCloudCourse100928_4_EXM7 > 下一關]
     11
     12
     13{{{
     14#!java
     15package org.nchc.hadoop;
     16import java.io.IOException;
     17import java.util.StringTokenizer;
     18
     19import org.apache.hadoop.conf.Configuration;
     20import org.apache.hadoop.fs.Path;
     21import org.apache.hadoop.io.LongWritable;
     22import org.apache.hadoop.io.Text;
     23import org.apache.hadoop.mapred.OutputCollector;
     24import org.apache.hadoop.mapred.Reporter;
     25import org.apache.hadoop.mapreduce.Job;
     26import org.apache.hadoop.mapreduce.Mapper;
     27import org.apache.hadoop.mapreduce.Reducer;
     28import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
     29import org.apache.hadoop.mapreduce.lib.input.FileSplit;
     30import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
     31import org.apache.hadoop.util.GenericOptionsParser;
     32
     33//      WordIndex
     34//      說明:
     35//              將每個字出於哪個檔案,那一行印出來
     36//
     37//      測試方法:
     38//              將此程式運作在hadoop 0.20 平台上,執行:
     39//              ---------------------------
     40//              hadoop jar WordIndex.jar <input> <output>
     41//              ---------------------------
     42//
     43//      注意:
     44//1.    在hdfs 上來源檔案的路徑為 你所指定的 <input>
     45//請注意必須先放資料到此hdfs上的資料夾內,且此資料夾內只能放檔案,不可再放資料夾
     46//2.    運算完後,程式將執行結果放在hdfs 的輸出路徑為 你所指定的 <output>
     47
     48public class WordIndex {
     49        public static class wordindexM extends
     50                        Mapper<LongWritable, Text, Text, Text> {
     51                public void map(LongWritable key, Text value, Context context)
     52                                throws IOException, InterruptedException {
     53
     54                        FileSplit fileSplit = (FileSplit) context.getInputSplit();
     55
     56                        Text map_key = new Text();
     57                        Text map_value = new Text();
     58                        String line = value.toString();
     59                        StringTokenizer st = new StringTokenizer(line.toLowerCase());
     60                        while (st.hasMoreTokens()) {
     61                                String word = st.nextToken();
     62                                map_key.set(word);
     63                                map_value.set(fileSplit.getPath().getName() + ":" + line);
     64                                context.write(map_key, map_value);
     65                        }
     66                }
     67        }
     68
     69        static public class wordindexR extends Reducer<Text, Text, Text, Text> {
     70
     71                public void reduce(Text key, Iterable<Text> values,
     72                                OutputCollector<Text, Text> output, Reporter reporter)
     73                                throws IOException {
     74                        String v = "";
     75                        StringBuilder ret = new StringBuilder("\n");
     76                        for (Text val : values) {
     77                                v += val.toString().trim();
     78                                if (v.length() > 0)
     79                                        ret.append(v + "\n");
     80                        }
     81
     82                        output.collect((Text) key, new Text(ret.toString()));
     83                }
     84        }
     85
     86        public static void main(String[] args) throws IOException,
     87                        InterruptedException, ClassNotFoundException {
     88//               debug using
     89//               String[] argv = { "/user/hadooper/input", "/user/hadooper/output-wi" };
     90//               args = argv;
     91
     92                Configuration conf = new Configuration();
     93                String[] otherArgs = new GenericOptionsParser(conf, args)
     94                                .getRemainingArgs();
     95                if (otherArgs.length < 2) {
     96                        System.out.println("hadoop jar WordIndex.jar <inDir> <outDir>");
     97                        return;
     98                }
     99                Job job = new Job(conf, "word index");
     100                job.setJobName("word inverted index");
     101                job.setJarByClass(WordIndex.class);
     102
     103                job.setMapOutputKeyClass(Text.class);
     104                job.setMapOutputValueClass(Text.class);
     105                job.setOutputKeyClass(Text.class);
     106                job.setOutputValueClass(Text.class);
     107
     108                job.setMapperClass(wordindexM.class);
     109                job.setReducerClass(wordindexR.class);
     110                job.setCombinerClass(wordindexR.class);
     111
     112                FileInputFormat.setInputPaths(job, args[0]);
     113                CheckAndDelete.checkAndDelete(args[1], conf);
     114                FileOutputFormat.setOutputPath(job, new Path(args[1]));
     115
     116                long start = System.nanoTime();
     117
     118                job.waitForCompletion(true);
     119
     120                long time = System.nanoTime() - start;
     121                System.err.println(time * (1E-9) + " secs.");
     122        }
     123}
     124
     125}}}
     126
     127