PageRenderTime 36ms CodeModel.GetById 18ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

/tags/release-0.2.0-rc0/src/java/org/apache/hcatalog/mapreduce/HCatRecordReader.java

#
Java | 103 lines | 45 code | 14 blank | 44 comment | 1 complexity | 40f458a1c073384ca33be85667658b25 MD5 | raw file
  1/*
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18package org.apache.hcatalog.mapreduce;
 19
 20import java.io.IOException;
 21
 22import org.apache.hadoop.io.Writable;
 23import org.apache.hadoop.io.WritableComparable;
 24import org.apache.hadoop.mapreduce.InputSplit;
 25import org.apache.hadoop.mapreduce.RecordReader;
 26import org.apache.hadoop.mapreduce.TaskAttemptContext;
 27import org.apache.hcatalog.data.HCatRecord;
 28
 29/** The HCat wrapper for the underlying RecordReader, this ensures that the initialize on
 30 * the underlying record reader is done with the underlying split, not with HCatSplit.
 31 */
 32class HCatRecordReader extends RecordReader<WritableComparable, HCatRecord> {
 33
 34    /** The underlying record reader to delegate to. */
 35    private final RecordReader<? extends WritableComparable, ? extends Writable> baseRecordReader;
 36
 37    /** The storage driver used */
 38    private final HCatInputStorageDriver storageDriver;
 39
 40    /**
 41     * Instantiates a new hcat record reader.
 42     * @param baseRecordReader the base record reader
 43     */
 44    public HCatRecordReader(HCatInputStorageDriver storageDriver, RecordReader<? extends WritableComparable, ? extends Writable> baseRecordReader) {
 45        this.baseRecordReader = baseRecordReader;
 46        this.storageDriver = storageDriver;
 47    }
 48
 49    /* (non-Javadoc)
 50     * @see org.apache.hadoop.mapreduce.RecordReader#initialize(org.apache.hadoop.mapreduce.InputSplit, org.apache.hadoop.mapreduce.TaskAttemptContext)
 51     */
 52    @Override
 53    public void initialize(InputSplit split, TaskAttemptContext taskContext)
 54    throws IOException, InterruptedException {
 55        InputSplit baseSplit = split;
 56
 57        if( split instanceof HCatSplit ) {
 58            baseSplit = ((HCatSplit) split).getBaseSplit();
 59        }
 60
 61        baseRecordReader.initialize(baseSplit, taskContext);
 62    }
 63
 64    /* (non-Javadoc)
 65     * @see org.apache.hadoop.mapreduce.RecordReader#getCurrentKey()
 66     */
 67    @Override
 68    public WritableComparable getCurrentKey() throws IOException, InterruptedException {
 69        return baseRecordReader.getCurrentKey();
 70    }
 71
 72    /* (non-Javadoc)
 73     * @see org.apache.hadoop.mapreduce.RecordReader#getCurrentValue()
 74     */
 75    @Override
 76    public HCatRecord getCurrentValue() throws IOException, InterruptedException {
 77        return storageDriver.convertToHCatRecord(baseRecordReader.getCurrentKey(),baseRecordReader.getCurrentValue());
 78    }
 79
 80    /* (non-Javadoc)
 81     * @see org.apache.hadoop.mapreduce.RecordReader#getProgress()
 82     */
 83    @Override
 84    public float getProgress() throws IOException, InterruptedException {
 85        return baseRecordReader.getProgress();
 86    }
 87
 88    /* (non-Javadoc)
 89     * @see org.apache.hadoop.mapreduce.RecordReader#nextKeyValue()
 90     */
 91    @Override
 92    public boolean nextKeyValue() throws IOException, InterruptedException {
 93        return baseRecordReader.nextKeyValue();
 94    }
 95
 96    /* (non-Javadoc)
 97     * @see org.apache.hadoop.mapreduce.RecordReader#close()
 98     */
 99    @Override
100    public void close() throws IOException {
101        baseRecordReader.close();
102    }
103}