I. background
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