Hadoop-0.20.0源代码分析(05)

以文件流作为一个切面,阅读Hadoop源代码org.apache.hadoop.fs包中源代码。关于流,分为输入流和输出流两种,下面也这样简单划分为两类进行阅读分析。

输入流类

与输入流相关的接口和类的继承层次关系如下所示:

  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接口定义如下:

  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 plain copy print ?
  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类,并实现上述这两个接口,必须实现接口中定义的操作:

  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 plain copy print ?
  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   

首先看抽象的输入流类,实现的源代码如下所示:

  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. }  

 输出流类

与输出流相关的接口和类的继承层次关系如下所示:

  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,该类的实现如下所示:

  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输出流对象:

  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。

基于上面构造方法,缺省设置一些参数,得到如下两个重载的构造方法:

  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类实现的方法如下所示:

  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方法表示实现同步流缓冲区的操作,使得缓冲的流对象与原始输出流对象同步,保证写入数据的正确性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值