Hadoopの大きなデータ技術のMapReduce(3) - カスタム実用的な操作ケースのInputFormat

3.1.9カスタムケースのInputFormat実用的な操作

  • かどうかはHDFSやMapReduceは、小さなファイルの効率を扱うときには、非常に低いですが、必然的にあなたが適切な解決策を持っている必要があり、シーンのハンドルに小さなファイルの数が多い場合は、この時間に直面しています。あなたは、合併のInputFormatは、小さなファイルを達成カスタマイズすることができます。
1。需要
  • 小さなSequenceFileファイル(SequenceFile Hadoopのファイルはキーと値のペアのファイル形式のバイナリ形式を格納するために使用されている)、ファイルパス名+キー、ファイルに格納された複数のファイルを格納SequenceFile複数に結合されたファイルコンテンツ値。

(1)入力データ

  • one.txt
yongpeng weidong weinan
sanfeng luozong xiaoming
  • two.txt
longlong fanfan
mazong kailun yuhang yixin
longlong fanfan
mazong kailun yuhang yixi
  • three.txt
shuaige changmo zhenqiang
dongli lingu xuanxuan

(2)所望の出力ファイル形式
ここに画像を挿入説明

2。プログラムの実現

(1)カスタムInputFromat

/**
 * @Author zhangyong
 * @Date 2020/3/6 22:24
 * @Version 1.0
 * 定义类继承FileInputFormat
 */
public class WholeFileInputformat extends FileInputFormat<Text, BytesWritable>{
	@Override
	protected boolean isSplitable(JobContext context, Path filename) {
		return false;
	}
	@Override
	public RecordReader<Text, BytesWritable> createRecordReader(InputSplit split, TaskAttemptContext context)	throws IOException, InterruptedException {
		WholeRecordReader recordReader = new WholeRecordReader();
		recordReader.initialize(split, context);
		return recordReader;
	}
}

(2)カスタムクラスRecordReader

/**
 * @Author zhangyong
 * @Date 2020/3/6 22:24
 * @Version 1.0
 */
public class WholeRecordReader extends RecordReader<Text, BytesWritable> {
    private Configuration configuration;
    private FileSplit split;
    private boolean isProgress = true;
    private BytesWritable value = new BytesWritable ();
    private Text k = new Text ();
    @Override
    public void initialize(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException {
        this.split = (FileSplit) split;
        configuration = context.getConfiguration ();
    }
    @Override
    public boolean nextKeyValue() throws IOException, InterruptedException {
        if (isProgress) {
            // 1 定义缓存区
            byte[] contents = new byte[(int) split.getLength ()];
            FileSystem fs = null;
            FSDataInputStream fis = null;
            try {
                // 2 获取文件系统
                Path path = split.getPath ();
                fs = path.getFileSystem (configuration);
                // 3 读取数据
                fis = fs.open (path);
                // 4 读取文件内容
                IOUtils.readFully (fis, contents, 0, contents.length);
                // 5 输出文件内容
                value.set (contents, 0, contents.length);
                // 6 获取文件路径及名称
                String name = split.getPath ().toString ();
           		// 7 设置输出的key值
                k.set (name);
            } catch (Exception e) {
            } finally {
                IOUtils.closeStream (fis);
            }
            isProgress = false;
            return true;
        }
        return false;
    }
    @Override
    public Text getCurrentKey() throws IOException, InterruptedException {
        return k;
    }
    @Override
    public BytesWritable getCurrentValue() throws IOException, InterruptedException {
        return value;
    }
    @Override
    public float getProgress() throws IOException, InterruptedException {
        return 0;
    }
    @Override
    public void close() throws IOException {
    }
}

(3)書き込み処理プロセスSequenceFileMapper

/**
 * @Author zhangyong
 * @Date 2020/3/6 22:24
 * @Version 1.0
 * 定义类继承FileInputFormat
 */
public class SequenceFileMapper extends Mapper<Text, BytesWritable, Text, BytesWritable> {
    @Override
    protected void map(Text key, BytesWritable value, Context context) throws IOException, InterruptedException {
        context.write (key, value);
    }
}

(4)ライト処理プロセスSequenceFileReducer

/**
 * @Author zhangyong
 * @Date 2020/3/6 22:24
 * @Version 1.0
 */
public class SequenceFileReducer extends Reducer<Text, BytesWritable, Text, BytesWritable> {

    @Override
    protected void reduce(Text key, Iterable<BytesWritable> values, Context context) throws IOException, InterruptedException {

        context.write (key, values.iterator ().next ());
    }
}

(5)ライト処理プロセスSequenceFileDriver

/**
 * @Author zhangyong
 * @Date 2020/3/6 22:24
 * @Version 1.0
 */
public class SequenceFileDriver {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        // 数据输入路径和输出路径
        args = new String[2];
        args[0] = "src/main/resources/format/input";
        args[1] = "src/main/resources/format/output";

        // 读取配置文件
        Configuration cfg = new Configuration();
        cfg.set("mapreduce.framework.name", "local");
        cfg.set("fs.defaultFS", "file:///");

        final FileSystem filesystem = FileSystem.get(cfg);
        if (filesystem.exists(new Path(args[0]))) {
            filesystem.delete(new Path(args[1]), true);
        }

        Job job = Job.getInstance (cfg);

        // 2 设置jar包存储位置、关联自定义的mapper和reducer
        job.setJarByClass (SequenceFileDriver.class);
        job.setMapperClass (SequenceFileMapper.class);
        job.setReducerClass (SequenceFileReducer.class);

        // 7设置输入的inputFormat
        job.setInputFormatClass (WholeFileInputformat.class);

        // 8设置输出的outputFormat
        job.setOutputFormatClass (SequenceFileOutputFormat.class);

        // 3 设置map输出端的kv类型
        job.setMapOutputKeyClass (Text.class);
        job.setMapOutputValueClass (BytesWritable.class);

        // 4 设置最终输出端的kv类型
        job.setOutputKeyClass (Text.class);
        job.setOutputValueClass (BytesWritable.class);

        // 5 设置输入输出路径
        FileInputFormat.setInputPaths (job, new Path (args[0]));
        FileOutputFormat.setOutputPath (job, new Path (args[1]));

        // 6 提交job
        boolean result = job.waitForCompletion (true);
        System.exit (result ? 0 : 1);
    }
}
3。出力

ここに画像を挿入説明

公開された37元の記事 ウォン称賛7 ビュー1171

おすすめ

転載: blog.csdn.net/zy13765287861/article/details/104706363