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.LongWritable;
027    import org.apache.hadoop.io.Text;
028    
029    /**
030     * This class treats a line in the input as a key/value pair separated by a 
031     * separator character. The separator can be specified in config file 
032     * under the attribute name mapreduce.input.keyvaluelinerecordreader.key.value.separator. The default
033     * separator is the tab character ('\t').
034     * 
035     * @deprecated Use 
036     * {@link org.apache.hadoop.mapreduce.lib.input.KeyValueLineRecordReader} 
037     * instead
038     */
039    @Deprecated
040    @InterfaceAudience.Public
041    @InterfaceStability.Stable
042    public class KeyValueLineRecordReader implements RecordReader<Text, Text> {
043      
044      private final LineRecordReader lineRecordReader;
045    
046      private byte separator = (byte) '\t';
047    
048      private LongWritable dummyKey;
049    
050      private Text innerValue;
051    
052      public Class getKeyClass() { return Text.class; }
053      
054      public Text createKey() {
055        return new Text();
056      }
057      
058      public Text createValue() {
059        return new Text();
060      }
061    
062      public KeyValueLineRecordReader(Configuration job, FileSplit split)
063        throws IOException {
064        
065        lineRecordReader = new LineRecordReader(job, split);
066        dummyKey = lineRecordReader.createKey();
067        innerValue = lineRecordReader.createValue();
068        String sepStr = job.get("mapreduce.input.keyvaluelinerecordreader.key.value.separator", "\t");
069        this.separator = (byte) sepStr.charAt(0);
070      }
071    
072      public static int findSeparator(byte[] utf, int start, int length, 
073          byte sep) {
074        return org.apache.hadoop.mapreduce.lib.input.
075          KeyValueLineRecordReader.findSeparator(utf, start, length, sep);
076      }
077    
078      /** Read key/value pair in a line. */
079      public synchronized boolean next(Text key, Text value)
080        throws IOException {
081        byte[] line = null;
082        int lineLen = -1;
083        if (lineRecordReader.next(dummyKey, innerValue)) {
084          line = innerValue.getBytes();
085          lineLen = innerValue.getLength();
086        } else {
087          return false;
088        }
089        if (line == null)
090          return false;
091        int pos = findSeparator(line, 0, lineLen, this.separator);
092        org.apache.hadoop.mapreduce.lib.input.KeyValueLineRecordReader.
093          setKeyValue(key, value, line, lineLen, pos);
094        return true;
095      }
096      
097      public float getProgress() throws IOException {
098        return lineRecordReader.getProgress();
099      }
100      
101      public synchronized long getPos() throws IOException {
102        return lineRecordReader.getPos();
103      }
104    
105      public synchronized void close() throws IOException { 
106        lineRecordReader.close();
107      }
108    }