Changes between Version 5 and Version 6 of waue/2010/0118


Ignore:
Timestamp:
Jan 19, 2010, 11:29:36 AM (14 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • waue/2010/0118

    v5 v6  
    66{{{
    77#!java
    8         public static class wordindexM extends
    9                         Mapper<LongWritable, Text, Text, Text> {
    10                 public void map(LongWritable key, Text value,
    11                                 OutputCollector<Text, Text> output, Reporter reporter)
    12                                 throws IOException {
     8import java.io.IOException;
     9import java.util.StringTokenizer;
    1310
    14                         FileSplit fileSplit = (FileSplit) reporter.getInputSplit();
    15                        
    16                         String line = value.toString();
    17                         StringTokenizer st = new StringTokenizer(line.toLowerCase());
    18                         while (st.hasMoreTokens()) {
    19                                 String word = st.nextToken();
    20                                 output.collect(new Text(word), new Text(fileSplit.getPath()
    21                                                 .getName()
    22                                                 + ":" + line));
    23                         }
    24                 }
    25         }
    26 }}}
     11import org.apache.hadoop.conf.Configuration;
     12import org.apache.hadoop.fs.Path;
     13import org.apache.hadoop.io.LongWritable;
     14import org.apache.hadoop.io.Text;
     15import org.apache.hadoop.mapred.OutputCollector;
     16import org.apache.hadoop.mapred.Reporter;
     17import org.apache.hadoop.mapreduce.Job;
     18import org.apache.hadoop.mapreduce.Mapper;
     19import org.apache.hadoop.mapreduce.Reducer;
     20import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
     21import org.apache.hadoop.mapreduce.lib.input.FileSplit;
     22import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
     23import org.apache.hadoop.util.GenericOptionsParser;
    2724
    28 遇到問題:
    29 {{{
    30 #!text
    31 10/01/18 20:52:39 INFO input.FileInputFormat: Total input paths to process : 2
    32 10/01/18 20:52:39 INFO mapred.JobClient: Running job: job_201001181452_0038
    33 10/01/18 20:52:40 INFO mapred.JobClient:  map 0% reduce 0%
    34 10/01/18 20:52:50 INFO mapred.JobClient: Task Id : attempt_201001181452_0038_m_000000_0, Status : FAILED
    35 java.io.IOException: Type mismatch in key from map: expected org.apache.hadoop.io.Text, recieved org.apache.hadoop.io.LongWritable
    36 
    37 }}}
    38 
    39  * 已解決
    40 
    41 {{{
    42 #!java
    43         public static class wordindexM extends
     25public class WordIndex {
     26         public static class wordindexM extends
    4427                        Mapper<LongWritable, Text, Text, Text> {
    4528                public void map(LongWritable key, Text value,
     
    5639                                map_key.set(word);
    5740                                map_value.set(fileSplit.getPath().getName() + ":" + line);
    58                                 output.collect(map_key,map_value);
     41                                output.collect(map_key, map_value);
    5942                        }
    6043                }
    61       }
     44        }
     45
     46
     47        static public class wordindexR extends Reducer<Text, Text, Text, Text> {
     48
     49                public void reduce(Text key, Iterable<Text> values,
     50                                OutputCollector<Text, Text> output, Reporter reporter)
     51                                throws IOException {
     52                        String v = "";
     53                        StringBuilder ret = new StringBuilder("\n");
     54                        for (Text val : values) {
     55                                v += val.toString().trim();
     56                                if (v.length() > 0)
     57                                        ret.append(v + "\n");
     58                        }
     59
     60                        output.collect((Text) key, new Text(ret.toString()));
     61                }
     62        }
     63
     64        public static void main(String[] args) throws IOException,
     65                        InterruptedException, ClassNotFoundException {
     66                // debug using
     67                String[] argv = { "/user/waue/input", "/user/waue/output-wi" };
     68                args = argv;
     69
     70                Configuration conf = new Configuration();
     71                String[] otherArgs = new GenericOptionsParser(conf, args)
     72                                .getRemainingArgs();
     73                if (otherArgs.length < 2) {
     74                        System.out.println("hadoop jar WordIndex.jar <inDir> <outDir>");
     75                        return;
     76                }
     77                Job job = new Job(conf, "word index");
     78                job.setJobName("word inverted index");
     79                job.setJarByClass(WordIndex.class);
     80
     81                job.setMapOutputKeyClass(Text.class);
     82                job.setMapOutputValueClass(Text.class);
     83                job.setOutputKeyClass(Text.class);
     84                job.setOutputValueClass(Text.class);
     85
     86                job.setMapperClass(wordindexM.class);
     87                job.setReducerClass(wordindexR.class);
     88                job.setCombinerClass(wordindexR.class);
     89
     90                FileInputFormat.setInputPaths(job, args[0]);
     91                FileOutputFormat.setOutputPath(job, new Path(args[1]));
     92
     93                long start = System.nanoTime();
     94
     95                job.waitForCompletion(true);
     96
     97                long time = System.nanoTime() - start;
     98                System.err.println(time * (1E-9) + " secs.");
     99        }
     100}
     101
    62102}}}
    63103
    64  * 解析
     104遇到問題:
     105{{{
     106#!text
     10710/01/18 20:52:39 INFO input.FileInputFormat: Total input paths to process : 2
     10810/01/18 20:52:39 INFO mapred.JobClient: Running job: job_201001181452_0038
     10910/01/18 20:52:40 INFO mapred.JobClient:  map 0% reduce 0%
     11010/01/18 20:52:50 INFO mapred.JobClient: Task Id : attempt_201001181452_0038_m_000000_0, Status : FAILED
     111java.io.IOException: Type mismatch in key from map: expected org.apache.hadoop.io.Text, recieved org.apache.hadoop.io.LongWritable
    65112
    66 用output.collect寫入輸出串流,''' new Text(word) ''' 感覺用這個方法就可以把 Text 型態的資料寫入,
     113}}}
    67114
    68 hadoop 0.18 可以這麼做不會出問題,但在 hadoop 0.20 之後,如果遇到''' " Type mismatch in key from xxx " ''' 問題時
     115 * 目前認為是
    69116
    70 可以換成用 Text.set() 方法來解決問題 !
     117{{{
     118#!java
     119         public static class wordindexM extends
     120                        Mapper<LongWritable, Text, Text, Text> {
     121                public void map(LongWritable key, Text value,
     122                                OutputCollector<Text, Text> output, Reporter reporter)
     123                                throws IOException {
     124}}}
    71125
     126用了新的 org.apache.hadoop.mapreduce.Mapper  可以省略掉不用 extends ..  implements ... 的宣告,
     127
     128不過它預設搭配的map 實做方法是 ''' "public void map(LongWritable key, Text value, Context context) " ''' 而非
     129'''  "public void map(LongWritable key, Text value, OutputCollector<Text, Text> output, Reporter reporter) " '''
     130
     131
     132 * 解決辦法
     133
     134