MapReduce3.x 自定义 FileInputFormat

查看Mapper源码可以发现,在每次进行map方法之前会执行nextKeyValue、getCurrentKey、getCurrentValue方法,它们分别代表:判断接下来是否还有Key Value pairs,如果还有则继续执行map方法;获得下一次执行map的Key;获得下一次执行map的Value。所以如果要自定义FileInputFormat就要重写这几个方法。

以下是这次实验的项目结构目录

以下分别是TestFileInputFormat、TestRecordReader、InputFormatDriver类

package com.atguigu.mapreduce.FileInputFormatTest;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import java.io.IOException;

public class TestFileInputFormat extends FileInputFormat<Text, IntWritable> {

    @Override
    public RecordReader<Text, IntWritable> createRecordReader(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException {
        return new TestRecordReader();
    }
}

 经过Debug后,发现initialize方法只会执行一次,就起到相当于构造函数一样的作用;close函数也只会在最后Mapper阶段结束后执行一次,关闭所有的流。

package com.atguigu.mapreduce.FileInputFormatTest;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.JobContext;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.util.LineReader;

import java.io.IOException;

public class TestRecordReader extends RecordReader<Text, IntWritable> {

    private FileSplit fileSplit;
    private JobContext jobContext;
    private Text inputKey;
    private IntWritable inputValue;
    private LineReader in;
    private Text line;

    @Override
    public void initialize(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException {
        this.fileSplit = (FileSplit) split;
        this.jobContext = context;
        Configuration conf = context.getConfiguration();
        Path file = this.fileSplit.getPath();
        FileSystem fs = file.getFileSystem(conf);
        FSDataInputStream fileIn = fs.open(file);
        in = new LineReader(fileIn, conf);
        line = new Text();
        inputKey = new Text();
        inputValue = new IntWritable(1);
    }

    @Override
    public boolean nextKeyValue() throws IOException, InterruptedException {
        int lineSize = in.readLine(line);
        if (lineSize == 0) return false;

        String[] pieces = line.toString().split(" ");
        inputKey.set(pieces[0]);

        return true;
    }

    @Override
    public Text getCurrentKey() throws IOException, InterruptedException {
        return this.inputKey;
    }

    @Override
    public IntWritable getCurrentValue() throws IOException, InterruptedException {
        return this.inputValue;
    }

    @Override
    public float getProgress() throws IOException, InterruptedException {
        return 0;
    }

    @Override
    public void close() throws IOException {
        IOUtils.closeStream(in);
    }
}

和预想的一样,每次在执行map方法之前会调用以上的nextKeyValue、getCurrentKey、getCurrentValue方法获取 key value pairs 的信息,在这三个重写方法中就可以实现特定的业务逻辑

package com.atguigu.mapreduce.FileInputFormatTest;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class InputFormatDriver {

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        job.setJarByClass(InputFormatDriver.class);

        job.setMapperClass(InputFormatMapper.class);
        job.setReducerClass(InputFormatReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        // 自定义FileInputFormat
        job.setInputFormatClass(TestFileInputFormat.class);

        FileInputFormat.setInputPaths(job, new Path("D:\\BaiduNetdiskDownload\\11_input\\inputfileinputformat"));
        FileOutputFormat.setOutputPath(job, new Path("D:\\hadoop\\output666"));

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值