hdfs的FileSystem API详解程序员
2023-06-13 09:20:21 时间
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")); }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/7216.html
服务器部署程序员系统优化网站设置运维相关文章
- Python 删除HDFS过期文件
- HDFS原理概念扫盲
- Hadoop分布式文件系统HDFS
- hadoop hdfs命令 脚本源码_hadoop启动hdfs命令
- 个人号微信api接口开发_API接口授权系统php源码
- 通过Zookeeper获取激活的HDFS节点
- PySpark从hdfs获取词向量文件并进行word2vec
- hdfs-shell操作详解程序员
- 利用Java API通过路径过滤上传多文件至HDFS详解大数据
- Hadoop综合练习第三节–HDFS读写文件操作详解大数据
- HDFS源码分析(六)—–租约详解大数据
- Hadoop分布式文件系统–HDFS结构分析详解大数据
- HDFS、YARN和MapReduce简介详解大数据
- 掌握Oracle数据库API的神奇力量(oracle数据库api)
- HDFS与MongoDB: 数据存储的新维度(hdfs与mongodb)
- Linux加密API:安全加固保护您的数据(linux加密api)
- 探讨Oracle是否有API接口(oracle有api吗)
- MySQL数据库API: 打开新世界的大门(mysql数据库api)
- 使用Linux命令查看HDFS文件系统详细信息(linux查看hdfs)
- 深入理解Linux内核API(linux内核api)
- 利用HDFS实现MySQL数据的快速导入(hdfs 导入mysql)
- 使用C语言搭建MySQL数据库API(c语言mysql api)
- 数据库存储的API文档从离线文件变有效信息(api文档存mysql)
- API连接MySQL数据库实现快速灵活的数据存取(api连mysql数据库)
- 研究Oracle API接口,助推业务发展(oracle api接口)
- Redis读写分离API实现提升效率(redis读写分离api)