Hadoop中HDFS的API操作、HDFS文件上传(测试参数优先级)、copyFromLocalFile参数解读、HDFS文件下载、文件更名和移动、删除文件和目录、文件详情查看、文件和文件夹判断

7.HDFS的API操作

7.2HDFS的API案例实操

7.2.1HDFS文件上传(测试参数优先级)

在这里插入图片描述
在这里插入图片描述

package com.summer.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author Redamancy
 * @create 2022-08-15 17:59
 */


public class HdfsClient {
    private FileSystem fs;
    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        //连接的集群nn地址
        URI uri = new URI("hdfs://hadoop102:8020");
        //创建一个配置文件
        Configuration configuration = new Configuration();
        //用户
        String user = "summer";
        //获取到了客户端对象
        fs = FileSystem.get(uri, configuration,user);
    }
    @After
    public void close() throws IOException {
        //关闭资源
        fs.close();
    }

    @Test
    public void testPut() throws IOException {
        //参数解读:
        // 参数一:表示删除原数据,为true表示上传完文件后,原数据文件将会删除;为false表示上传完文件后,原数据文件将不会删除;
        // 参数二:是否允许覆盖,为true表示不管目的地地址是否有同名的文件都会覆盖;
        // 为false表示目的地地址有同名的文件则报错org.apache.hadoop.fs.PathExistsException: `hdfs://hadoop102/xiyou/huaguoshan/sunwukong.txt': Target hdfs://hadoop102/xiyou/huaguoshan/sunwukong.txt already exists;
        // 参数三:原数据路径Windows;参数四:目的地路径HDFS
        fs.copyFromLocalFile(false,true,new Path("D:\\sunwukong.txt"),new Path("hdfs://hadoop102/xiyou/huaguoshan"));
    }
}

7.2.1.1copyFromLocalFile参数解读
//参数解读:
        // 参数一:表示删除原数据,为true表示上传完文件后,原数据文件将会删除;为false表示上传完文件后,原数据文件将不会删除;
        // 参数二:是否允许覆盖,为true表示不管目的地地址是否有同名的文件都会覆盖;
        // 为false表示目的地地址有同名的文件则报错org.apache.hadoop.fs.PathExistsException: `hdfs://hadoop102/xiyou/huaguoshan/sunwukong.txt': Target hdfs://hadoop102/xiyou/huaguoshan/sunwukong.txt already exists;
        // 参数三:原数据路径Windows;参数四:目的地路径HDFS

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

7.2.1.2副本个数

在这里插入图片描述

为什么这里的副本有三个,因为在下面设置的
在这里插入图片描述
参数优先级:服务器的自定义配置(hdfs-site.xml) >服务器的默认配置(hdfs-default.xml)

7.2.1.3将hdfs-site.xml拷贝到项目的resources资源目录下
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>

<configuration>
	<property>
		<name>dfs.replication</name>
         <value>1</value>
	</property>
</configuration>

在这里插入图片描述

如果创建的文件副本变为了1,则

在这里插入图片描述
在这里插入图片描述

参数优先级:在项目资源目录下的用户自定义配置文件(如在resources下创建的hdfs-site.xml,可以看上面的过程) >服务器的自定义配置(hdfs-site.xml) >服务器的默认配置(hdfs-default.xml)

7.2.1.4客户端代码中设置的值

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
为2,则参数优先级排序:(1)客户端代码中设置的值 >(2)在项目资源目录下的用户自定义配置文件(如在resources下创建的hdfs-site.xml,可以看上面的过程) >(3)服务器的自定义配置(hdfs-site.xml) >(4)服务器的默认配置(hdfs-default.xml)

7.2.2HDFS文件下载

package com.summer.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author Redamancy
 * @create 2022-08-15 17:59
 */


public class HdfsClient {
    private FileSystem fs;
    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        //连接的集群nn地址
        URI uri = new URI("hdfs://hadoop102:8020");
        //创建一个配置文件
        Configuration configuration = new Configuration();
        configuration.set("dfs.replication","2");
        //用户
        String user = "summer";
        //获取到了客户端对象
        fs = FileSystem.get(uri, configuration,user);
    }
    @After
    public void close() throws IOException {
        //关闭资源
        fs.close();
    }
    @Test
    public void testGet() throws IOException {
        //参数的解读:参数一:原文件是否删除;参数二:原文件路径HDFS;参数三:目标地址路径Windows;参数四:是否要校验CRC
        fs.copyToLocalFile(false,new Path("hdfs://hadoop102/xiyou/huaguoshan"),new Path("D:\\"),false);
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
CRC文件是一个校验文件,保证文件传输完整。

注意:如果执行上面代码,下载不了文件,有可能是你电脑的微软支持的运行库少,需要安装一下微软运行库。

7.2.3HDFS文件更名和移动

package com.summer.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author Redamancy
 * @create 2022-08-15 17:59
 */


public class HdfsClient {
    private FileSystem fs;
    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        //连接的集群nn地址
        URI uri = new URI("hdfs://hadoop102:8020");
        //创建一个配置文件
        Configuration configuration = new Configuration();
        configuration.set("dfs.replication","2");
        //用户
        String user = "summer";
        //获取到了客户端对象
        fs = FileSystem.get(uri, configuration,user);
    }
    @After
    public void close() throws IOException {
        //关闭资源
        fs.close();
    }
    //文件的更名和移动
    @Test
    public void testmv() throws IOException {
        //参数解读:参数一:原文件路径;参数二:目标文件路径
        //对文件名称的修改
        fs.rename(new Path("/testinput/word.txt"),new Path("/testinput/summer.txt"));

        //文件的更名和移动
        fs.rename(new Path("/testinput/summer.txt"),new Path("/after.txt"));
        
		//目录更名
        fs.rename(new Path("/testinput"),new Path("/testoutput"));
    }
}

在这里插入图片描述

7.2.3.1对文件名称的修改

核心代码:

fs.rename(new Path("/testinput/word.txt"),new Path("/testinput/summer.txt"));

在这里插入图片描述

在这里插入图片描述

7.2.3.2文件的更名和移动

核心代码:

 fs.rename(new Path("/testinput/summer.txt"),new Path("/after.txt"));

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

7.2.3.3目录更名

核心代码:

fs.rename(new Path("/testinput"),new Path("/testoutput"));

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

7.2.4HDFS删除文件和目录

package com.summer.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author Redamancy
 * @create 2022-08-15 17:59
 */


public class HdfsClient {
    private FileSystem fs;
    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        //连接的集群nn地址
        URI uri = new URI("hdfs://hadoop102:8020");
        //创建一个配置文件
        Configuration configuration = new Configuration();
        configuration.set("dfs.replication","2");
        //用户
        String user = "summer";
        //获取到了客户端对象
        fs = FileSystem.get(uri, configuration,user);
    }
    @After
    public void close() throws IOException {
        //关闭资源
        fs.close();
    }
    //文件删除
    @Test
    public void testRm() throws IOException {
        //参数解读:参数一:要删除的路径;参数二:是否递归删除
        //删除文件
        fs.delete(new Path("/hadoop-3.1.3.tar.gz"),false);

        //删除空目录
        fs.delete(new Path("/xiyou"),false);
        //删除非空目录
        fs.delete(new Path("/jinguo"),true);
    }
}

7.2.4.1删除文件

核心代码:

fs.delete(new Path("/hadoop-3.1.3.tar.gz"),false);

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

7.2.4.2删除空目录

核心代码:

fs.delete(new Path("/xiyou"),false);

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

7.2.4.3删除非空目录

核心代码:

fs.delete(new Path("/jinguo"),true);

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

7.2.5HDFS文件详情查看

查看文件名称、权限、长度、块信息

//获取文件详细信息
    @Test
    public void fileDetail() throws IOException {

        //获取所有文件信息
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("hdfs://hadoop102/"), 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.getReplication());
            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));
        }

在这里插入图片描述

=============hdfs://hadoop102/after.txt=============
rw-r--r--
summer
3
26
1659793848099
3
134217728
after.txt
[0,26,hadoop102,hadoop104,hadoop103]
=============hdfs://hadoop102/hadoop-3.1.3.tar.gz=============
rw-r--r--
summer
2
338075860
1660640604122
2
134217728
hadoop-3.1.3.tar.gz
[0,134217728,hadoop104,hadoop102, 134217728,134217728,hadoop103,hadoop102, 268435456,69640404,hadoop102,hadoop103]
=============hdfs://hadoop102/tmp/logs/summer/logs-tfile/application_1659955855387_0001/hadoop103_40403=============
rw-r-----
summer
3
66228
1659955977487
3
134217728
hadoop103_40403
[0,66228,hadoop102,hadoop104,hadoop103]
=============hdfs://hadoop102/tmp/logs/summer/logs-tfile/application_1659955855387_0001/hadoop104_43491=============
rw-r-----
summer
3
67455
1659955978021
3
134217728
hadoop104_43491
[0,67455,hadoop102,hadoop104,hadoop103]
=============hdfs://hadoop102/tmp/logs/summer/logs-tfile/application_1659963897517_0001/hadoop102_33697=============
rw-r-----
summer
3
32036
1659969846561
3
134217728
hadoop102_33697
[0,32036,hadoop102,hadoop104,hadoop103]
=============hdfs://hadoop102/tmp/logs/summer/logs-tfile/application_1659963897517_0001/hadoop104_42477=============
rw-r-----
summer
3
103855
1659969846884
3
134217728
hadoop104_42477
[0,103855,hadoop102,hadoop104,hadoop103]

Process finished with exit code 0

7.2.6HDFS文件和文件夹判断

package com.summer.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

/**
 * @author Redamancy
 * @create 2022-08-15 17:59
 */


public class HdfsClient {
    private FileSystem fs;
    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        //连接的集群nn地址
        URI uri = new URI("hdfs://hadoop102:8020");
        //创建一个配置文件
        Configuration configuration = new Configuration();
        configuration.set("dfs.replication","2");
        //用户
        String user = "summer";
        //获取到了客户端对象
        fs = FileSystem.get(uri, configuration,user);
    }
    @After
    public void close() throws IOException {
        //关闭资源
        fs.close();
    }
    //判断是文件夹还是文件
    @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());
            }
        }
    }
}

在这里插入图片描述

  • 10
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
1. 展示HDFS文件列表 要展示HDFS文件列表,可以使用Hadoop提供的HDFS API。以下是使用Java代码在SSM框架展示HDFS文件列表的示例: ```java public List<String> listHdfsFiles(String path) throws IOException { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path hdfsPath = new Path(path); RemoteIterator<LocatedFileStatus> files = fs.listFiles(hdfsPath, true); List<String> fileList = new ArrayList<>(); while (files.hasNext()) { fileList.add(files.next().getPath().toString()); } return fileList; } ``` 2. 上传文件 要上传文件HDFS,可以使用Hadoop提供的HDFS API。以下是使用Java代码在SSM框架上传文件HDFS的示例: ```java public void uploadFileToHdfs(String localFilePath, String hdfsPath) throws IOException { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path srcPath = new Path(localFilePath); Path dstPath = new Path(hdfsPath); fs.copyFromLocalFile(srcPath, dstPath); } ``` 3. 删除文件删除HDFS文件,可以使用Hadoop提供的HDFS API。以下是使用Java代码在SSM框架删除HDFS文件的示例: ```java public void deleteHdfsFile(String hdfsPath) throws IOException { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path path = new Path(hdfsPath); fs.delete(path, true); } ``` 4. 下载文件 要从HDFS下载文件,可以使用Hadoop提供的HDFS API。以下是使用Java代码在SSM框架HDFS下载文件的示例: ```java public void downloadHdfsFile(String hdfsPath, String localPath) throws IOException { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path srcPath = new Path(hdfsPath); Path dstPath = new Path(localPath); fs.copyToLocalFile(srcPath, dstPath); } ``` 以上示例的代码仅供参考,具体的实现方式可以根据实际需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Redamancy_06

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值