HDFS的Java访问接口

来源:互联网 发布:手机淘宝咋退货 编辑:程序博客网 时间:2024/05/21 18:40

得到filesystem的实例

 有两个静态方法可以得到filesystem接口的实例

 public static FileSystem get(Configuration conf) throws IOException
 public static FileSystem get(URI uri, Configuration conf) throws IOException

 第一个方法得到缺省的文件系统,具体由配置文件的fs.default.name属性决定。

 第二个方法由URI的前缀决定是哪个文件系统(参考前一篇文章),如果URI没有前缀也是得到缺省的文件系统。

 使用open方法得到文件的输入流

 

 有了filesystem的实例,我们就可以调用open方法得到某个文件的输入流了

 public FSDataInputStream open(Path f) throws IOException
 public abstract FSDataInputStream open(Path f, int bufferSize) throws IOException

 第一个方法用缺省的buffer大小4K

 使用FSDataInputStream读数据

 

 FSDataInputStream派生自java.io.DataInputStream,支持随机读取。

 public class FSDataInputStream extends DataInputStream
    implements Seekable, PositionedReadable {

    void seek(long pos) throws IOException;

    public int read(long position, byte[] buffer, int offset, int length) throws IOException;

}

 使用FSDataOutputStream写数据

 filesystem接口创建文件的接口

 public FSDataOutputStream create(Path f) throws IOException

 还可以append到一个已经存在的文件

 public FSDataOutputStream append(Path f) throws IOException

 public class FSDataOutputStream extends DataOutputStream implements Syncable {

   public void write(int b) throws IOException;

   public void write(byte b[], int off, int len) throws IOException;

 }

 filesystem的文件系统操作方法

 public boolean mkdirs(Path f) throws IOException ;

 public void copyToLocalFile(boolean delSrc, Path src, Path dst) throws IOException;

 public abstract boolean delete(Path f, boolean recursive) throws IOException;

 public abstract boolean rename(Path src, Path dst) throws IOException;

 public abstract FileStatus[] listStatus(Path f) throws IOException;