【大数据】四、HDFS 基础操作

news/2024/5/20 5:09:35 标签: 大数据, hdfs, hadoop

IDE 连接

在本地电脑上解压 hadoop.tar.gz,配置环境变量

之后 去github 上 把 winutil.exe 和 hadoop.dll 下载到 hadoop 的bin 文件夹下

再修改 etc/hadoop-env.cmd 中的 JDK 路径

我们使用 IDEA 打开一个 JAVA Maven项目,进行测试

注意,这里的包导入全部都是在 hadoop 下的导入:

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>3.3.6</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.3.6</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>3.3.6</version>
        </dependency>

进行简单的入门级体会:

public class HDFSApi {

    @Test
    public void getFileSystemTest() throws IOException {
        
        // 创建配置文件对象用于读取配置文件信息
        // 其默认会读取 core-default.xml  hdfs-site.xml  mapred-default.xml  yarn-default.xml 四大配置文件
        // 如果项目中存在配置文件  core-site.xml  hdfs-site.xml  mapred-site.xml  yarn-site.xml  则会读取这四个配置文件
        // 配置文件读取完成之后,我们还可以对配置文件进行修改
        Configuration conf = new Configuration();

        // 进行 属性配置,若不配置 获取的是 org.apache.hadoop.fs.LocalFileSystem 这个不是我们需要的对象
        // 进行配置之后,获取到的就是 org.apache.hadoop.hdfs.DistributedFileSystem 对象了,这个对象使我们操作 HDFS 所需要的核心对象
        conf.set("fs.defaultFS", "hdfs://192.168.202.101:9820");

        FileSystem fs = FileSystem.get(conf);
        
        System.out.println(fs.getClass().getName());
        
    }
}

文件操作

上传与下载

文件的上传与下载操作

/**
 * 另外要注意的问题是:在操作 HDFS 时,我们操作 HDFS 使用的用户和我们操作当前操作系统的用户保持了一致,这样会导致我们没有对于当前用户的写操作的权限
 * 这就需要我们配置操作 HDFS 的用户
 */
public class HDFSApi {

    FileSystem fs;

    @Before
    public void getFileSystemTest() throws IOException {

        // 配置操作 HDFS 的用户
        System.setProperty("HADOOP_USER_NAME", "hadoop");

        // 创建配置文件对象用于读取配置文件信息
        // 其默认会读取 core-default.xml  hdfs-site.xml  mapred-default.xml  yarn-default.xml 四大配置文件
        // 如果项目中存在配置文件  core-site.xml  hdfs-site.xml  mapred-site.xml  yarn-site.xml  则会读取这四个配置文件
        // 配置文件读取完成之后,我们还可以对配置文件进行修改
        Configuration conf = new Configuration();

        // 进行 属性配置,若不配置 获取的是 org.apache.hadoop.fs.LocalFileSystem 这个不是我们需要的对象
        // 进行配置之后,获取到的就是 org.apache.hadoop.hdfs.DistributedFileSystem 对象了,这个对象使我们操作 HDFS 所需要的核心对象
        conf.set("fs.defaultFS", "hdfs://192.168.202.101:8020");

        fs = FileSystem.get(conf);

        System.out.println(fs.getClass().getName());

    }

    @After
    public void closeFileSystem() throws IOException {
        fs.close();
    }

    /**
     * 文件上传
     */
    @Test
    public void uploadTest() throws IOException {
        // 配置要上传的文件和文件上传的目标路径
        Path src = new Path("C:/Users/M_Bai/Desktop/bejson_gen_beans.zip");
        Path dst = new Path("/");

        // 上传文件
        fs.copyFromLocalFile(src, dst);
    }
    /**
     * 文件下载
     */
    @Test
    public void downloadTest() throws IOException {
        // 配置要下载的文件路径以及文件要下载到的位置
        Path src = new Path("/bejson_gen_beans.zip");
        Path dst = new Path("C:/Users/M_Bai/Desktop/new.mp4");

        fs.copyToLocalFile(src, dst);
    }
}

文件夹操作

    @Test
    public void mkdirTest() throws IOException {
        fs.mkdirs(new Path("/test_mkdir"));
    }

    @Test
    public void deleteTest() throws IOException {
        // 这里的第二个 bool 型参数代表是否递归删除
        fs.delete(new Path("/test_mkdir"), true);
        //  删除单个文件
        fs.delete(new Path("/file_test.txt"));
    }

    // 重命名
    @Test
    public void renameTest() throws IOException {
        fs.rename(new Path("/file_test.txt"), new Path("/file.txt"));
    }

    // 判断文件、文件夹是否存在
    @Test
    public void existTest() throws IOException {
        boolean isExist = fs.exists(new Path("file.txt"));
        System.out.println(isExist);
    }

IOUtils

上传文件

    @Test
    public void ioUtilsTest() throws IOException {
        // 基础配置与 FileSystem 对象的创建
        System.setProperty("HADOOP_USER_NAME", "hadoop");
        Configuration configuration = new Configuration();
        configuration.set("fs.defaultFS", "hdfs://192.168.202.101:8020");
        FileSystem fileSystem = FileSystem.get(configuration);

        // 将要上传的文件转换为流
        FileInputStream input = new FileInputStream("D:/BigData/hadoopTest/hadoopAPI/hadoopAPI/src/main/java/qinghe/hdfs/TestApi.java");

        // 创建输出到 HDFS 的文件的流
        FSDataOutputStream output = fileSystem.create(new Path("/TestApi.java"));

        // 利用 IOUtils 将输入流复制给输出流,也就是将输出流写入到 HDFS 的文件中
        IOUtils.copyBytes(input, output, configuration);

        // 关闭流
        IOUtils.closeStream(input);
        IOUtils.closeStream(output);
    }

下载一个文件:

    @Test
    public void ioUtilsTestDownload() throws IOException {
        System.setProperty("HADOOP_USER_NAME", "hadoop");
        Configuration configuration = new Configuration();
        configuration.set("fs.defaultFS", "hdfs://192.168.202.101:8020");
        FileSystem fileSystem = FileSystem.get(configuration);

        // 将要下载的文件转化为流
        FSDataInputStream input = fileSystem.open(new Path("/TestApi.java"));

        // 创建写入到本地的流
        FileOutputStream output = new FileOutputStream("C:/Users/M_Bai/Desktop/nnnnnnnnnnnnnnnnnnnnnn.mp4");

        IOUtils.copyBytes(input, output, configuration);
        IOUtils.closeStream(input);
        IOUtils.closeStream(output);
    }

文件信息的查看:

    /**
     * 查看文件的状态信息
     */
    @Test
    public void listFileStatusTest() throws IOException {
        // 文件信息需要使用 iterator 进行遍历,每一个文件占用一个迭代器
        RemoteIterator<LocatedFileStatus> iterator = fs.listLocatedStatus(new Path("/TestApi.java"));
        while (iterator.hasNext()) {
            // 获取到当前遍历的文件
            LocatedFileStatus fileStatus = iterator.next();
            System.out.println("基本信息:" + fileStatus);

            // 获取到当前文件的块的集合
            BlockLocation[] blockLocations = fileStatus.getBlockLocations();
            // 遍历该文件所在的所有块
            for (BlockLocation blockLocation : blockLocations) {
                System.out.println("当前块的所有副本信息:" + Arrays.toString(blockLocation.getHosts()));
                System.out.println("当前块的大小:" + blockLocation.getLength());
                System.out.println("当前块的副本的 IP 地址:" + Arrays.toString(blockLocation.getNames()));
            }
            System.out.println("系统块的大小:" + fileStatus.getBlockSize());
            System.out.println("当前文件的总大小:" + fileStatus.getLen());
        }
    }

http://www.niftyadmin.cn/n/5444515.html

相关文章

敏捷开发——第二次作业JS/服务器的部署

部署 Web 服务器 1. 安装 Apache HTTP 服务器并部署静态网页应用 ⭐⭐ 默认情况下&#xff0c;Apache 在 /var/www/html 目录下寻找要提供服务的文件。可以将静态网页文件放置在这个目录下 2.安装 Nginx 并部署静态页面应用 3. 实践部分 1. 2. 3. 在 /var/www/html 目录下…

无人机拦截与对抗

配置yolo 1.框架 yolo框架使用darknet_ros&#xff0c;这个版本支持yolov3和yolov4的配置文件 2.报错 &#xff08;1&#xff09;CUDA报错 nvcc fatal : Unsupported gpu architecture compute_30.&#xff08;1&#xff09;查看显卡匹配型号&#xff1a;https://blog.csdn.…

基于python+vue中医学习服务管理系统flask-django-php-nodejs

随着世界经济信息化、全球化的到来和互联网的飞速发展&#xff0c;推动了各行业的改革。若想达到安全&#xff0c;快捷的目的&#xff0c;就需要拥有信息化的组织和管理模式&#xff0c;建立一套合理、动态的、交互友好的、高效的中医学习服务管理系统。当前的信息管理存在工作…

MySQL索引(图文并茂)

目录 一、索引的概念 二、索引的作用 三、创建索引的原则依据 四、索引的分类和创建 1、索引的分类 2、索引的创建 2.1 普通索引 2.1.1 直接创建索引 2.1.2 修改表方式创建 2.1.3 创建表的时候指定索引 2.2 唯一索引 2.2.1 直接创建唯一索引 2.2.2 修改表方式创建 …

用好商用无人自助咖啡机,真正实现“AI智能”制饮!

随着科技的不断进步和智能化技术的广泛应用&#xff0c;商用无人自助咖啡机作为餐饮行业的新宠&#xff0c;正逐渐改变着我们的生活方式和消费体验。通过结合人工智能技术&#xff0c;这些无人自助咖啡机正在实现真正的“AI智能”制饮&#xff0c;为消费者带来全新的咖啡体验。…

Mall4j开源商城系统-基于SpringBoot+Vue系统开发介绍

今天来介绍一款非常不错的Mall4j开源商城系统 Mall4j开源商城&#xff0c;一个基于spring boot、spring oauth2.0、mybatis、redis的轻量级、前后端分离、防范xss攻击、拥有分布式锁&#xff0c;为生产环境多实例完全准备&#xff0c;数据库为b2b2c设计&#xff0c;拥有完整sku…

大型语言模型(LLM)全解读

大型语言模型&#xff08;Large Language Model&#xff0c;LLM&#xff09;是指使用大规模数据集进行预训练的神经网络模型&#xff0c;用于生成人类类似的自然语言文本。LLM在自然语言处理&#xff08;Natural Language Processing&#xff0c;NLP&#xff09;领域有着广泛的…

Linux中关于库的概念

一、概念 库是一个二进制文件&#xff0c;包含的代码可被程序调用。例如标准C库、数学库、线程库... 库有源码&#xff0c;可下载后编译&#xff1b;也可以直接安装二进制包。 库是事先编译好的&#xff0c;可以复用的代码。 在OS上运行的程序基本上都要使用库&#xff0c;…