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.lib;
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.fs.FileSystem;
026    import org.apache.hadoop.mapred.JobConf;
027    import org.apache.hadoop.mapred.OutputFormat;
028    import org.apache.hadoop.mapred.RecordWriter;
029    import org.apache.hadoop.mapred.Reporter;
030    import org.apache.hadoop.util.Progressable;
031    
032    /**
033     * FilterOutputFormat is a convenience class that wraps OutputFormat. 
034     */
035    @InterfaceAudience.Public
036    @InterfaceStability.Stable
037    public class FilterOutputFormat<K, V> implements OutputFormat<K, V> {
038    
039      protected OutputFormat<K,V> baseOut;
040    
041      public FilterOutputFormat () {
042        this.baseOut = null;
043      }
044    
045      /**
046       * Create a FilterOutputFormat based on the supplied output format.
047       * @param out the underlying OutputFormat
048       */
049      public FilterOutputFormat (OutputFormat<K,V> out) {
050        this.baseOut = out;
051      }
052    
053      public RecordWriter<K, V> getRecordWriter(FileSystem ignored, JobConf job, 
054          String name, Progressable progress) throws IOException {
055        return getBaseOut().getRecordWriter(ignored, job, name, progress);
056      }
057    
058      public void checkOutputSpecs(FileSystem ignored, JobConf job) 
059      throws IOException {
060        getBaseOut().checkOutputSpecs(ignored, job);
061      }
062      
063      private OutputFormat<K,V> getBaseOut() throws IOException {
064        if (baseOut == null) {
065          throw new IOException("Outputformat not set for FilterOutputFormat");
066        }
067        return baseOut;
068      }
069    
070      /**
071       * <code>FilterRecordWriter</code> is a convenience wrapper
072       * class that implements  {@link RecordWriter}.
073       */
074    
075      public static class FilterRecordWriter<K,V> implements RecordWriter<K,V> {
076    
077        protected RecordWriter<K,V> rawWriter = null;
078    
079        public FilterRecordWriter() throws IOException {
080          rawWriter = null;
081        }
082    
083        public FilterRecordWriter(RecordWriter<K,V> rawWriter)  throws IOException {
084          this.rawWriter = rawWriter;
085        }
086    
087        public void close(Reporter reporter) throws IOException {
088          getRawWriter().close(reporter);
089        }
090    
091        public void write(K key, V value) throws IOException {
092          getRawWriter().write(key, value);
093        }
094        
095        private RecordWriter<K,V> getRawWriter() throws IOException {
096          if (rawWriter == null) {
097            throw new IOException ("Record Writer not set for FilterRecordWriter");
098          }
099          return rawWriter;
100        }
101      }
102    
103    }