博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《Hadoop权威指南》 Hadoop文件系统操作接口
阅读量:4170 次
发布时间:2019-05-26

本文共 3966 字,大约阅读时间需要 13 分钟。

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

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); }}

转载地址:http://yykai.baihongyu.com/

你可能感兴趣的文章
Spring cloud之eureka搭建
查看>>
Spring boot集成jxls实现导出excel功能
查看>>
Spring boot集成jxls实现导入功能
查看>>
移动端页面引入vconsole调试
查看>>
Spring Boot 的配置文件的格式
查看>>
Spring boot读取配置的方式
查看>>
Spring boot读取配置的方式(指定配置文件)
查看>>
Spring Boot切换不同环境配置
查看>>
Spring boot兼容旧Spring项目的方式
查看>>
Spring boot常用启动器
查看>>
Spring cloud之eureka服务注册
查看>>
Spring boot打jar包分离静态资源
查看>>
Mybatis自动生成代码
查看>>
Eureka搭建高可用集群
查看>>
Spring cloud之Ribbon初识
查看>>
删除Maven仓库的.lastUpdate文件
查看>>
Spring cloud之Ribbon搭建
查看>>
TreeMap 与 HashMap 的区别
查看>>
初识CAS
查看>>
Fork/Join 框架
查看>>