LineRecordReader源码

来源:互联网 发布:qsv转制软件 编辑:程序博客网 时间:2024/05/18 00:48
LineRecordReader由一个FileSplit构造出来,start是这个FileSplit的起始位置,pos是当前读取分片的位 置,end是分片结束位置,in是打开的一个读取这个分片的输入流,它是使用这个FileSplit对应的文件名来打开的。key和value则分别是每 次读取的K-V对。然后我们还看到可以利用getProgress()来跟踪读取分片的进度,这个函数就是根据已经读取的K-V对占总K-V对的比例来显 示进度的。
  1. public class LineRecordReader extends RecordReader<</span>LongWritableText>  
  2.   private static final Log LOG LogFactory.getLog(LineRecordReader.class);  
  3.   
  4.   private CompressionCodecFactory compressionCodecs null 
  5.   private long start;  
  6.   private long pos;  
  7.   private long end;  
  8.   private LineReader in;  
  9.   private int maxLineLength;  
  10.   private LongWritable key null 
  11.   private Text value null 
  12.   
  13.   //我们知道LineRecordReader是读取一个InputSplit的,它从InputSplit中不断以其定义的格式读取K-V对  
  14.   //initialize函数主要是计算分片的始末位置,以及打开想要的输入流以供读取K-V对,输入流另外处理分片经过压缩的情况  
  15.   public void initialize(InputSplit genericSplit,  
  16.                          TaskAttemptContext context) throws IOException  
  17.     FileSplit split (FileSplit) genericSplit;  
  18.     Configuration job context.getConfiguration();  
  19.     this.maxLineLength job.getInt("mapred.linerecordreader.maxlength",  
  20.                                     Integer.MAX_VALUE);  
  21.     start split.getStart();  
  22.     end start split.getLength();  
  23.     final Path file split.getPath();  
  24.     compressionCodecs new CompressionCodecFactory(job);  
  25.     final CompressionCodec codec compressionCodecs.getCodec(file);  
  26.   
  27.     // open the file and seek to the start of the split  
  28.     FileSystem fs file.getFileSystem(job);  
  29.     FSDataInputStream fileIn fs.open(split.getPath());  
  30.     boolean skipFirstLine false 
  31.     if (codec != null)  
  32.       in new LineReader(codec.createInputStream(fileIn), job);  
  33.       end Long.MAX_VALUE;  
  34.     else  
  35.       if (start != 0)  
  36.         skipFirstLine true 
  37.         --start;  
  38.         fileIn.seek(start);  
  39.        
  40.       in new LineReader(fileIn, job);  
  41.      
  42.     if (skipFirstLine)  // skip first line and re-establish "start".  
  43.       start += in.readLine(new Text(), 0,  
  44.                            (int)Math.min((long)Integer.MAX_VALUE, end start));  
  45.      
  46.     this.pos start 
  47.    
  48.     
  49.   public boolean nextKeyValue() throws IOException  
  50.     if (key == null)  
  51.       key new LongWritable();  
  52.      
  53.     key.set(pos); //对于LineRecordReader来说,它以偏移值为key,以一行为value  
  54.     if (value == null)  
  55.       value new Text();  
  56.      
  57.     int newSize 0 
  58.     while (pos <</span> end 
  59.       newSize in.readLine(value, maxLineLength,  
  60.                             Math.max((int)Math.min(Integer.MAX_VALUE, end-pos),  
  61.                                      maxLineLength));  
  62.       if (newSize == 0)  
  63.         break;  
  64.        
  65.       pos += newSize;  
  66.       if (newSize <</span> maxLineLength 
  67.         break;  
  68.        
  69.   
  70.       // line too long. try again  
  71.       LOG.info("Skipped line of size newSize at pos   
  72.                (pos newSize));  
  73.      
  74.     if (newSize == 0)  
  75.       key null 
  76.       value null 
  77.       return false;  
  78.     else  
  79.       return true;  
  80.      
  81.    
  82.   
  83.   @Override  
  84.   public LongWritable getCurrentKey()  
  85.     return key;  
  86.    
  87.   
  88.   @Override  
  89.   public Text getCurrentValue()  
  90.     return value;  
  91.    
  92.   
  93.     
  94.   public float getProgress()  
  95.     if (start == end)  
  96.       return 0.0f;  
  97.     else  
  98.       return Math.min(1.0f, (pos start) (float)(end start));//读取进度由已读取InputSplit大小比总InputSplit大小  
  99.      
  100.    
  101.     
  102.   public synchronized void close() throws IOException  
  103.     if (in != null)  
  104.       in.close();   
  105.      
  106.    
0 0
原创粉丝点击