Hadoop--08--wordcount

Source: Internet
Author: User

<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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.