CombineTextInputFormat用法

来源:互联网 发布:印度超越 知乎 编辑:程序博客网 时间:2024/05/13 03:18

输入数据:








代码:

[java] view plain copy print?
  1. package inputformat;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.hadoop.conf.Configuration;  
  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.mapreduce.Job;  
  10. import org.apache.hadoop.mapreduce.Mapper;  
  11. import org.apache.hadoop.mapreduce.Reducer;  
  12. import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;  
  13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  15. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;  
  16. import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;  
  17. /* 
  18.  * 处理的数据源是多个小文件 
  19.  * 会把多个小文件合并处理,合并的大小如果小于128M,就当成一个InputSplit处理。 
  20.  * 与SequenceFileInputFormat不同的是,SequenceFileInputFormat处理的数据源是合并好的SequencceFile类型的数据。 
  21.  */  
  22. public class CombineTextInputFormatTest {  
  23.     public static class MyMapper extends  
  24.             Mapper<LongWritable, Text, Text, LongWritable> {  
  25.         final Text k2 = new Text();  
  26.         final LongWritable v2 = new LongWritable();  
  27.   
  28.         protected void map(LongWritable key, Text value,  
  29.                 Mapper<LongWritable, Text, Text, LongWritable>.Context context)  
  30.                 throws InterruptedException, IOException {  
  31.             final String line = value.toString();  
  32.             final String[] splited = line.split("\\s");  
  33.             for (String word : splited) {  
  34.                 k2.set(word);  
  35.                 v2.set(1);  
  36.                 context.write(k2, v2);  
  37.             }  
  38.         }  
  39.     }  
  40.   
  41.     public static class MyReducer extends  
  42.             Reducer<Text, LongWritable, Text, LongWritable> {  
  43.         LongWritable v3 = new LongWritable();  
  44.   
  45.         protected void reduce(Text k2, Iterable<LongWritable> v2s,  
  46.                 Reducer<Text, LongWritable, Text, LongWritable>.Context context)  
  47.                 throws IOException, InterruptedException {  
  48.             long count = 0L;  
  49.             for (LongWritable v2 : v2s) {  
  50.                 count += v2.get();  
  51.             }  
  52.             v3.set(count);  
  53.             context.write(k2, v3);  
  54.         }  
  55.     }  
  56.   
  57.     public static void main(String[] args) throws Exception {  
  58.         final Configuration conf = new Configuration();  
  59.         final Job job = Job.getInstance(conf, CombineTextInputFormatTest.class.getSimpleName());  
  60.         // 1.1  
  61.         FileInputFormat.setInputPaths(job,  
  62.                 "hdfs://192.168.1.10:9000/input");  
  63.           
  64.         //这里改了一下  
  65.         job.setInputFormatClass(CombineTextInputFormat.class);  
  66.           
  67.           
  68.         // 1.2  
  69.         job.setMapperClass(MyMapper.class);  
  70.         job.setMapOutputKeyClass(Text.class);  
  71.         job.setMapOutputValueClass(LongWritable.class);  
  72.         // 1.3 默认只有一个分区  
  73.         job.setPartitionerClass(HashPartitioner.class);  
  74.         job.setNumReduceTasks(1);  
  75.         // 1.4省略不写  
  76.         // 1.5省略不写  
  77.   
  78.         // 2.2  
  79.         job.setReducerClass(MyReducer.class);  
  80.         job.setOutputKeyClass(Text.class);  
  81.         job.setOutputValueClass(LongWritable.class);  
  82.         // 2.3  
  83.         FileOutputFormat.setOutputPath(job, new Path(  
  84.                 "hdfs://192.168.1.10:9000/out2"));  
  85.         job.setOutputFormatClass(TextOutputFormat.class);  
  86.         // 执行打成jar包的程序时,必须调用下面的方法  
  87.         job.setJarByClass(CombineTextInputFormatTest.class);  
  88.         job.waitForCompletion(true);  
  89.     }  
  90. }  



console输出:

[java] view plain copy print?
  1. [root@i-love-you hadoop]# bin/hadoop jar data/ConbineText.jar  
  2. 15/04/16 15:27:02 INFO client.RMProxy: Connecting to ResourceManager at /0.0.0.0:8032  
  3. 15/04/16 15:27:06 WARN mapreduce.JobSubmitter: Hadoop command-line option parsing not performed. Implement the Tool interface and execute your application with ToolRunner to remedy this.  
  4. 15/04/16 15:27:07 INFO input.FileInputFormat: Total input paths to process : 2  
  5. 15/04/16 15:27:07 INFO input.CombineFileInputFormat: DEBUG: Terminated node allocation with : CompletedNodes: 1, size left: 79  
  6. 15/04/16 15:27:07 INFO mapreduce.JobSubmitter: number of splits:1  
  7. 15/04/16 15:27:08 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1429167587909_0003  
  8. 15/04/16 15:27:08 INFO impl.YarnClientImpl: Submitted application application_1429167587909_0003  
  9. 15/04/16 15:27:08 INFO mapreduce.Job: The url to track the job: http://i-love-you:8088/proxy/application_1429167587909_0003/  
  10. 15/04/16 15:27:08 INFO mapreduce.Job: Running job: job_1429167587909_0003  
  11. 15/04/16 15:27:23 INFO mapreduce.Job: Job job_1429167587909_0003 running in uber mode : false  
  12. 15/04/16 15:27:23 INFO mapreduce.Job:  map 0% reduce 0%  
  13. 15/04/16 15:27:39 INFO mapreduce.Job:  map 100% reduce 0%  
  14. 15/04/16 15:28:07 INFO mapreduce.Job:  map 100% reduce 100%  
  15. 15/04/16 15:28:17 INFO mapreduce.Job: Job job_1429167587909_0003 completed successfully  
  16. 15/04/16 15:28:18 INFO mapreduce.Job: Counters: 49  
  17.         File System Counters  
  18.                 FILE: Number of bytes read=215  
  19.                 FILE: Number of bytes written=212395  
  20.                 FILE: Number of read operations=0  
  21.                 FILE: Number of large read operations=0  
  22.                 FILE: Number of write operations=0  
  23.                 HDFS: Number of bytes read=259  
  24.                 HDFS: Number of bytes written=38  
  25.                 HDFS: Number of read operations=7  
  26.                 HDFS: Number of large read operations=0  
  27.                 HDFS: Number of write operations=2  
  28.         Job Counters  
  29.                 Launched map tasks=1  
  30.                 Launched reduce tasks=1  
  31.                 Other local map tasks=1  
  32.                 Total time spent by all maps in occupied slots (ms)=14359  
  33.                 Total time spent by all reduces in occupied slots (ms)=22113  
  34.                 Total time spent by all map tasks (ms)=14359  
  35.                 Total time spent by all reduce tasks (ms)=22113  
  36.                 Total vcore-seconds taken by all map tasks=14359  
  37.                 Total vcore-seconds taken by all reduce tasks=22113  
  38.                 Total megabyte-seconds taken by all map tasks=14703616  
  39.                 Total megabyte-seconds taken by all reduce tasks=22643712  
  40.         Map-Reduce Framework  
  41.                 Map input records=4  
  42.                 Map output records=13  
  43.                 Map output bytes=183  
  44.                 Map output materialized bytes=215  
  45.                 Input split bytes=180  
  46.                 Combine input records=0  
  47.                 Combine output records=0  
  48.                 Reduce input groups=5  
  49.                 Reduce shuffle bytes=215  
  50.                 Reduce input records=13  
  51.                 Reduce output records=5  
  52.                 Spilled Records=26  
  53.                 Shuffled Maps =1  
  54.                 Failed Shuffles=0  
  55.                 Merged Map outputs=1  
  56.                 GC time elapsed (ms)=209  
  57.                 CPU time spent (ms)=2860  
  58.                 Physical memory (bytes) snapshot=313401344  
  59.                 Virtual memory (bytes) snapshot=1687605248  
  60.                 Total committed heap usage (bytes)=136450048  
  61.         Shuffle Errors  
  62.                 BAD_ID=0  
  63.                 CONNECTION=0  
  64.                 IO_ERROR=0  
  65.                 WRONG_LENGTH=0  
  66.                 WRONG_MAP=0  
  67.                 WRONG_REDUCE=0  
  68.         File Input Format Counters  
  69.                 Bytes Read=0  
  70.         File Output Format Counters  
  71.                 Bytes Written=38  



计算结果:

[java] view plain copy print?
  1. [root@i-love-you hadoop]# bin/hdfs dfs -text /out2/part-*  
  2. hadoop  6  
  3. hello   2  
  4. java    3  
  5. me      1  
  6. struts  1  



可见把两个小文件的数据合并在一起处理了,合并成一个InputSplit。


0 0
原创粉丝点击