WordCount code in Hadoop-loading Hadoop configuration files directly

Source: Internet
Author: User



WordCount code in Hadoop-loading Hadoop configuration files directly


In MyEclipse, write the WordCount code directly, calling the Core-site.xml,hdfs-site.xml,mapred-site.xml configuration file directly in the code



Package com.apache.hadoop.function;import java.io.ioexception;import java.util.iterator;import  java.util.StringTokenizer;import org.apache.hadoop.fs.Path;import  org.apache.hadoop.io.intwritable;import org.apache.hadoop.io.text;import  org.apache.hadoop.mapred.fileinputformat;import org.apache.hadoop.mapred.fileoutputformat;import  org.apache.hadoop.mapred.jobclient;import org.apache.hadoop.mapred.jobconf;import  org.apache.hadoop.mapred.mapreducebase;import org.apache.hadoop.mapred.mapper;import  org.apache.hadoop.mapred.outputcollector;import org.apache.hadoop.mapred.reducer;import  org.apache.hadoop.mapred.reporter;import org.apache.hadoop.mapred.textinputformat;import  org.apache.hadoop.mapred.textoutputformat;import org.apache.hadoop.mapreduce.mapper.context;public  Class wordcount {public static class wordcountmapper extends mapreducebase  implementsmapper<object, text, text, intwritable> {private final static  Intwritable one = new intwritable (1);p Rivate text word = new text (); @Overridepublic  void map (object key, text value,outputcollector<text,  Intwritable> output, reporter reporter) throws ioexception {stringtokenizer  Itr = new stringtokenizer (Value.tostring ());while  (Itr.hasmoretokens ())  {word.set ( Itr.nexttoken ()); Output.collect (Word, one);}} public static class wordcountreducer extends mapreducebase implementsreducer< text, intwritable, text, intwritable> {private intwritable result =  new intwritable (); @Overridepublic  void reduce (text key, iterator<intwritable > values,outputcollector<text, intwritable> output, reporter&nBsp;reporter) Throws ioexception {// todo auto-generated method stubint sum  = 0;while  (Values.hasnext ())  {sum += values.next (). get ();} Result.set (sum); Output.collect (Key, result);}} Public static void main (String[] args)  throws exception {string input  = null; string output = null; Jobconf conf = new jobconf (Wordcount.class); Conf.setjobname ("WordCount");//  test environment if   (args == null | | &NBSP;ARGS.LENGTH&NBSP;&LT;&NBSP;2)  {input =  "hdfs://192.168.1.220:9000/usr/input/wordcount/*";   //the input file path of the program output =  "Hdfs://192.168.1.220:9000/usr/output/wordcount";   // The output file path of the program Conf.addresource ("Classpath:/hadoop/core-site.xml");   //configuration file needs to be exported from Hadoop, Save to MyEclipse directory Conf.addresource ("Classpath:/hadoop/hdfs-site.xml"); Conf.addresource ("classpath:/hadoop/ Mapred-site.xml ");}  else&nbsP {//  formal environment input = args[0];output = args[1];} Conf.setoutputkeyclass (Text.class); Conf.setoutputvalueclass (Intwritable.class); Conf.setmapperclass ( Wordcountmapper.class); Conf.setcombinerclass (Wordcountreducer.class); Conf.setreducerclass ( Wordcountreducer.class); Conf.setinputformat (Textinputformat.class); Conf.setoutputformat (TextOutputFormat.class ); Fileinputformat.setinputpaths (Conf, new path (input)); Fileoutputformat.setoutputpath (Conf, new path (output)); Jobclient.runjob (conf); System.exit (0);}}





This article is from the "If you bloom, the breeze came in" blog, please be sure to keep this source http://iqdutao.blog.51cto.com/2597934/1792334

WordCount code in Hadoop-loading Hadoop configuration files directly

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.