自定义实现RCFileOutputFormat.java

  1. import java.io.IOException;  
  2.   
  3. import org.apache.Hadoop.conf.Configuration;  
  4. import org.apache.hadoop.fs.FileSystem;  
  5. import org.apache.hadoop.fs.Path;  
  6.   
  7. import org.apache.hadoop.io.compress.DefaultCodec;  
  8.   
  9. import org.apache.hadoop.hive.ql.io.RCFile;  
  10. import org.apache.hadoop.hive.serde2.columnar.BytesRefArrayWritable;  
  11. import org.apache.hadoop.io.LongWritable;  
  12. import org.apache.hadoop.io.WritableComparable;  
  13. import org.apache.hadoop.io.compress.CompressionCodec;  
  14. import org.apache.hadoop.mapreduce.RecordWriter;  
  15. import org.apache.hadoop.mapreduce.TaskAttemptContext;  
  16. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  17.   
  18. import org.apache.hadoop.util.ReflectionUtils;  
  19.   
  20. /** 
  21.  * RCFileOutputFormat. 
  22.  *  
  23.  */  
  24. public class RCFileOutputFormat  
  25.         extends  
  26.         FileOutputFormat<WritableComparable<LongWritable>, BytesRefArrayWritable> {  
  27.   
  28.     /** 
  29.      * set number of columns into the given configuration. 
  30.      *  
  31.      * @param conf 
  32.      *            configuration instance which need to set the column number 
  33.      * @param columnNum 
  34.      *            column number for RCFile's Writer 
  35.      *  
  36.      */  
  37.     public static void setColumnNumber(Configuration conf, int columnNum) {  
  38.         assert columnNum > 0;  
  39.         conf.setInt(RCFile.COLUMN_NUMBER_CONF_STR, columnNum);  
  40.     }  
  41.   
  42.     /** 
  43.      * Returns the number of columns set in the conf for writers. 
  44.      *  
  45.      * @param conf 
  46.      * @return number of columns for RCFile's writer 
  47.      */  
  48.     public static int getColumnNumber(Configuration conf) {  
  49.         return conf.getInt(RCFile.COLUMN_NUMBER_CONF_STR, 0);  
  50.     }  
  51.   
  52.     @Override  
  53.     public RecordWriter<WritableComparable<LongWritable>, BytesRefArrayWritable> getRecordWriter(  
  54.             TaskAttemptContext arg0) throws IOException, InterruptedException {  
  55.         Configuration conf = arg0.getConfiguration();  
  56.         conf.setBoolean("mapred.output.compress", true);  
  57.         Path outputPath = FileOutputFormat.getOutputPath(arg0);  
  58.         FileSystem fs = outputPath.getFileSystem(conf);  
  59.         if (!fs.exists(outputPath)) {  
  60.             fs.mkdirs(outputPath);  
  61.         }  
  62.         Path file = getDefaultWorkFile(arg0, "");  
  63.         CompressionCodec codec = null;  
  64.         if (getCompressOutput(arg0)) {  
  65.             Class<?> codecClass = getOutputCompressorClass(arg0,  
  66.                     DefaultCodec.class);  
  67.             codec = (CompressionCodec) ReflectionUtils.newInstance(codecClass,  
  68.                     conf);  
  69.         }  
  70.         final RCFile.Writer out = new RCFile.Writer(fs, conf, file, null, codec);  
  71.   
  72.         return new RecordWriter<WritableComparable<LongWritable>, BytesRefArrayWritable>() {  
  73.   
  74.             @Override  
  75.             public void write(WritableComparable<LongWritable> key,  
  76.                     BytesRefArrayWritable value) throws IOException {  
  77.                 out.append(value);  
  78.             }  
  79.   
  80.             @Override  
  81.             public void close(TaskAttemptContext arg0) throws IOException,  
  82.                     InterruptedException {  
  83.                 out.close();  
  84.   
  85.             }  
  86.         };  
  87.   
  88.     }  
  89. }  

应用:

  1. job.setOutputFormatClass(RCFileOutputFormat.class);  
  2.  job.setOutputValueClass(BytesRefArrayWritable.class);  
  1.  BytesRefArrayWritable values = new BytesRefArrayWritable(COLUMNS);  
  2.  values.set(0, new BytesRefWritable(fuid.getBytes()));  
  3.  values.set(1, new BytesRefWritable(this.sid.getBytes()));  
  4.  values.set(2, new BytesRefWritable(this.times.getBytes());  
  5.  context.write(new Text(this.uid), values);