001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package org.apache.hadoop.mapred;
020    
021    import java.io.IOException;
022    
023    import org.apache.hadoop.classification.InterfaceAudience;
024    import org.apache.hadoop.classification.InterfaceStability;
025    import org.apache.hadoop.conf.Configuration;
026    import org.apache.hadoop.io.Text;
027    import org.apache.hadoop.io.Writable;
028    import org.apache.hadoop.io.WritableComparable;
029    
030    /**
031     * This class converts the input keys and values to their String forms by calling toString()
032     * method. This class to SequenceFileAsTextInputFormat class is as LineRecordReader
033     * class to TextInputFormat class.
034     */
035    @InterfaceAudience.Public
036    @InterfaceStability.Stable
037    public class SequenceFileAsTextRecordReader
038      implements RecordReader<Text, Text> {
039      
040      private final SequenceFileRecordReader<WritableComparable, Writable>
041      sequenceFileRecordReader;
042    
043      private WritableComparable innerKey;
044      private Writable innerValue;
045    
046      public SequenceFileAsTextRecordReader(Configuration conf, FileSplit split)
047        throws IOException {
048        sequenceFileRecordReader =
049          new SequenceFileRecordReader<WritableComparable, Writable>(conf, split);
050        innerKey = sequenceFileRecordReader.createKey();
051        innerValue = sequenceFileRecordReader.createValue();
052      }
053    
054      public Text createKey() {
055        return new Text();
056      }
057      
058      public Text createValue() {
059        return new Text();
060      }
061    
062      /** Read key/value pair in a line. */
063      public synchronized boolean next(Text key, Text value) throws IOException {
064        Text tKey = key;
065        Text tValue = value;
066        if (!sequenceFileRecordReader.next(innerKey, innerValue)) {
067          return false;
068        }
069        tKey.set(innerKey.toString());
070        tValue.set(innerValue.toString());
071        return true;
072      }
073      
074      public float getProgress() throws IOException {
075        return sequenceFileRecordReader.getProgress();
076      }
077      
078      public synchronized long getPos() throws IOException {
079        return sequenceFileRecordReader.getPos();
080      }
081      
082      public synchronized void close() throws IOException {
083        sequenceFileRecordReader.close();
084      }
085      
086    }