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.fs.FileSystem;
024    import org.apache.hadoop.fs.Path;
025    
026    import org.apache.hadoop.io.MapFile;
027    import org.apache.hadoop.io.WritableComparable;
028    import org.apache.hadoop.io.Writable;
029    import org.apache.hadoop.io.SequenceFile.CompressionType;
030    import org.apache.hadoop.io.compress.CompressionCodec;
031    import org.apache.hadoop.io.compress.DefaultCodec;
032    import org.apache.hadoop.classification.InterfaceAudience;
033    import org.apache.hadoop.classification.InterfaceStability;
034    import org.apache.hadoop.conf.Configuration;
035    import org.apache.hadoop.util.Progressable;
036    import org.apache.hadoop.util.ReflectionUtils;
037    
038    /** An {@link OutputFormat} that writes {@link MapFile}s.
039     * @deprecated Use 
040     * {@link org.apache.hadoop.mapreduce.lib.output.MapFileOutputFormat} instead
041     */
042    @Deprecated
043    @InterfaceAudience.Public
044    @InterfaceStability.Stable
045    public class MapFileOutputFormat 
046    extends FileOutputFormat<WritableComparable, Writable> {
047    
048      public RecordWriter<WritableComparable, Writable> getRecordWriter(FileSystem ignored, JobConf job,
049                                          String name, Progressable progress)
050        throws IOException {
051        // get the path of the temporary output file 
052        Path file = FileOutputFormat.getTaskOutputPath(job, name);
053        
054        FileSystem fs = file.getFileSystem(job);
055        CompressionCodec codec = null;
056        CompressionType compressionType = CompressionType.NONE;
057        if (getCompressOutput(job)) {
058          // find the kind of compression to do
059          compressionType = SequenceFileOutputFormat.getOutputCompressionType(job);
060    
061          // find the right codec
062          Class<? extends CompressionCodec> codecClass = getOutputCompressorClass(job,
063              DefaultCodec.class);
064          codec = ReflectionUtils.newInstance(codecClass, job);
065        }
066        
067        // ignore the progress parameter, since MapFile is local
068        final MapFile.Writer out =
069          new MapFile.Writer(job, fs, file.toString(),
070                             job.getOutputKeyClass().asSubclass(WritableComparable.class),
071                             job.getOutputValueClass().asSubclass(Writable.class),
072                             compressionType, codec,
073                             progress);
074    
075        return new RecordWriter<WritableComparable, Writable>() {
076    
077            public void write(WritableComparable key, Writable value)
078              throws IOException {
079    
080              out.append(key, value);
081            }
082    
083            public void close(Reporter reporter) throws IOException { out.close();}
084          };
085      }
086    
087      /** Open the output generated by this format. */
088      public static MapFile.Reader[] getReaders(FileSystem ignored, Path dir,
089                                                Configuration conf)
090          throws IOException {
091        return org.apache.hadoop.mapreduce.lib.output.MapFileOutputFormat.
092          getReaders(dir, conf);
093      }
094        
095      /** Get an entry from output generated by this class. */
096      public static <K extends WritableComparable, V extends Writable>
097      Writable getEntry(MapFile.Reader[] readers,
098                                      Partitioner<K, V> partitioner,
099                                      K key,
100                                      V value) throws IOException {
101        int part = partitioner.getPartition(key, value, readers.length);
102        return readers[part].get(key, value);
103      }
104    
105    }
106