依赖包

<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.3</version>
</dependency>

代码样例

写入文件,上传,下载,可以自己开发的网盘

package com.lys.app.oozie;

import java.net.URI;

import org.apache.hadoop.fs.*;
import org.apache.hadoop.conf.Configuration;

/**
* @Auther: liuysh
* @Date: 2020/12/1 16:23
* @Description: http://node223.xdata:50070/explorer.html#/test/lys
*/
public class HdfsUtils {

public static FileSystem getFileSystem() throws Exception{
Configuration conf = new Configuration();
conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
// conf.set("fs.defaultFS", "hdfs://node223.xdata:8020");
FileSystem fileSystem= FileSystem.get(new URI("hdfs://node223.xdata:8020"), conf, "hdfs");
return fileSystem;
}



public static void mkDir(String path) throws Exception{
FileSystem fileSystem=getFileSystem();
Path dirHdfsPath = new Path(path);
if (!fileSystem.exists(dirHdfsPath))
fileSystem.mkdirs(dirHdfsPath);

}


public static void write(String path, String content) throws Exception{
write(path, content.getBytes());
}

public static void write(String path, byte[] file) throws Exception {
FileSystem fs = getFileSystem();
FSDataOutputStream outputStream = null;

String dirPath = path.substring(0, path.lastIndexOf("/"));
Path dirHdfsPath = new Path(dirPath);
if (!fs.exists(dirHdfsPath))
fs.mkdirs(dirHdfsPath);
outputStream = fs.create(new Path(path));
outputStream.write(file);

}


public static void copyFromLocalFile(Path src, Path dst) throws Exception {
FileSystem fs = getFileSystem();
fs.copyFromLocalFile(src,dst);

}

public static void copyToLocalFile(Path src, Path dst) throws Exception {
FileSystem fs = getFileSystem();
// 第一个false表示不会删除源文件, 第二个true表示使用本地文件系统
fs.copyToLocalFile(false,src,dst,true);

}


private static void list(Path path) throws Exception {
FileSystem fs = getFileSystem();
FileStatus[] fileStatuses = fs.listStatus(path);
for (FileStatus file:fileStatuses
) {
if(file.isFile()){
System.out.println(file.getPath().toString());
}else{
System.out.println(file.getPath().toString());
list(file.getPath());
}
}
fs.close();
}



public static void main(String[] args) throws Exception{
mkDir("/test/lys");
write("/test/lys/20201202.sql","show tables;");



copyToLocalFile(new Path("/test/lys/shell/VGG.pdf"),new Path("E:\\person_management\\hdfs001\\VGG.pdf"));

list(new Path("/test"));

}
}