FileSystem
FileSystem时文件系统api,通过其对HDFS文件系统进行操作。
FileSystem获取方式:
- 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;
FileSystem获取文件输入流:
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);
}
}
}
FsDataInputStream对象
该对象实现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;
}
FileSystem写入数据
- 创建文件
public FSDataOutputStream create(Path f) throws IOException
create方法有很多重载,其会新建文件,并且默认会在文件父目录不存在时自行创建,因此如果
想在父目录不存在时抛出异常,可以在方法内使用exists()进行判断。
- append()追加
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只允许对一个打开的文件顺序写入,
或向一个已有文件添加,即不支持文件尾部以外的其他位置的写入。
FileSystem创建目录
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<paths.length;i++){
paths[i] = new Path(args[i]);
}
FileStatus[] status = fs.listStatus(paths);
Path[] listedPaths = FileUtil.stat2Paths(status);
for(Path p :listedPaths){
System.out.println(p);
}
}
}
PathFilter对象
- 通配符匹配文件
PathFilter为hadoop的匹配接口
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);
}
}