hdfs的FileSystem API

来源:互联网 发布:基于java博客 编辑:程序博客网 时间:2024/04/30 08:04

FileSystem类是与hadoop的文件系统交互的重要接口。虽然我们只是着重于HDFS的实现,但我们在编码时一般也要注意代码在FileSystem不同子类文件系统之间的可移植性。这是非常有用的,比如说你可以非常方便的直接用同样的代码在你的本地文件系统上进行测试。

FSDataInputStream:

与URL的openStream()方法返回InputStream不同,FileSystem的open()方法返回的是一个FSDataInputStream对象(继承关系:java.io.InputStream --> java.io.FilterInputStream --> java.io.DataInputStream --> org.apache.hadoop.fs.FSDataInputStream)。由于FSDataInputStream实现了Closeable ,   DataInput ,   PositionedReadable ,   Seekable 等接口,你可以从流中的任意一个位置读取数据。 

下面我们看一些使用这些API的例子:

//将hdfs中的文件从控制台打印出来public class hdfsTest{public static void main(String[] args) throws IOException, URISyntaxException{String PATH="hdfs://192.168.12.100:9000/data/test.txt";Configuration conf=new Configuration();FileSystem fs=FileSystem.get(URI.create(PATH), conf);InputStream in = null;try{in=fs.open(new Path(PATH));  //使用open函数获取文件的输入流IOUtils.copyBytes(in, System.out, 4096, false);}finally{IOUtils.closeStream(in);}}}//seek可以定位文件的绝对位置,skip定位到现在位置的相对位置public static void main(String[] args) throws IOException, URISyntaxException{String PATH="hdfs://192.168.12.100:9000/data/test.txt";Configuration conf=new Configuration();FileSystem fs=FileSystem.get(URI.create(PATH), conf);FSDataInputStream in = null;try{in=fs.open(new Path(PATH)); //open返回FSDataInputStream对象IOUtils.copyBytes(in, System.out, 4096, false);in.seek(0);IOUtils.copyBytes(in, System.out, 4096, false);}finally{IOUtils.closeStream(in);}}//create()用来创建一个文件,此方法有多个重载版本,允许我们指定是否需要强制覆盖已有的文件,文件备份数量,写入文件时所用的缓冲区大小,文件块和文件权限public static void main(String[] args) throws IOException, URISyntaxException{String dst="hdfs://192.168.12.100:9000/data/test.txt";String src="/root/Desktop/test.txt";Configuration conf=new Configuration();FileSystem fs=FileSystem.get(URI.create(dst), conf);InputStream in =new BufferedInputStream(new FileInputStream(src));OutputStream out=fs.create(new Path(dst),new Progressable(){  //progressable对象显示了在创建文件的过程public void progress() {System.out.print(".");}});IOUtils.copyBytes(in, out, 4096, true);}//FileStatus获取文件或者目录的信息,这个类封装了文件长度,块大小,备份,修改时间,所有者以及权限信息public static void main(String[] args) throws IOException, URISyntaxException{String dst="hdfs://192.168.12.100:9000/data/test.txt";Configuration conf=new Configuration();FileSystem fs=FileSystem.get(URI.create(dst), conf);FileStatus status=fs.getFileStatus(new Path(dst));System.out.println(status.getPath());System.out.println(status.getLen());System.out.println(status.getBlockSize());System.out.println(status.getOwner());System.out.println(status.getGroup());System.out.println(status.getPermission());System.out.println(status.getReplication());}//列出某些目录的内容public static void main(String[] args) throws IOException, URISyntaxException{String dst="hdfs://192.168.12.100:9000/";Configuration conf=new Configuration();FileSystem fs=FileSystem.get(URI.create(dst), conf);Path path=new Path(dst);FileStatus[] status=fs.listStatus(path);Path[] listedPaths=FileUtil.stat2Paths(status); ////fileUtil中的stat2paths方法将一个FileStatus对象数组转换为Path对象数组for(Path p:listedPaths){System.out.println(p);}}//mkdirs是创建目录的方法public static void main(String[] args) throws IOException, URISyntaxException{String src="/dirs";Path path=new Path(src);Configuration conf=new Configuration();FileSystem fs=FileSystem.get(URI.create("hdfs://192.168.12.100:9000/"),conf);fs.mkdirs(path);}//rename重命名文件public static void main(String[] args) throws IOException, URISyntaxException{String file="hdfs://192.168.12.100:9000/data/test.txt";String newfile="/data/mytest.txt";Configuration conf=new Configuration();FileSystem fs=FileSystem.get(URI.create(file),conf);fs.rename(new Path(file), new Path(newfile));}//删除文件public static void main(String[] args) throws IOException, URISyntaxException{String file="hdfs://192.168.12.100:9000/data/";Configuration conf=new Configuration();FileSystem fs=FileSystem.get(URI.create(file),conf);fs.delete(new Path("/data/mytest.txt"));}



0 0
原创粉丝点击