3.1 客户端环境准备
参照:https://www.cnblogs.com/qiu-hua/p/13285628.html,注意pom.xml中的hadoop版本号一定要和集群上的版本号保持一致
3.2 HDFS的API实操
- HDFS创建文件夹
- HDFS文件上传
- HDFS文件下载
- HDFS文件更名和移动
- HDFS删除文件和目录
- HDFS文件详情查看
- HDFS文件和文件夹判断
public class HdfsClient{
private FileSystem fs;
@Before
public void init() throws IOException, InterruptedException, URISyntaxException {
// 连接的集群nn地址
URI uri = new URI("hdfs://hadoop102:8082");
// 创建一个配置文件
Configuration configuration = new Configuration();
// 用户
String user = "atguigu";
// 获取到客户对象
FileSystem fs = FileSystem.get(uri, configuration, user);
}
@After
public void close() throws IOException {
// 关闭资源
fs.close();
}
// 创建文件夹
@Test
public void testMkdirs() throws IOException, InterruptedException, URISyntaxException {
// 2 创建目录
fs.mkdirs(new Path("/xiyou/huaguoshan"));
}
// 上传
// 参数优先级:hdfs-default.xml => hdfs-site.xml => 项目资源resource目录下的hdfs-site.xml配置文件 => 代码中的配置设置
@Test
public void testPut() throws IOException {
// 2 上传文件,ctrl+p查看参数,参数一:表示删除本地原数据,参数二:目的路径是否允许覆盖;参数三:本地路径位置;参数四:目的地路径
fs.copyFromLocalFile(false, false, new Path("D:\\sunwukong.txt"), new Path("hdfs://hadoop102/xiyou/huaguoshan"));
}
// 下载
@Test
public void testGet() throws IOException {
// 2 下载文件,参数一:表示删除hdfs上的路径文件,参数二:原文件hdfs的路径,参数三:目标地址windows本地路径, 参数四:是否开启本地校验,false开启校验
fs.copyToLocalFile(false, new Path("hdfs://hadoop102/xiyou/huaguoshan"), new Path("D:\\"), true);
}
// 删除
@Test
public void testRm() throws IOException {
// 1.删除文件,参数一:要删除的hdfs路径,参数二:是否递归删除
fs.delete(new Path("hdfs://hadoop102/jdk-8u212-linux-x64.tar.gz"), false);
// 2.删除空目录
fs.delete(new Path("hdfs://hadoop102/xiyou"), false);
// 3.删除非空目录,参数一:要删除的hdfs路径,参数二:是否递归删除
fs.delete(new Path("hdfs://hadoop102/jinguo"), true);
}
// 文件的更名和移动
@Test
public void testmv() throws IOException {
// 1.修改文件名称,参数一:原文件路径,参数二:目标文件路径
fs.rename(new Path("hdfs://hadoop102/input/word.txt"), new Path("hdfs://hadoop102/input/ss.txt"));
}
// 2.文件的移动和更名
fs.rename(new Path("hdfs://hadoop102/input/ss.txt"), new Path("hdfs://hadoop102/cls.txt"));
}
// 3.目录的更名
fs.rename(new Path("hdfs://hadoop102/input"), new Path("hdfs://hadoop102/output"));
}
// 获取文件详情信息
@Test
public void fileDetail() throws IOException {
// 1.获取所有文件信息
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("hdfs://hadoop102/"), true);
// 2.遍历文件
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));
}
}
// 判断是文件夹,还是文件
@Test
public void testFile() throws IOException {
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for (FileStatus status : listStatus){
if(status.isFile()){
System.out.println("文件:" + status.getPath().getName());
}else{
System.out.println("目录:" + status.getPath().getName());
}
}
}
}