如何使用Hadoop的MultipleOutputs进行多文件输出

来源:互联网 发布:java数组的用法 编辑:程序博客网 时间:2024/06/06 00:38
有时候,我们使用Hadoop处理数据时,在Reduce阶段,我们可能想对每一个输出的key进行单独输出一个目录或文件,这样方便数据分析,比如根据某个时间段对日志文件进行时间段归类等等。这时候我们就可以使用MultipleOutputs类,来搞定这件事,

下面,先来看下散仙的测试数据:

Java代码  收藏代码
  1. 中国;我们  
  2. 美国;他们  
  3. 中国;123  
  4. 中国人;善良  
  5. 美国;USA  
  6. 美国;在北美洲  

输出结果:预期输出结果是:
中国一组,美国一组,中国人一组
核心代码如下:

Java代码  收藏代码
  1. package com.partition.test;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.hadoop.fs.FileSystem;  
  6. import org.apache.hadoop.fs.Path;  
  7. import org.apache.hadoop.io.LongWritable;  
  8. import org.apache.hadoop.io.Text;  
  9. import org.apache.hadoop.mapred.JobConf;  
  10. import org.apache.hadoop.mapreduce.Job;  
  11. import org.apache.hadoop.mapreduce.Mapper;  
  12. import org.apache.hadoop.mapreduce.Partitioner;  
  13. import org.apache.hadoop.mapreduce.Reducer;  
  14. import org.apache.hadoop.mapreduce.lib.db.DBConfiguration;  
  15. import org.apache.hadoop.mapreduce.lib.db.DBInputFormat;  
  16. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  17. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  18. import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;  
  19. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;  
  20.   
  21. import com.qin.operadb.PersonRecoder;  
  22. import com.qin.operadb.ReadMapDB;  
  23.    
  24. /*** 
  25.  * @author qindongliang 
  26.  *  
  27.  * 大数据技术交流群:324714439 
  28.  * **/  
  29. public class TestMultiOutput {  
  30.       
  31.       
  32.     /** 
  33.      * map任务 
  34.      *  
  35.      * **/  
  36.     public static class PMapper extends Mapper<LongWritable, Text, Text, Text>{  
  37.           
  38.         @Override  
  39.         protected void map(LongWritable key, Text value,Context context)  
  40.                 throws IOException, InterruptedException {  
  41.              String ss[]=value.toString().split(";");  
  42.             context.write(new Text(ss[0]), new Text(ss[1]));      
  43.         }  
  44.           
  45.           
  46.     }  
  47.       
  48.    
  49.      public static class PReduce extends Reducer<Text, Text, Text, Text>{  
  50.          /** 
  51.           * 设置多个文件输出 
  52.           * */  
  53.          private MultipleOutputs mos;  
  54.            
  55.          @Override  
  56.         protected void setup(Context context)  
  57.                 throws IOException, InterruptedException {  
  58.               mos=new MultipleOutputs(context);//初始化mos  
  59.         }  
  60.          @Override  
  61.         protected void reduce(Text arg0, Iterable<Text> arg1, Context arg2)  
  62.                 throws IOException, InterruptedException {  
  63.                
  64.               String key=arg0.toString();  
  65.              for(Text t:arg1){  
  66.                    if(key.equals("中国")){   
  67.                        /** 
  68.                         * 一个参数 
  69.                         * **/  
  70.                        mos.write("china", arg0,t);   
  71.                    } else if(key.equals("美国")){  
  72.                        mos.write("USA", arg0,t);      
  73.                    } else if(key.equals("中国人")){  
  74.                        mos.write("cperson", arg0,t);   
  75.                          
  76.                    }  
  77.            
  78.                  //System.out.println("Reduce:  "+arg0.toString()+"   "+t.toString());  
  79.              }  
  80.                  
  81.                
  82.         }  
  83.            
  84.          @Override  
  85.         protected void cleanup(  
  86.                  Context context)  
  87.                 throws IOException, InterruptedException {  
  88.              mos.close();//释放资源  
  89.         }  
  90.            
  91.      }  
  92.        
  93.        
  94.      public static void main(String[] args) throws Exception{  
  95.          JobConf conf=new JobConf(ReadMapDB.class);  
  96.          //Configuration conf=new Configuration();  
  97.         // conf.set("mapred.job.tracker","192.168.75.130:9001");  
  98.         //读取person中的数据字段  
  99.         // conf.setJar("tt.jar");  
  100.         //注意这行代码放在最前面,进行初始化,否则会报  
  101.        
  102.        
  103.         /**Job任务**/  
  104.         Job job=new Job(conf, "testpartion");  
  105.         job.setJarByClass(TestMultiOutput.class);  
  106.         System.out.println("模式:  "+conf.get("mapred.job.tracker"));;  
  107.         // job.setCombinerClass(PCombine.class);  
  108.         //job.setPartitionerClass(PPartition.class);  
  109.         //job.setNumReduceTasks(5);  
  110.          job.setMapperClass(PMapper.class);  
  111.            
  112.          /** 
  113.           * 注意在初始化时需要设置输出文件的名 
  114.           * 另外名称,不支持中文名,仅支持英文字符 
  115.           *  
  116.           * **/  
  117.          MultipleOutputs.addNamedOutput(job, "china", TextOutputFormat.class, Text.class, Text.class);  
  118.          MultipleOutputs.addNamedOutput(job, "USA", TextOutputFormat.class, Text.class, Text.class);  
  119.          MultipleOutputs.addNamedOutput(job, "cperson", TextOutputFormat.class, Text.class, Text.class);  
  120.          job.setReducerClass(PReduce.class);  
  121.          job.setOutputKeyClass(Text.class);  
  122.          job.setOutputValueClass(Text.class);  
  123.           
  124.         String path="hdfs://192.168.75.130:9000/root/outputdb";  
  125.         FileSystem fs=FileSystem.get(conf);  
  126.         Path p=new Path(path);  
  127.         if(fs.exists(p)){  
  128.             fs.delete(p, true);  
  129.             System.out.println("输出路径存在,已删除!");  
  130.         }  
  131.         FileInputFormat.setInputPaths(job, "hdfs://192.168.75.130:9000/root/input");  
  132.         FileOutputFormat.setOutputPath(job,p );  
  133.         System.exit(job.waitForCompletion(true) ? 0 : 1);    
  134.            
  135.            
  136.     }  
  137.       
  138.       
  139.   
  140. }  

如果是中文的路径名,则会报如下的一个异常:
Java代码  收藏代码
  1. 模式:  local  
  2. 输出路径存在,已删除!  
  3. WARN - NativeCodeLoader.<clinit>(52) | Unable to load native-hadoop library for your platform... using builtin-java classes where applicable  
  4. WARN - JobClient.copyAndConfigureFiles(746) | Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.  
  5. WARN - JobClient.copyAndConfigureFiles(870) | No job jar file set.  User classes may not be found. See JobConf(Class) or JobConf#setJar(String).  
  6. INFO - FileInputFormat.listStatus(237) | Total input paths to process : 1  
  7. WARN - LoadSnappy.<clinit>(46) | Snappy native library not loaded  
  8. INFO - JobClient.monitorAndPrintJob(1380) | Running job: job_local1533332464_0001  
  9. INFO - LocalJobRunner$Job.run(340) | Waiting for map tasks  
  10. INFO - LocalJobRunner$Job$MapTaskRunnable.run(204) | Starting task: attempt_local1533332464_0001_m_000000_0  
  11. INFO - Task.initialize(534) |  Using ResourceCalculatorPlugin : null  
  12. INFO - MapTask.runNewMapper(729) | Processing split: hdfs://192.168.75.130:9000/root/input/group.txt:0+91  
  13. INFO - MapTask$MapOutputBuffer.<init>(949) | io.sort.mb = 100  
  14. INFO - MapTask$MapOutputBuffer.<init>(961) | data buffer = 79691776/99614720  
  15. INFO - MapTask$MapOutputBuffer.<init>(962) | record buffer = 262144/327680  
  16. INFO - MapTask$MapOutputBuffer.flush(1289) | Starting flush of map output  
  17. INFO - MapTask$MapOutputBuffer.sortAndSpill(1471) | Finished spill 0  
  18. INFO - Task.done(858) | Task:attempt_local1533332464_0001_m_000000_0 is done. And is in the process of commiting  
  19. INFO - LocalJobRunner$Job.statusUpdate(466) |   
  20. INFO - Task.sendDone(970) | Task 'attempt_local1533332464_0001_m_000000_0' done.  
  21. INFO - LocalJobRunner$Job$MapTaskRunnable.run(229) | Finishing task: attempt_local1533332464_0001_m_000000_0  
  22. INFO - LocalJobRunner$Job.run(348) | Map task executor complete.  
  23. INFO - Task.initialize(534) |  Using ResourceCalculatorPlugin : null  
  24. INFO - LocalJobRunner$Job.statusUpdate(466) |   
  25. INFO - Merger$MergeQueue.merge(408) | Merging 1 sorted segments  
  26. INFO - Merger$MergeQueue.merge(491) | Down to the last merge-pass, with 1 segments left of total size: 101 bytes  
  27. INFO - LocalJobRunner$Job.statusUpdate(466) |   
  28. WARN - LocalJobRunner$Job.run(435) | job_local1533332464_0001  
  29. java.lang.IllegalArgumentException: Name cannot be have a '一' char  
  30.     at org.apache.hadoop.mapreduce.lib.output.MultipleOutputs.checkTokenName(MultipleOutputs.java:160)  
  31.     at org.apache.hadoop.mapreduce.lib.output.MultipleOutputs.checkNamedOutputName(MultipleOutputs.java:186)  
  32.     at org.apache.hadoop.mapreduce.lib.output.MultipleOutputs.write(MultipleOutputs.java:363)  
  33.     at org.apache.hadoop.mapreduce.lib.output.MultipleOutputs.write(MultipleOutputs.java:348)  
  34.     at com.partition.test.TestMultiOutput$PReduce.reduce(TestMultiOutput.java:74)  
  35.     at com.partition.test.TestMultiOutput$PReduce.reduce(TestMultiOutput.java:1)  
  36.     at org.apache.hadoop.mapreduce.Reducer.run(Reducer.java:177)  
  37.     at org.apache.hadoop.mapred.ReduceTask.runNewReducer(ReduceTask.java:649)  
  38.     at org.apache.hadoop.mapred.ReduceTask.run(ReduceTask.java:418)  
  39.     at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:398)  
  40. INFO - JobClient.monitorAndPrintJob(1393) |  map 100% reduce 0%  
  41. INFO - JobClient.monitorAndPrintJob(1448) | Job complete: job_local1533332464_0001  
  42. INFO - Counters.log(585) | Counters: 17  
  43. INFO - Counters.log(587) |   File Input Format Counters   
  44. INFO - Counters.log(589) |     Bytes Read=91  
  45. INFO - Counters.log(587) |   FileSystemCounters  
  46. INFO - Counters.log(589) |     FILE_BYTES_READ=177  
  47. INFO - Counters.log(589) |     HDFS_BYTES_READ=91  
  48. INFO - Counters.log(589) |     FILE_BYTES_WRITTEN=71111  
  49. INFO - Counters.log(587) |   Map-Reduce Framework  
  50. INFO - Counters.log(589) |     Map output materialized bytes=105  
  51. INFO - Counters.log(589) |     Map input records=6  
  52. INFO - Counters.log(589) |     Reduce shuffle bytes=0  
  53. INFO - Counters.log(589) |     Spilled Records=6  
  54. INFO - Counters.log(589) |     Map output bytes=87  
  55. INFO - Counters.log(589) |     Total committed heap usage (bytes)=227737600  
  56. INFO - Counters.log(589) |     Combine input records=0  
  57. INFO - Counters.log(589) |     SPLIT_RAW_BYTES=112  
  58. INFO - Counters.log(589) |     Reduce input records=0  
  59. INFO - Counters.log(589) |     Reduce input groups=0  
  60. INFO - Counters.log(589) |     Combine output records=0  
  61. INFO - Counters.log(589) |     Reduce output records=0  
  62. INFO - Counters.log(589) |     Map output records=6  


源码中关于名称的校验如下:
Java代码  收藏代码
  1. /** 
  2.   * Checks if a named output name is valid token. 
  3.   * 
  4.   * @param namedOutput named output Name 
  5.   * @throws IllegalArgumentException if the output name is not valid. 
  6.   */  
  7.  private static void checkTokenName(String namedOutput) {  
  8.    if (namedOutput == null || namedOutput.length() == 0) {  
  9.      throw new IllegalArgumentException(  
  10.        "Name cannot be NULL or emtpy");  
  11.    }  
  12.    for (char ch : namedOutput.toCharArray()) {  
  13.      if ((ch >= 'A') && (ch <= 'Z')) {  
  14.        continue;  
  15.      }  
  16.      if ((ch >= 'a') && (ch <= 'z')) {  
  17.        continue;  
  18.      }  
  19.      if ((ch >= '0') && (ch <= '9')) {  
  20.        continue;  
  21.      }  
  22.      throw new IllegalArgumentException(  
  23.        "Name cannot be have a '" + ch + "' char");  
  24.    }  
  25.  }  

程序运行成功输出:
Java代码  收藏代码
  1. 模式:  192.168.75.130:9001  
  2. 输出路径存在,已删除!  
  3. WARN - JobClient.copyAndConfigureFiles(746) | Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.  
  4. INFO - FileInputFormat.listStatus(237) | Total input paths to process : 1  
  5. WARN - NativeCodeLoader.<clinit>(52) | Unable to load native-hadoop library for your platform... using builtin-java classes where applicable  
  6. WARN - LoadSnappy.<clinit>(46) | Snappy native library not loaded  
  7. INFO - JobClient.monitorAndPrintJob(1380) | Running job: job_201404101853_0006  
  8. INFO - JobClient.monitorAndPrintJob(1393) |  map 0% reduce 0%  
  9. INFO - JobClient.monitorAndPrintJob(1393) |  map 100% reduce 0%  
  10. INFO - JobClient.monitorAndPrintJob(1393) |  map 100% reduce 33%  
  11. INFO - JobClient.monitorAndPrintJob(1393) |  map 100% reduce 100%  
  12. INFO - JobClient.monitorAndPrintJob(1448) | Job complete: job_201404101853_0006  
  13. INFO - Counters.log(585) | Counters: 29  
  14. INFO - Counters.log(587) |   Job Counters   
  15. INFO - Counters.log(589) |     Launched reduce tasks=1  
  16. INFO - Counters.log(589) |     SLOTS_MILLIS_MAPS=9289  
  17. INFO - Counters.log(589) |     Total time spent by all reduces waiting after reserving slots (ms)=0  
  18. INFO - Counters.log(589) |     Total time spent by all maps waiting after reserving slots (ms)=0  
  19. INFO - Counters.log(589) |     Launched map tasks=1  
  20. INFO - Counters.log(589) |     Data-local map tasks=1  
  21. INFO - Counters.log(589) |     SLOTS_MILLIS_REDUCES=13645  
  22. INFO - Counters.log(587) |   File Output Format Counters   
  23. INFO - Counters.log(589) |     Bytes Written=0  
  24. INFO - Counters.log(587) |   FileSystemCounters  
  25. INFO - Counters.log(589) |     FILE_BYTES_READ=105  
  26. INFO - Counters.log(589) |     HDFS_BYTES_READ=203  
  27. INFO - Counters.log(589) |     FILE_BYTES_WRITTEN=113616  
  28. INFO - Counters.log(589) |     HDFS_BYTES_WRITTEN=87  
  29. INFO - Counters.log(587) |   File Input Format Counters   
  30. INFO - Counters.log(589) |     Bytes Read=91  
  31. INFO - Counters.log(587) |   Map-Reduce Framework  
  32. INFO - Counters.log(589) |     Map output materialized bytes=105  
  33. INFO - Counters.log(589) |     Map input records=6  
  34. INFO - Counters.log(589) |     Reduce shuffle bytes=105  
  35. INFO - Counters.log(589) |     Spilled Records=12  
  36. INFO - Counters.log(589) |     Map output bytes=87  
  37. INFO - Counters.log(589) |     Total committed heap usage (bytes)=176033792  
  38. INFO - Counters.log(589) |     CPU time spent (ms)=1880  
  39. INFO - Counters.log(589) |     Combine input records=0  
  40. INFO - Counters.log(589) |     SPLIT_RAW_BYTES=112  
  41. INFO - Counters.log(589) |     Reduce input records=6  
  42. INFO - Counters.log(589) |     Reduce input groups=3  
  43. INFO - Counters.log(589) |     Combine output records=0  
  44. INFO - Counters.log(589) |     Physical memory (bytes) snapshot=278876160  
  45. INFO - Counters.log(589) |     Reduce output records=0  
  46. INFO - Counters.log(589) |     Virtual memory (bytes) snapshot=1460908032  
  47. INFO - Counters.log(589) |     Map output records=6  

运行成功后,生成的文件如下所示:


china-r-00000里面的数据如下:
Java代码  收藏代码
  1. 中国  我们  
  2. 中国  123  

USA-r-00000里面的数据如下:
Java代码  收藏代码
  1. 美国  他们  
  2. 美国  USA  
  3. 美国  在北美洲  

cperson-r-00000里面的数据如下:
Java代码  收藏代码
  1. 中国人  善良  


在输出结果中,reduce自带的那个文件仍然会输出,但是里面没有任何数据,至此,我们已经在hadoop1.2.0的基于新的API里,测试多文件输出通过。
0 0
原创粉丝点击