NLineInputFormat案例分析与实现

NLineInputFormat

》1 什么是NLineInputFormat

用于读hdfs中的文本文件,每次入读固定行数

本质上代表每个map进程处理的InputSplit不再按Block块去划分,而是按NlineInputFormat指定的行数N来划分。

即输入文件的总行数/N=切片数,如果不整除,切片数=商+1

键是文件中行的字节偏移量,值是行本身。N 是每个 Mapper 收到的输入行数

N 设置为1(默认值)时,每个 Mapper 正好收到一行输入。 mapreduce.input.lineinputformat.linespermap 属性(在旧版本 API 中的mapred.line.input.format.linespermap 属性)实现 N 值的设定。
以下是一个示例,仍然以上面的4行输入为例。

Rich learning form
Intelligent learning engine
Learning more convenient
From the real demand for more close to the enterprise

例如,如果 N 是2,则每个输入分片包含两行。一个 mapper 收到前两行键值对:

(0,Rich learning form)
(19,Intelligent learning engine)
另一个 mapper 则收到后两行:
(47,Learning more convenient)
(72,From the real demand for more close to the enterprise)

这里的键和值与TextInputFormat生成的一样。

案例分析

对每个单词进行个数统计,要求根据每个输入文件的行数来规定输出多少个切片。
此案例要求每三行放入一个切片中。
(1)输入数据

banzhang ni hao
xihuan hadoop banzhang
banzhang ni hao
xihuan hadoop banzhang
banzhang ni hao
xihuan hadoop banzhang
banzhang ni hao

(2)期望输出数据

Number of splits:4

(3)Map阶段
获取一行
切割
循环输出
(4)Reduce阶段
汇总
输出
(5)Driver

//设置每个切片中划分三条记录
NLineInputFormat.setNumLinesPerSplit(job,3);
 job.setInputFormatClass(NLineInputFormat.class);

代码实现

(1)编写Mapper类

package com.dev1.nline;


public class NLineMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
   private Text k = new Text();
   private LongWritable v = new LongWritable();
    @Override
  protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] words = value.toString().split(" ");

        for(String word:words){
            //Text k = new Text();
  k.set(word);

            //LongWritable v = new LongWritable();
  v.set(1);

            context.write(k,v);
        }

    }
}

(2)编写Reducer类

package com.dev1.nline;

public class NLineReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
    LongWritable v = new LongWritable();
    @Override
  protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
        long sum = 0l;
        // 1 汇总
  for (LongWritable value : values) {
            sum += value.get();
        }
        v.set(sum);
        // 2 输出
  context.write(key, v);
    }
}

(3)编写Driver类

package com.dev1.nline;



public class NLineDriver {
    public static void main(String[] args) throws Exception{
        // 输入输出路径需要根据自己电脑上实际的输入输出路径设置
  // 1 获取job对象
  Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);
        // 7设置每个切片InputSplit中划分三条记录
  NLineInputFormat.setNumLinesPerSplit(job, 3);
        // 8使用NLineInputFormat处理记录数
  job.setInputFormatClass(NLineInputFormat.class);
        // 2设置jar包位置,关联mapper和reducer
  job.setJarByClass(NLineDriver.class);
        job.setMapperClass(NLineMapper.class);
        job.setReducerClass(NLineReducer.class);
        // 3设置map输出kv类型
  job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        // 4设置最终输出kv类型
  job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        // 5设置输入输出数据路径
  FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        // 6提交job
  job.waitForCompletion(true);
    }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

翁老师的教学团队

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值