org.shaf.core.io.emulator.SequenceReader.java Source code

Java tutorial

Introduction

Here is the source code for org.shaf.core.io.emulator.SequenceReader.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.emulator;

import java.io.EOFException;
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.Writable;

import org.shaf.core.util.Log;

/**
 * A {@link RecordReader}, which reads key-value pairs from binary files.
 * 
 * <p>
 * The behavior of the {@code SequenceReader} is similar to the behavior of the
 * Hadoop IO class {@code SequenceFileOutputFormat}.
 * </p>
 * 
 * <p>
 * The record format:
 * <ul>
 * <li>Key type: <b>Writable</b></li>
 * <li>Value type: <b>Writable</b></li>
 * </ul>
 * </p>
 * 
 * @author Mykola Galushka
 */
public class SequenceReader extends FileReader<Writable, Writable> {

    /**
     * Defines a logger.
     */
    private static final Log LOG = Log.forClass(SequenceReader.class);

    /**
     * The key class.
     */
    private final Class<?> keyClass;

    /**
     * The value class.
     */
    private final Class<?> valueClass;

    /**
     * Constructs a new sequence reader.
     * 
     * @param config
     *            the reader configuration.
     * @throws IOException
     *             if the reader has failed to initialize.
     */
    public SequenceReader(final Configuration config) throws IOException {
        super(config);

        try {
            this.keyClass = Class.forName(super.in.readUTF());
            this.valueClass = Class.forName(super.in.readUTF());
        } catch (ClassNotFoundException exc) {
            throw new IOException("Invalid sequence file format.", exc);
        }
    }

    /**
     * Reads a {@link Record record} from the sequence file.
     */
    @Override
    public Record<Writable, Writable> readRecord() throws IOException {
        try {
            Writable key = (Writable) this.keyClass.newInstance();
            key.readFields(super.in);

            Writable value = (Writable) this.valueClass.newInstance();
            value.readFields(super.in);

            return new Record<>(key, value);
        } catch (InstantiationException | IllegalAccessException exc) {
            throw new IOException("Invalid sequence file format.", exc);
        } catch (EOFException exc) {
            LOG.trace("The file reader reached the end of file.", exc);
            return null;
        }
    }
}