<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >import java.io.IOException; Import java.util.*; Import Org.apache.hadoop.fs.Path; Import org.apache.hadoop.conf.*; Import org.apache.hadoop.io.*; Import org.apache.hadoop.mapred.*; Import org.apache.hadoop.util.*; public class WordCount { </span>
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><span style= "White-space:pre" ></span>public static class Wordcountmap extends Mapreducebase Implements Mapper<longwritable, text, text, intwritable> {Private final static intwritable one = new Intwritable (1 ); Private text Word = new text (); public void Map (longwritable key, Text value, Outputcollector<text, intwritable> output, Reporter Reporter) throws I oexception {String line = value.tostring (); StringTokenizer tokenizer = new StringTokenizer (line); while (Tokenizer.hasmoretokens ()) {Word.set (Tokenizer.nexttoken ()); Output.collect (Word, one); }}} public static class Wordcountreduce extends Mapreducebase implements Reducer<text, Intwritable, Text, Intwrita ble> {public void reduce (Text key, iterator<intwritable> values, Outputcollector<text, intwritable> OUTP UT, Reporter Reporter) throws IOException {int sum = 0; while (Values.hasnext ()) {sum + = ValuEs.next (). get (); } output.collect (Key, New intwritable (sum)); }} public static void Main (string[] args) throws Exception {jobconf conf = new jobconf (wordcount.class); Conf.setjobname ("WordCount"); Conf.setoutputkeyclass (Text.class); Conf.setoutputvalueclass (Intwritable.class); Conf.setmapperclass (Map.class); Conf.setreducerclass (Reduce.class); Conf.setinputformat (Textinputformat.class); Conf.setoutputformat (Textoutputformat.class); Fileinputformat.setinputpaths (conf, new Path (Args[0])); Fileoutputformat.setoutputpath (conf, new Path (Args[1])); Jobclient.runjob (conf); }} </span> Program Analysis
1, Wordcountmap class inherits org.apache.hadoop.mapreduce.mapper,4 generic type is the type of the map function input key, enter the type of value, output key type, output value type.
2. The Wordcountreduce class inherits the same org.apache.hadoop.mapreduce.reducer,4 generic type meaning as the map class. 3. The output type of map is the same as the input type of reduce, and in general, the output type of map is the same as the output type of reduce, so the input type of reduce is the same as the output type.
4. Hadoop determines the format of the input content according to the following code:Job.setinputformatclass (textinputformat.class);Textinputformat is the default input method for Hadoop, which inherits from Fileinputformat. In Textinputformat, it cuts data sets into small datasets inputsplit, each inputsplit handled by a single mapper. In addition, InputFormat provides a recordreader implementation that parses a inputsplit into the form of <key,value> and provides it to the map function:key: The data type is longwritable relative to the byte offset in the data shard. value: The content of each row of data, the type is text. So, in this case, the Key/value type of the map function is longwritable and text.
5. Hadoop determines the format of the output content according to the following code:Job.setoutputformatclass (textoutputformat.class);Textoutputformat is the default output format for Hadoop, which stores one row of records in a text file, such as theHappy...
Hadoop--08--wordcount