Hadoop-0.20.0源代碼分析(05)

以文件流作爲一個切面,閱讀Hadoop源代碼org.apache.hadoop.fs包中源代碼。關於流,分爲輸入流和輸出流兩種,下面也這樣簡單劃分爲兩類進行閱讀分析。

輸入流類

與輸入流相關的接口和類的繼承層次關係如下所示:

[java] view plaincopy
  1. ◦java.io.InputStream(java.io.Closeable)  
  2.     ◦java.io.FilterInputStream  
  3.         ◦java.io.DataInputStream(implements java.io.DataInput)  
  4.             ◦org.apache.hadoop.fs.FSDataInputStream(implements org.apache.hadoop.fs.Seekable, org.apache.hadoop.fs.PositionedReadable)  
  5.                 ◦org.apache.hadoop.fs.HarFileSystem.HarFSDataInputStream  

FSDataInputStream類實現了Seekable與PositionedReadable接口,賦予了Hadoop文件系統中的文件輸入流分別能夠進行流式搜索與定位流式讀取的語義。

Seekable接口定義如下:

[java] view plaincopy
  1. package org.apache.hadoop.fs;  
  2.   
  3. import java.io.*;  
  4.   
  5. /** Stream that permits seeking. */  
  6. public interface Seekable {  
  7.   /** 
  8.    * 從指定文件中的位置pos,對文件流進行前向搜索。 
  9.    */  
  10.   void seek(long pos) throws IOException;  
  11.     
  12.   /** 
  13.    * 返回文件流中當前偏移位置。 
  14.    */  
  15.   long getPos() throws IOException;  
  16.   
  17.   /** 
  18.    * 從targetPos位置搜索文件數據的一個不同拷貝,搜索到則返回true,否則返回false。 
  19.    */  
  20.   boolean seekToNewSource(long targetPos) throws IOException;  
  21. }  

Seekable 接口中定義的方法,都是基於文件流的位置進行操作的方法,使得在文件系統中或文件系統之間進行流式操作更加方便。

PositionedReadable接口定義如下:

[c-sharp] view plaincopy
  1. package org.apache.hadoop.fs;  
  2.   
  3. import java.io.*;  
  4. import org.apache.hadoop.fs.*;  
  5.   
  6. public interface PositionedReadable {  
  7.   /** 
  8.    * 讀取文件流中最多到length大小的字節,到字節緩衝區buffer中,它是從給定的position位置開始讀取的。  
  9.    * 該讀取方式不改變文件的當前偏移位置offset,並且該方法是線程安全的。 
  10.    */  
  11.   public int read(long position, byte[] buffer, int offset, int length) throws IOException;  
  12.     
  13.   /** 
  14.    * 讀取文件流中length大小的字節,到字節緩衝區buffer中,它是從給定的position位置開始讀取的。 
  15.    * 該讀取方式不改變文件的當前偏移位置offset,並且該方法是線程安全的。 
  16.    */  
  17.   public void readFully(long position, byte[] buffer, int offset, int length) throws IOException;  
  18.     
  19.   /** 
  20.    * 讀取文件流中buffer長度的字節,到字節緩衝區buffer中,它是從給定的position位置開始讀取的 
  21.    * 該讀取方式不改變文件的當前偏移位置offset,並且該方法是線程安全的。 
  22.    */  
  23.   public void readFully(long position, byte[] buffer) throws IOException;  
  24. }  

PositionedReadable接口中定義了三個基於位置來進行流式讀取的操作。

接着,FSDataInputStream類繼承自DataInputStream類,並實現上述這兩個接口,必須實現接口中定義的操作:

[java] view plaincopy
  1. package org.apache.hadoop.fs;  
  2.   
  3. import java.io.*;  
  4.   
  5. public class FSDataInputStream extends DataInputStream implements Seekable, PositionedReadable {  
  6.   
  7.   public FSDataInputStream(InputStream in)  
  8.     throws IOException {  
  9.     super(in); // 調用基類的構造方法,初始化一個基本流類屬性InputStream in  
  10.     if( !(in instanceof Seekable) || !(in instanceof PositionedReadable) ) { // 強制保證InputStream in必須實現Seekable與PositionedReadable這兩個接口。  
  11.       throw new IllegalArgumentException(  
  12.           "In is not an instance of Seekable or PositionedReadable");  
  13.     }  
  14.   }  
  15.     
  16.   public synchronized void seek(long desired) throws IOException {  
  17.     ((Seekable)in).seek(desired); // 設置從in的desired位置開始搜索輸入流流in  
  18.   }  
  19.   
  20.   public long getPos() throws IOException {  
  21.     return ((Seekable)in).getPos();  
  22.   }  
  23.     
  24.   public int read(long position, byte[] buffer, int offset, int length)  
  25.     throws IOException {  
  26.     return ((PositionedReadable)in).read(position, buffer, offset, length);  
  27.   }  
  28.     
  29.   public void readFully(long position, byte[] buffer, int offset, int length)  
  30.     throws IOException {  
  31.     ((PositionedReadable)in).readFully(position, buffer, offset, length);  
  32.   }  
  33.     
  34.   public void readFully(long position, byte[] buffer)  
  35.     throws IOException {  
  36.     ((PositionedReadable)in).readFully(position, buffer, 0, buffer.length);  
  37.   }  
  38.     
  39.   public boolean seekToNewSource(long targetPos) throws IOException {  
  40.     return ((Seekable)in).seekToNewSource(targetPos);   
  41.   }  
  42. }  

FSDataInputStream輸入流類最顯著的特徵就是,能夠基於流的位置而進行流式操作。

另外,在org.apache.hadoop.fs包中還定義了基於RAF(Random Access File)風格的輸入流類,可以隨機讀取該流對象。繼承關係如下所示:

[c-sharp] view plaincopy
  1. ◦java.io.InputStream(implements java.io.Closeable)     
  2.     ◦org.apache.hadoop.fs.FSInputStream(implements org.apache.hadoop.fs.Seekable, org.apache.hadoop.fs.PositionedReadable)   
  3.         ◦org.apache.hadoop.fs.FSInputChecker   
  4.             ◦org.apache.hadoop.fs.ChecksumFileSystem.ChecksumFSInputChecker   

首先看抽象的輸入流類,實現的源代碼如下所示:

[java] view plaincopy
  1. package org.apache.hadoop.fs;  
  2.   
  3. import java.io.*;  
  4.   
  5. public abstract class FSInputStream extends InputStream implements Seekable, PositionedReadable {  
  6.   /** 
  7.    * 從給定的偏移位置pos開始搜索,下一次讀取就從該位置開始讀取。 
  8.    */  
  9.   public abstract void seek(long pos) throws IOException;  
  10.   
  11.   /** 
  12.    * 返回文件的當前前向偏移位置 
  13.    */  
  14.   public abstract long getPos() throws IOException;  
  15.   
  16.   /** 
  17.    * 搜索不同的文件數據的拷貝,如果搜索到則返回true,否則返回false 
  18.    */  
  19.   public abstract boolean seekToNewSource(long targetPos) throws IOException;  
  20.   
  21.   public int read(long position, byte[] buffer, int offset, int length)  
  22.     throws IOException {  
  23.     synchronized (this) {  
  24.       long oldPos = getPos();  
  25.       int nread = -1;  
  26.       try {  
  27.         seek(position);  
  28.         nread = read(buffer, offset, length);  
  29.       } finally {  
  30.         seek(oldPos);  
  31.       }  
  32.       return nread;  
  33.     }  
  34.   }  
  35.       
  36.   public void readFully(long position, byte[] buffer, int offset, int length)  
  37.     throws IOException {  
  38.     int nread = 0;  
  39.     while (nread < length) {  
  40.       int nbytes = read(position+nread, buffer, offset+nread, length-nread);  
  41.       if (nbytes < 0) {  
  42.         throw new EOFException("End of file reached before reading fully.");  
  43.       }  
  44.       nread += nbytes;  
  45.     }  
  46.   }  
  47.       
  48.   public void readFully(long position, byte[] buffer)  
  49.     throws IOException {  
  50.     readFully(position, buffer, 0, buffer.length);  
  51.   }  
  52. }  

 輸出流類

與輸出流相關的接口和類的繼承層次關係如下所示:

[java] view plaincopy
  1. ◦java.io.OutputStream(implements java.io.Closeable, java.io.Flushable)  
  2.     ◦java.io.FilterOutputStream  
  3.         ◦java.io.DataOutputStream  
  4.             ◦org.apache.hadoop.fs.FSDataOutputStream(implements org.apache.hadoop.fs.Syncable)  

FSDataOutputStream輸出流類內部實現了一個基於位置的緩衝輸出流類PositionCache,該類的實現如下所示:

[java] view plaincopy
  1. /** 
  2.  * 該PositionCache類是一個緩衝流類,對輸出流的位置進行緩存。 
  3.  */  
  4. rivate static class PositionCache extends FilterOutputStream {  
  5.   
  6.  private FileSystem.Statistics statistics;  
  7.  long position; // 緩存中輸出流對象out的偏移位置  
  8.   
  9.  public PositionCache(OutputStream out, FileSystem.Statistics stats, long pos) throws IOException {  
  10.    super(out); // 初始化從基類繼承下來的OutputStream out對象  
  11.     statistics = stats;  
  12.    position = pos;  
  13.  }  
  14.   
  15.  public void write(int b) throws IOException {  
  16.    out.write(b); // 向輸出流對象out中寫入一個字節b  
  17.    position++; // 緩存中輸出流的偏移位置加1  
  18.    if (statistics != null) {  
  19.      statistics.incrementBytesWritten(1); // 更新文件系統的統計數據對象  
  20.    }  
  21.  }  
  22.   
  23.  public void write(byte b[], int off, int len) throws IOException {  
  24.    out.write(b, off, len); //   
  25.    position += len; // 更新緩存  
  26.    if (statistics != null) {  
  27.      statistics.incrementBytesWritten(len); // 更新文件統計數據  
  28.    }  
  29.  }  
  30.      
  31.  public long getPos() throws IOException {  
  32.    return position;   // 返回輸出流中當前待寫入位置  
  33.  }  
  34.    
  35.  public void close() throws IOException {  
  36.    out.close(); // 關閉輸出流  
  37.  }  

創建一個PositionCache緩衝流對象以後,可以向該文件輸出緩衝流中寫入相關的數據,作爲緩衝使用,其中相關數據包括:文件系統(FileSystem)統計信息FileSystem.Statistics、當前待寫入流的位置。每當需要向文件系統中寫入數據,都會從PositionCache緩衝流中獲取到一個寫入位置(也就是,要從流中的該位置開始寫入)。

FSDataOutputStream輸出流類的通過一個PositionCache緩衝流來構造一個FSDataOutputStream輸出流對象:

[java] view plaincopy
  1. public FSDataOutputStream(OutputStream out, FileSystem.Statistics stats, long startPosition) throws IOException {  
  2.   super(new PositionCache(out, stats, startPosition)); // 緩衝了out流,緩存的數據對象包括stats、startPosition  
  3.   wrappedStream = out;  
  4. }  

實例化FSDataOutputStream類,能夠獲取到當前用於要寫入數據的流對象,也就是該類包裝的輸出流類OutputStream類型屬性wrappedStream,其中wrappedStream就是一個OutputStream。

基於上面構造方法,缺省設置一些參數,得到如下兩個重載的構造方法:

[java] view plaincopy
  1. @Deprecated  
  2. public FSDataOutputStream(OutputStream out) throws IOException {  
  3.   this(out, null);  
  4. }  
  5.   
  6. public FSDataOutputStream(OutputStream out, FileSystem.Statistics stats)  
  7.   throws IOException {  
  8.   this(out, stats, 0);  
  9. }  

該FSDataOutputStream類實現的方法如下所示:

[java] view plaincopy
  1. public long getPos() throws IOException {  
  2.   return ((PositionCache)out).getPos();  
  3. }  
  4.   
  5. public void close() throws IOException {  
  6.   out.close();           
  7. }  
  8.   
  9. public OutputStream getWrappedStream() {  
  10.   return wrappedStream;  
  11. }  
  12.   
  13. /** wrappedStream是必須實現Syncable接口的流類,強制同步全部緩衝區 */  
  14. public void sync() throws IOException {  
  15.   if (wrappedStream instanceof Syncable) {  
  16.     ((Syncable)wrappedStream).sync();  
  17.   }  
  18. }  

其中,sync方法表示實現同步流緩衝區的操作,使得緩衝的流對象與原始輸出流對象同步,保證寫入數據的正確性。 


發佈了4 篇原創文章 · 獲贊 2 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章