Use multipleoutputformat of hadoop

Source: Internet
Author: User

I. background

Multi-file output in hadoop mapreduce is textoutformat by default, and the output is the file name in ascending order of part-r-00000 and part-r-00001. Hadoop provides

Multipleoutputformat class. You can override this class to customize custom file names.

Ii. Technical Details

1. Environment: hadoop 0.19 (currently hadoop 0.20.2 does not support multipleoutputformat well), Linux.

2. The code example for implementing multipleoutputformat is as follows:

 

Public class wordcount {public static class tokenizermapper extends mapreducebase implements mapper <longwritable, text, text, intwritable> {private final static intwritable COUNT = new intwritable (1 ); private text word = new text (); Public void map (longwritable key, text value, outputcollector <text, intwritable> output, reporter) throws ioexception {stringtokenizer itr = new stringtoken Izer (value. tostring (); While (itr. hasmoretokens () {word. set (itr. nexttoken (); output. collect (word, count) ;}} public static class intsumreducer extends mapreducebase implements extends CER <text, intwritable, text, intwritable> {private intwritable result = new intwritable (); public void reduce (Text key, iterator <intwritable> values, outputcollector <text, intwritable> output, reporter) Throws ioexception {int sum = 0; while (values. hasnext () {sum + = values. next (). get ();} result. set (SUM); output. collect (Key, result) ;}} public static class wordcountoutputformat extends multipleoutputformat <text, intwritable> {private textoutputformat <text, intwritable> output = NULL; @ override protected recordwriter <text, intwritable> getbaserecordwriter (filesystem FS, jobconf job, string Name, progressable arg3) throws ioexception {If (output = NULL) {output = new textoutputformat <text, intwritable> ();} return output. getrecordwriter (FS, job, name, arg3) ;}@ override protected string generatefilenameforkeyvalue (Text key, intwritable value, string name) {char c = key. tostring (). tolowercase (). charat (0); If (C> = 'A' & C <= 'Z') {return C + ". TXT ";} return" result.txt ";} Pu BLIC static void main (string [] ARGs) throws exception {jobconf job = new jobconf (wordcount. class); job. setjobname ("wordcount"); string [] otherargs = new genericoptionsparser (job, argS ). getremainingargs (); If (otherargs. length! = 2) {system. err. println ("Usage: wordcount <in> <out>"); system. exit (2);} job. setjarbyclass (wordcount. class); job. setmapperclass (tokenizermapper. class); job. setcombinerclass (intsumreducer. class); job. setreducerclass (intsumreducer. class); job. setoutputkeyclass (text. class); job. setoutputvalueclass (intwritable. class); job. setoutputformat (wordcountoutputformat. class); // sets the output format fileinputformat. addinputpath (job, new path (otherargs [0]); fileoutputformat. setoutputpath (job, new path (otherargs [1]); jobclient. runjob (job );}}

3. set the output format in the main function, Job. setoutputformat (wordcountoutputformat. class); Implement the wordcountoutputformat class to inherit the multipleoutputformat class, override the getbaserecordwriter and generatefilenameforkeyvalue functions, in the generatefilenameforkeyvalue function, the parameter string name is the default output part-00000:

public static class WordCountOutputFormat extends        MultipleOutputFormat<Text, IntWritable> {     private TextOutputFormat<Text, IntWritable> output = null;     @Override     protected RecordWriter<Text, IntWritable> getBaseRecordWriter(          FileSystem fs, JobConf job, String name, Progressable arg3)          throws IOException {        if (output == null) {          output = new TextOutputFormat<Text, IntWritable>();        }        return output.getRecordWriter(fs, job, name, arg3);     }     @Override     protected String generateFileNameForKeyValue(Text key,          IntWritable value, String name) {        char c = key.toString().toLowerCase().charAt(0);        if (c >= 'a' && c <= 'z') {          return c + ".txt";        }        return "result.txt";     }    }

4. The program result is:

-rw-r--r--   2 root supergroup          7 2010-08-07 17:44  /hua/multipleoutput1/c.txt-rw-r--r--   2 root supergroup          6 2010-08-07 17:44  /hua/multipleoutput1/h.txt-rw-r--r--   2 root supergroup          7 2010-08-07 17:44  /hua/multipleoutput1/k.txt-rw-r--r--   2 root supergroup          6 2010-08-07 17:44  /hua/multipleoutput1/m.txt-rw-r--r--   2 root supergroup         28 2010-08-07 17:44  /hua/multipleoutput1/result.txt-rw-r--r--   2 root supergroup          6 2010-08-07 17:44  /hua/multipleoutput1/t.txt

If generatefilenameforkeyvalue is returned, return C + "_" + name + ". txt"; the result is:

-rw-r--r--   2 root supergroup          7 2010-08-07 17:23  /hua/multipleoutput/c_part-00000.txt-rw-r--r--   2 root supergroup          6 2010-08-07 17:23  /hua/multipleoutput/h_part-00000.txt-rw-r--r--   2 root supergroup          7 2010-08-07 17:23  /hua/multipleoutput/k_part-00000.txt-rw-r--r--   2 root supergroup          6 2010-08-07 17:23  /hua/multipleoutput/m_part-00000.txt-rw-r--r--   2 root supergroup         28 2010-08-07 17:23  /hua/multipleoutput/result.txt-rw-r--r--   2 root supergroup          6 2010-08-07 17:23  /hua/multipleoutput/t_part-00000.txt
Iii. Summary
Although the API uses 0.19, but the API that uses 0.20 is equally available, it only prompts that the method is outdated.

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.