hdfs的FileSystem API
2015-11-30 22:57
387 查看
FileSystem类是与hadoop的文件系统交互的重要接口。虽然我们只是着重于HDFS的实现,但我们在编码时一般也要注意代码在FileSystem不同子类文件系统之间的可移植性。这是非常有用的,比如说你可以非常方便的直接用同样的代码在你的本地文件系统上进行测试。
与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"));
}
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"));
}
相关文章推荐
- Android Manifest 用法
- 什么是 GraphQL?
- 详解HDFS Short Circuit Local Reads
- Spark RDD API详解(一) Map和Reduce
- Hadoop_2.1.0 MapReduce序列图
- 使用Hadoop搭建现代电信企业架构
- 单机版搭建Hadoop环境图文教程详解
- 一个小型js框架myJSFrame附API使用帮助
- 详细分析交换机、路由器、集线器的区别和联系
- PowerShell打开或关闭光驱
- 批处理的api WMIC学习体会有感第1/2页
- 批处理 API实现文件下载的代码第1/2页
- Lua教程(十七):C API简介
- 强制删除工具 xdelbox xdelbox1.5正式版下载
- 揪出交换机端口背后“凶手”导致网速太慢
- 电脑重启后突然检测不到硬盘的原因分析与解决办法
- C#中设计、使用Fluent API
- Google官方支持的NodeJS访问API,提供后台登录授权
- PQ分区出错! 巧用Ghost急速补救的绝妙办法
- 深入C++中API的问题详解