一、、HDFS 常用类
Configuration 配置
Path 路径
FileSystem 文件系统
Stream 流
IOUtils IO工具
API文档
二、类解析
(1)Configuration


配置文件加载顺序

设置/获取参数方法

(2)Path

Path 常用方法

(3)FileSystem

创建文件

打开文件

文件追加

从本地拷贝文件到HDFS

从HDFS拷贝文件到本地

创建目录

删除及重命名

获取文件或目录信息

设置文件或目录属性


(4)Stream
HDFS输入流

HDFS输出流

(5)IOUtils
IOUtils 构造方法

IOUtils 拷贝流方法


三、HDFS 依赖的jar包及Java Doc位置

四、例子
(1)创建HDFS文件(createNewFile)
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
Path path = new Path(fileName);
boolean isCreated = hdfs.createNewFile(path);
(2)从本地拷贝文件到HDFS(copyFromLocalFile)
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
Path srcPath = new Path(srcFile);
Path dstPath = new Path(dstFile);
hdfs.copyFromLocalFile(srcPath,dstPath)
(3)从HDFS读取文件(open)
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
Path path = new Path(dstFile);
FSDataInputStream inputStream = hdfs.open(path);
Text line = new Text()
LineReader liReader = new LineReader(inputStream);while (liReader.readLine(line) > 0) {
System.out.println(line);
}
inputStream.close();
(4)追加方式写入HDFS文件(append)
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
Path path= new Path(dstFile);
FSDataOutputStream out = hdfs.append(path);
//在文件尾部,追加数据
out.close();
(5)列出目录下的所有文件(listStatus)
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
Path dir = new Path(dirName);
FileStatus[] fileStatus = hdfs.listStatus(new Path[]{dir});