    001/*
    002 * Copyright (c) 2016-2017 Chris K Wensel <chris@wensel.net>. All Rights Reserved.
    003 * Copyright (c) 2007-2017 Xplenty, Inc. All Rights Reserved.
    004 *
    005 * Project and contact information: http://www.cascading.org/
    006 *
    007 * This file is part of the Cascading project.
    008 *
    009 * Licensed under the Apache License, Version 2.0 (the "License");
    010 * you may not use this file except in compliance with the License.
    011 * You may obtain a copy of the License at
    012 *
    013 *     http://www.apache.org/licenses/LICENSE-2.0
    014 *
    015 * Unless required by applicable law or agreed to in writing, software
    016 * distributed under the License is distributed on an "AS IS" BASIS,
    017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    018 * See the License for the specific language governing permissions and
    019 * limitations under the License.
    020 */
    021
    022package cascading.tap.hadoop.io;
    023
    024import java.io.IOException;
    025
    026import cascading.flow.FlowProcess;
    027import cascading.flow.SliceCounters;
    028import cascading.scheme.Scheme;
    029import cascading.tap.Tap;
    030import cascading.tap.hadoop.util.MeasuredRecordReader;
    031import cascading.tap.type.FileType;
    032import cascading.tuple.TupleEntrySchemeIterator;
    033import cascading.util.CloseableIterator;
    034import org.apache.hadoop.conf.Configuration;
    035import org.apache.hadoop.mapred.RecordReader;
    036
    037/**
    038 *
    039 */
    040public class HadoopTupleEntrySchemeIterator extends TupleEntrySchemeIterator<Configuration, RecordReader>
    041  {
    042  private MeasuredRecordReader measuredRecordReader;
    043
    044  public HadoopTupleEntrySchemeIterator( FlowProcess<? extends Configuration> flowProcess, Tap parentTap, RecordReader recordReader ) throws IOException
    045    {
    046    this( flowProcess, parentTap, parentTap.getScheme(), makeIterator( flowProcess, parentTap, recordReader ) );
    047    }
    048
    049  @Deprecated
    050  public HadoopTupleEntrySchemeIterator( FlowProcess<? extends Configuration> flowProcess, Scheme scheme, CloseableIterator<RecordReader> closeableIterator )
    051    {
    052    super( flowProcess, scheme, closeableIterator, flowProcess.getStringProperty( FileType.CASCADING_SOURCE_PATH ) );
    053    }
    054
    055  public HadoopTupleEntrySchemeIterator( FlowProcess<? extends Configuration> flowProcess, Tap tap, Scheme scheme, CloseableIterator<RecordReader> closeableIterator )
    056    {
    057    super( flowProcess, tap, scheme, closeableIterator, flowProcess.getStringProperty( FileType.CASCADING_SOURCE_PATH ) );
    058    }
    059
    060  private static CloseableIterator<RecordReader> makeIterator( FlowProcess<? extends Configuration> flowProcess, Tap parentTap, RecordReader recordReader ) throws IOException
    061    {
    062    if( recordReader != null )
    063      return new RecordReaderIterator( recordReader );
    064
    065    return new MultiRecordReaderIterator( flowProcess, parentTap );
    066    }
    067
    068  @Override
    069  protected RecordReader wrapInput( RecordReader recordReader )
    070    {
    071    if( measuredRecordReader == null )
    072      measuredRecordReader = new MeasuredRecordReader( getFlowProcess(), SliceCounters.Read_Duration );
    073
    074    measuredRecordReader.setRecordReader( recordReader );
    075
    076    return measuredRecordReader;
    077    }
    078  }