本文共 3966 字,大约阅读时间需要 13 分钟。
FileSystem时文件系统api,通过其对HDFS文件系统进行操作。
- public static FileSystem get(Configuration conf) throws IOException; - public static FileSystem get(URI uri, Configuration conf) throws IOException; - public static FileSystem get(URI uri, Configuration conf,String user) throws IOException;
- public static LocalFileSystem getLocal(Configuration config) throws IOException;
public FSDataInputStream open(Path f) throws IOException; //默认缓冲区大小为4KB - public abstract FSDataInputStream(Path f,int bufferSize) throws IOException;
public class FileSysemCat{ public static void main(String[] args) throws Exception{ String uri = args[0]; Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(uri),conf); InpuStream in = null; try{ in = fs.open(new Path(uri)); IOUtils.copyBytes(in , System.out,4096,false); }finally{ IOUtils.closeStream(in); } } }
该对象实现DataInputStream接口,支持随机访问,可以从流的任意位置读取数据。
public class FSDataInputStream extends DataInputStream implements Seekable , PositionedReadable{ } //Seek接口支持在文件中找到位置,并且有getPos获取当前文件偏移量public interface Seekable{ void seek(long pos) throws IOException; long getPos() throws IOException; boolean seekToNewSource(long targetPos) throws IOException;}//PositionedReadable接口,从指定偏移量处读取文件的一部分public interface PositionedReadable{ public int read(long position , byte[] buffer, int offset , int length) throws IOException; public void readFully(long position , bytes[] buffer, int offset, int length)throws IOException; public void readFully(long position , byte[] buffer ) throws IOException;}
public FSDataOutputStream create(Path f) throws IOException
create方法有很多重载,其会新建文件,并且默认会在文件父目录不存在时自行创建,因此如果
想在父目录不存在时抛出异常,可以在方法内使用exists()进行判断。public FSDataOutputStream append (Path f) throws IOException
public class FileCopyWithProgress{ public static void main(String[] args) Exception{ String localSrc = args[0]; String dst = args[1]; InputStream in = new BufferedInputStream(new FileInputStream(localSrc)); Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(dst),conf); OutputStream out = fs.create(new Path(dst)), new Progressable(){ public void progress(){ System.out.println("."); } }); IOUtils.copyBytes(in , out , 4096 , true); } }
Progressable是一个用于传递回调的接口,同于将数据写入datanode的进度通知给用户.
FSDataOutputStream对象
FSDataOutputStream对象由FileSystem使用create()返回,由查询文件当前位置的方法public class FSDataOutputStream extends DataOutputStream implements Syncable{ public long getPos() throws IOException{ }}
与FSDataInputStream不同,FSDataOutputStream不允许定位。因为HDFS只允许对一个打开的文件顺序写入,
或向一个已有文件添加,即不支持文件尾部以外的其他位置的写入。public boolean mkdirs(Path f) throws IOException
该方法会创建不存在的父目录,实际上在使用create()写入文件时会自动生成所有父目录。
FileStatus 类封装了文件系统中文件和目录的元数据,包括文件长度,块大小,副本,修改时间,所有者等。
//打印Hadoop中一些路径的文件歇息 public class ListStatus{ public static void main(String[] args) throws Exception{ String uri = args[0]; Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(uri),conf); Path[] paths = new Path[args.length]; for(int i = 0; i
public interface PathFilter{ boolean accept(Path path);}
public class RegexExcludePathFilter implements PathFilter{ private final String reges; public RegexExcludePathFilter(String regex){ this.regex = regex; } public boolean accept(Path path){ return path.toString().matches(regex); }}
转载地址:http://yykai.baihongyu.com/