NLineInputFormat用法-1

输入的数据:

  1. [root@i-love-you hadoop]# bin/hdfs dfs -text /input/hehe  
  2. hadoop hello  
  3. hadoop me  
  4. hadoop java  


代码:

  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.FileInputFormat;  
  13. import org.apache.hadoop.mapreduce.lib.input.NLineInputFormat;  
  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. public class NLineInputFormatTest {  
  19.     public static class MyMapper extends  
  20.             Mapper<LongWritable, Text, Text, LongWritable> {  
  21.         final Text k2 = new Text();  
  22.         final LongWritable v2 = new LongWritable();  
  23.   
  24.         protected void map(LongWritable key, Text value,  
  25.                 Mapper<LongWritable, Text, Text, LongWritable>.Context context)  
  26.                 throws InterruptedException, IOException {  
  27.             final String line = value.toString();  
  28.             final String[] splited = line.split("\\s");  
  29.             for (String word : splited) {  
  30.                 k2.set(word);  
  31.                 v2.set(1);  
  32.                 context.write(k2, v2);  
  33.             }  
  34.         }  
  35.     }  
  36.   
  37.     public static class MyReducer extends  
  38.             Reducer<Text, LongWritable, Text, LongWritable> {  
  39.         LongWritable v3 = new LongWritable();  
  40.   
  41.         protected void reduce(Text k2, Iterable<LongWritable> v2s,  
  42.                 Reducer<Text, LongWritable, Text, LongWritable>.Context context)  
  43.                 throws IOException, InterruptedException {  
  44.             long count = 0L;  
  45.             for (LongWritable v2 : v2s) {  
  46.                 count += v2.get();  
  47.             }  
  48.             v3.set(count);  
  49.             context.write(k2, v3);  
  50.         }  
  51.     }  
  52.   
  53.     public static void main(String[] args) throws Exception {  
  54.         final Configuration conf = new Configuration();  
  55.         final Job job = Job.getInstance(conf, NLineInputFormatTest.class.getSimpleName());  
  56.         // 1.1  
  57.         FileInputFormat.setInputPaths(job,  
  58.                 "hdfs://192.168.1.10:9000/input/hehe");  
  59.           
  60.         //这里改了一下,把TextInputFormat改成了NLineInputFormat  
  61.         NLineInputFormat.setNumLinesPerSplit(job, Integer.parseInt("2"));  
  62.         //NLineInputFormat.setNumLinesPerSplit(job, Integer.parseInt(args[0]));  
  63.         job.setInputFormatClass(NLineInputFormat.class);  
  64.           
  65.           
  66.         // 1.2  
  67.         job.setMapperClass(MyMapper.class);  
  68.         job.setMapOutputKeyClass(Text.class);  
  69.         job.setMapOutputValueClass(LongWritable.class);  
  70.         // 1.3 默认只有一个分区  
  71.         job.setPartitionerClass(HashPartitioner.class);  
  72.         job.setNumReduceTasks(1);  
  73.         // 1.4省略不写  
  74.         // 1.5省略不写  
  75.   
  76.         // 2.2  
  77.         job.setReducerClass(MyReducer.class);  
  78.         job.setOutputKeyClass(Text.class);  
  79.         job.setOutputValueClass(LongWritable.class);  
  80.         // 2.3  
  81.         FileOutputFormat.setOutputPath(job, new Path(  
  82.                 "hdfs://192.168.1.10:9000/out1"));  
  83.         job.setOutputFormatClass(TextOutputFormat.class);  
  84.         // 执行打成jar包的程序时,必须调用下面的方法  
  85.         job.setJarByClass(NLineInputFormatTest.class);  
  86.         job.waitForCompletion(true);  
  87.     }  
  88. }  


结果:

  1. [root@i-love-you hadoop]# bin/hadoop jar data/nline.jar  
  2. 15/04/16 15:04:48 INFO client.RMProxy: Connecting to ResourceManager at /0.0.0.0:8032  
  3. 15/04/16 15:04:50 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:04:51 INFO input.FileInputFormat: Total input paths to process : 1  
  5. 15/04/16 15:04:52 INFO mapreduce.JobSubmitter: number of splits:2  
  6. 15/04/16 15:04:53 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1429167587909_0001  
  7. 15/04/16 15:04:54 INFO impl.YarnClientImpl: Submitted application application_1429167587909_0001  
  8. 15/04/16 15:04:54 INFO mapreduce.Job: The url to track the job: http://i-love-you:8088/proxy/application_1429167587909_0001/  
  9. 15/04/16 15:04:54 INFO mapreduce.Job: Running job: job_1429167587909_0001  
  10. 15/04/16 15:05:38 INFO mapreduce.Job: Job job_1429167587909_0001 running in uber mode : false  
  11. 15/04/16 15:05:38 INFO mapreduce.Job:  map 0% reduce 0%  
  12. 15/04/16 15:08:31 INFO mapreduce.Job:  map 100% reduce 0%  
  13. 15/04/16 15:10:03 INFO mapreduce.Job:  map 100% reduce 100%  
  14. 15/04/16 15:10:07 INFO mapreduce.Job: Job job_1429167587909_0001 completed successfully  
  15. 15/04/16 15:10:08 INFO mapreduce.Job: Counters: 49  
  16.         File System Counters  
  17.                 FILE: Number of bytes read=101  
  18.                 FILE: Number of bytes written=318117  
  19.                 FILE: Number of read operations=0  
  20.                 FILE: Number of large read operations=0  
  21.                 FILE: Number of write operations=0  
  22.                 HDFS: Number of bytes read=248  
  23.                 HDFS: Number of bytes written=29  
  24.                 HDFS: Number of read operations=9  
  25.                 HDFS: Number of large read operations=0  
  26.                 HDFS: Number of write operations=2  
  27.         Job Counters  
  28.                 Launched map tasks=2  
  29.                 Launched reduce tasks=1  
  30.                 Other local map tasks=2  
  31.                 Total time spent by all maps in occupied slots (ms)=356494  
  32.                 Total time spent by all reduces in occupied slots (ms)=63006  
  33.                 Total time spent by all map tasks (ms)=356494  
  34.                 Total time spent by all reduce tasks (ms)=63006  
  35.                 Total vcore-seconds taken by all map tasks=356494  
  36.                 Total vcore-seconds taken by all reduce tasks=63006  
  37.                 Total megabyte-seconds taken by all map tasks=365049856  
  38.                 Total megabyte-seconds taken by all reduce tasks=64518144  
  39.         Map-Reduce Framework  
  40.                 Map input records=3  
  41.                 Map output records=6  
  42.                 Map output bytes=83  
  43.                 Map output materialized bytes=107  
  44.                 Input split bytes=200  
  45.                 Combine input records=0  
  46.                 Combine output records=0  
  47.                 Reduce input groups=4  
  48.                 Reduce shuffle bytes=107  
  49.                 Reduce input records=6  
  50.                 Reduce output records=4  
  51.                 Spilled Records=12  
  52.                 Shuffled Maps =2  
  53.                 Failed Shuffles=0  
  54.                 Merged Map outputs=2  
  55.                 GC time elapsed (ms)=16068  
  56.                 CPU time spent (ms)=7560  
  57.                 Physical memory (bytes) snapshot=356200448  
  58.                 Virtual memory (bytes) snapshot=2527195136  
  59.                 Total committed heap usage (bytes)=257171456  
  60.         Shuffle Errors  
  61.                 BAD_ID=0  
  62.                 CONNECTION=0  
  63.                 IO_ERROR=0  
  64.                 WRONG_LENGTH=0  
  65.                 WRONG_MAP=0  
  66.                 WRONG_REDUCE=0  
  67.         File Input Format Counters  
  68.                 Bytes Read=48  
  69.         File Output Format Counters  
  70.                 Bytes Written=29  




解析:

我设置每2行为一个InputSplit:

  1. NLineInputFormat.setNumLinesPerSplit(job, Integer.parseInt("2"));  

在输出中可以看到splits的个数:

  1. 15/04/16 15:04:52 INFO mapreduce.JobSubmitter: number of splits:2 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值