source: proiecte/HadoopJUnit/hadoop-0.20.1/src/mapred/org/apache/hadoop/mapreduce/lib/output/SequenceFileOutputFormat.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 4.1 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package org.apache.hadoop.mapreduce.lib.output;
20
21import java.io.IOException;
22
23import org.apache.hadoop.fs.FileSystem;
24import org.apache.hadoop.fs.Path;
25
26import org.apache.hadoop.io.SequenceFile;
27import org.apache.hadoop.io.SequenceFile.CompressionType;
28import org.apache.hadoop.io.compress.CompressionCodec;
29import org.apache.hadoop.io.compress.DefaultCodec;
30import org.apache.hadoop.mapreduce.Job;
31import org.apache.hadoop.mapreduce.JobContext;
32import org.apache.hadoop.mapreduce.OutputFormat;
33import org.apache.hadoop.mapreduce.RecordWriter;
34import org.apache.hadoop.mapreduce.TaskAttemptContext;
35import org.apache.hadoop.util.ReflectionUtils;
36import org.apache.hadoop.conf.Configuration;
37
38/** An {@link OutputFormat} that writes {@link SequenceFile}s. */
39public class SequenceFileOutputFormat <K,V> extends FileOutputFormat<K, V> {
40
41  public RecordWriter<K, V> 
42         getRecordWriter(TaskAttemptContext context
43                         ) throws IOException, InterruptedException {
44    Configuration conf = context.getConfiguration();
45   
46    CompressionCodec codec = null;
47    CompressionType compressionType = CompressionType.NONE;
48    if (getCompressOutput(context)) {
49      // find the kind of compression to do
50      compressionType = getOutputCompressionType(context);
51
52      // find the right codec
53      Class<?> codecClass = getOutputCompressorClass(context, 
54                                                     DefaultCodec.class);
55      codec = (CompressionCodec) 
56        ReflectionUtils.newInstance(codecClass, conf);
57    }
58    // get the path of the temporary output file
59    Path file = getDefaultWorkFile(context, "");
60    FileSystem fs = file.getFileSystem(conf);
61    final SequenceFile.Writer out = 
62      SequenceFile.createWriter(fs, conf, file,
63                                context.getOutputKeyClass(),
64                                context.getOutputValueClass(),
65                                compressionType,
66                                codec,
67                                context);
68
69    return new RecordWriter<K, V>() {
70
71        public void write(K key, V value)
72          throws IOException {
73
74          out.append(key, value);
75        }
76
77        public void close(TaskAttemptContext context) throws IOException { 
78          out.close();
79        }
80      };
81  }
82
83  /**
84   * Get the {@link CompressionType} for the output {@link SequenceFile}.
85   * @param job the {@link Job}
86   * @return the {@link CompressionType} for the output {@link SequenceFile},
87   *         defaulting to {@link CompressionType#RECORD}
88   */
89  public static CompressionType getOutputCompressionType(JobContext job) {
90    String val = job.getConfiguration().get("mapred.output.compression.type", 
91                                            CompressionType.RECORD.toString());
92    return CompressionType.valueOf(val);
93  }
94 
95  /**
96   * Set the {@link CompressionType} for the output {@link SequenceFile}.
97   * @param job the {@link Job} to modify
98   * @param style the {@link CompressionType} for the output
99   *              {@link SequenceFile}
100   */
101  public static void setOutputCompressionType(Job job, 
102                                                          CompressionType style) {
103    setCompressOutput(job, true);
104    job.getConfiguration().set("mapred.output.compression.type", 
105                               style.toString());
106  }
107
108}
109
Note: See TracBrowser for help on using the repository browser.