org.shaf.core.io.hadoop.WholeFileRecordReader.java Source code

Java tutorial

Introduction

Here is the source code for org.shaf.core.io.hadoop.WholeFileRecordReader.java

Source

/**
 * Copyright 2014-2015 SHAF-WORK
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.shaf.core.io.hadoop;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.io.IOUtils;
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.FileSplit;

/**
 * The {@code RecordReader} for the whole files. The file name becomes a record
 * key and the file body becomes a record value.
 * 
 * @author Mykola Galushka
 */
public class WholeFileRecordReader extends RecordReader<Text, Text> {

    /**
     * The reference to the file split.
     */
    private FileSplit split;

    /**
     * The reference to the job configuration.
     */
    private Configuration config;

    /**
     * The key for the currently reading file.
     */
    private Text key = new Text();

    /**
     * The value for the currently reading file.
     */
    private final Text value = new Text();

    /**
     * The flag which indicates whether the file has been processed.
     */
    private boolean fileProcessed = false;

    @Override
    public void initialize(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException {
        this.split = (FileSplit) split;
        this.config = context.getConfiguration();
    }

    @Override
    public boolean nextKeyValue() throws IOException, InterruptedException {
        if (fileProcessed) {
            return false;
        }

        int fileLength = (int) split.getLength();
        byte[] result = new byte[fileLength];

        FileSystem fs = FileSystem.get(config);
        FSDataInputStream in = null;
        try {
            this.key.set(split.getPath().toString());
            in = fs.open(split.getPath());
            IOUtils.readFully(in, result, 0, fileLength);
            value.set(new String(result, 0, fileLength));

        } finally {
            IOUtils.closeStream(in);
        }
        this.fileProcessed = true;
        return true;
    }

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

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

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

    @Override
    public void close() throws IOException {

    }
}