// 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop:8020"), configuration,"root");
// 创建目录
if (!fs.exists(new Path("/photo"))){
fs.mkdirs(new Path("/photo"));
}
//上传文件
fs.copyFromLocalFile(new Path("G:/hdfs/default.png"), new Path("/photo"));
//下载文件
fs.copyToLocalFile(new Path("/photo/default.png"), new Path("G:/hdfs/default2.png"));
//重命名文件
fs.rename(new Path("/photo/default.png"), new Path("/photo/123.png"));
//查看文件信息
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/photo"), true);
while (listFiles.hasNext()) {
LocatedFileStatus fileStatus = listFiles.next();
System.out.println("========" + fileStatus.getPath() + "=========");
System.out.println(fileStatus.getPermission());
System.out.println(fileStatus.getOwner());
System.out.println(fileStatus.getGroup());
System.out.println(fileStatus.getLen());
System.out.println(fileStatus.getModificationTime());
System.out.println(fileStatus.getReplication());
System.out.println(fileStatus.getBlockSize());
System.out.println(fileStatus.getPath().getName());
// 获取块信息
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
System.out.println(Arrays.toString(blockLocations));
}
//删除文件
fs.delete(new Path("/photo/123.png"), true);
// 关闭资源
fs.close();
HDFS 的 API 操作
最新推荐文章于 2024-08-24 00:00:00 发布