HDFS的API操作
HDFS环境准备
- 准备Hadoop的Windows10的客户端
- 准备JDK8(也就是对应Java1.8)
- 配置Hadoop的Windows10的客户端的环境变量配置
- 创建maven工程
- 导入依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
</dependencies>
公共部分代码解析
Declaration
Provides access to configuration parameters.
翻译过来就是:提供对配置参数的访问。
第一步 获取对配置参数的访问
Configuration conf = new Configuration();
第二步 创建文件系统的访问对象
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.174.137:9000"), conf, "root");
第三步 操作文件系统
最后一步 关闭资源
fs.close();
部分代码解析
1文件上传
Delcaration
The src file is on the local disk. Add it to FS at
the given dst name and the source is kept intact afterwards
@param src path
@param dst path
翻译:
第一个参数src 来源路径
第二个参数dst 目标路径
从源路径上传到目标路径
fs.copyFromLocalFile(new Path("e:/banzhang.txt"), new Path("/banzhang.txt"));
2文件下载
The src file is under FS, and the dst is on the local disk.
Copy it from FS control to the local dst name.
delSrc indicates if the src will be removed or not.
@param delSrc whether to delete the src
@param src path
@param dst path
翻译:
第一个参数 是否删除源文件
第二个参数 来源文件
第三个参数 目标路径
fs.copyToLocalFile(false, new Path("/banzhang.txt"), new Path("e:/banhua.txt"), true);
3文件夹删除
Delete a file.
@param f the path to delete.
@param recursive if path is a directory and set to
true, the directory is deleted else throws an exception. In
case of a file the recursive can be set to either true or false.
@return true if delete is successful else false.
@throws IOException
翻译:
第一个参数 目标路径(操作路径)
第二个参数 是否递归删除 如果删除的是个目录,目录里面还有别的文件,则会报错
fs.delete(new Path("/0508/"), true);
4文件名更改
第一个参数 源路径
第二个路径 更名之后的路径
fs.rename(new Path("/banzhang.txt"), new Path("/banhua.txt"));
5文件详情查看
// 2 获取文件详情
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
while(listFiles.hasNext()){
LocatedFileStatus status = listFiles.next();
// 输出详情
// 文件名称
System.out.println(status.getPath().getName());
// 长度
System.out.println(status.getLen());
// 权限
System.out.println(status.getPermission());
// 分组
System.out.println(status.getGroup());
// 获取存储的块信息
BlockLocation[] blockLocations = status.getBlockLocations();
for (BlockLocation blockLocation : blockLocations) {
// 获取块存储的主机节点
String[] hosts = blockLocation.getHosts();
for (String host : hosts) {
System.out.println(host);
}
}
System.out.println("-----------班长的分割线----------");
}
6文件和文件夹判断
// 2 判断是文件还是文件夹
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : listStatus) {
// 如果是文件
if (fileStatus.isFile()) {
System.out.println("f:"+fileStatus.getPath().getName());
}else {
System.out.println("d:"+fileStatus.getPath().getName());
}
}