• Hadoop(HDFS)


    一、HDFS 概述

    1.1 HDFS 产出背景及定义

    1 HDFS 产生背景
    随着数据量越来越大,在一个操作系统存不下所有的数据,那么就分配到更多的操作系
    统管理的磁盘中,但是不方便管理和维护,迫切 需要一种系统来管理多台机器上的文件 ,这
    就是分布式文件管理系统。 HDFS 只是分布式文件管理系统中的一种。
    2 HDFS 定义
    HDFS Hadoop Distributed File System ),它是一个文件系统 ,用于存储文件,通过目
    录树来定位文件; 其次,它是分布式的 ,由很多服务器联合起来实现其功能,集群中的服务
    器有各自的角色。
    HDFS 的使用场景:适合一次写入,多次读出的场景。 一个文件经过创建、写入和关闭
    之后就不需要改变。
    1.2 HDFS 优缺点

     

    1.3 HDFS 组成架构

     

    1.4 HDFS 文件块大小(面试重点)

     

     

    二、HDFS Shell 操作

    Version:0.9 StartHTML:0000000105 EndHTML:0000000445 StartFragment:0000000141 EndFragment:0000000405

    2.3.1 下载
    1 -moveFromLocal :从本地 剪切 粘贴到 HDFS
    1. cd /opt/module/hadoop-3.1.3/
    2. hadoop fs -mkdir /sanguo
    3. vim shuguo.txt
    4. 输入:
    5. shuguo
    6. hadoop fs -moveFromLocal ./shuguo.txt /sanguo
     

     2-copyFromLocal:从本地文件系统中拷贝文件到 HDFS 路径去

    1. vim weiguo.txt
    2. 输入:
    3. weiguo
    4. hadoop fs -copyFromLocal weiguo.txt /sanguo
    3 -put :等同于 copyFromLocal ,生产环境更习惯用 put
    1. vim wuguo.txt
    2. 输入:
    3. wuguo
    4. hadoop fs -put ./wuguo.txt /sanguo

    4-appendToFile:追加一个文件到已经存在的文件末尾

    1. vim liubei.txt
    2. 输入:
    3. liubei
    4. hadoop fs -appendToFile liubei.txt /sanguo/shuguo.txt

    2.3.2下载

    1 -copyToLocal :从 HDFS 拷贝到本地

    hadoop fs -copyToLocal /sanguo/shuguo.txt ./

    2-get:等同于 copyToLocal,生产环境更习惯用 get

    hadoop fs -get /sanguo/shuguo.txt ./shuguo2.txt

    2.3.3 HDFS 直接操作

    1)-ls: 显示目录信息

     hadoop fs -ls /sanguo

    2)-cat:显示文件内容

     hadoop fs -cat /sanguo/shuguo.txt

    3)-chgrp、-chmod、-chown:Linux 文件系统中的用法一样,修改文件所属权限

     hadoop fs -chmod 666 /sanguo/shuguo.txt

     hadoop fs -chown atguigu:atguigu /sanguo/shuguo.txt

    4)-mkdir:创建路径

     hadoop fs -mkdir /jinguo

    5)-cp:从 HDFS 的一个路径拷贝到 HDFS 的另一个路径

     hadoop fs -cp /sanguo/shuguo.txt /jinguo

    6)-mv:在 HDFS 目录中移动文件

     hadoop fs -mv /sanguo/wuguo.txt /jinguo

     hadoop fs -mv /sanguo/weiguo.txt /jinguo

    7)-tail:显示一个文件的末尾 1kb 的数据

     hadoop fs -tail /jinguo/shuguo.txt

    8)-rm:删除文件或文件夹

     hadoop fs -rm /sanguo/shuguo.txt

    9)-rm -r:递归删除目录及目录里面内容

     hadoop fs -rm -r /sanguo

    10)-du 统计文件夹的大小信息

     hadoop fs -du -s -h /jinguo

    输出

    27 81 /jinguo

     hadoop fs -du -h /jinguo

    输出

    14 42 /jinguo/shuguo.txt

    7 21 /jinguo/weiguo.txt

    6 18 /jinguo/wuguo.tx

    11)setrep 设置副本

     hadoop fs -setrep 10 /jinguo/shuguo.txt

    三、HDFS API 操作

    1、在idea新建maven项目

     

    项目建立后,引入依赖

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.apache.hadoop</groupId>
    4. <artifactId>hadoop-client</artifactId>
    5. <version>3.1.3</version>
    6. </dependency>
    7. <dependency>
    8. <groupId>junit</groupId>
    9. <artifactId>junit</artifactId>
    10. <version>4.12</version>
    11. </dependency>
    12. <dependency>
    13. <groupId>org.slf4j</groupId>
    14. <artifactId>slf4j-log4j12</artifactId>
    15. <version>1.7.30</version>
    16. </dependency>
    17. </dependencies>

     创建工具类HdfsClienty

    1、创建文件夹

    1. import org.apache.hadoop.conf.Configuration;
    2. import org.apache.hadoop.fs.FileSystem;
    3. import org.apache.hadoop.fs.Path;
    4. import org.junit.After;
    5. import org.junit.Before;
    6. import org.junit.Test;
    7. import java.io.IOException;
    8. import java.net.URI;
    9. import java.net.URISyntaxException;
    10. /**
    11. * @Author sxd
    12. * @create 2022/11/28 23:45
    13. */
    14. public class HdfsClient {
    15. private FileSystem fileSystem;
    16. @Before
    17. public void init() throws URISyntaxException, IOException, InterruptedException {
    18. // 链接集群地址
    19. URI uri = new URI("hdfs://hadoop102:8020");
    20. // 创建配置文件
    21. Configuration configuration = new Configuration();
    22. //用户
    23. String user = "root";
    24. // 获取客户端对象
    25. fileSystem = FileSystem.get(uri, configuration, user);
    26. }
    27. @After
    28. public void close() throws IOException {
    29. //关闭资源
    30. fileSystem.close();
    31. }
    32. @Test
    33. public void testMkdirs() throws IOException, URISyntaxException,
    34. InterruptedException {
    35. //创建文件夹
    36. fileSystem.mkdirs(new Path("/xiyou/huaguoshan1"));
    37. }
    38. // 上传操作
    39. @Test
    40. public void testPut() throws IOException {
    41. /*
    42. 参数解读:
    43. 参数一:标识删除原数据
    44. 参数二:是否允许覆盖
    45. 参数三:原数据路径
    46. 参数四:目的地路径
    47. */
    48. fileSystem.copyFromLocalFile(true,true,new Path("/Users/sunxiangdao/java/study/folder/hadoop/sunwukong"),new Path("hdfs://hadoop102/xiyou/huaguoshan"));
    49. }
    50. }

    运行testMkdirs后看到后台加入文件夹

    2、上传

    ①本地创建文件sunwukong.txt,

    运行testPut后,实现本地上传到服务器

    ②在resource文件夹下新建hdfs-site.xml文件

    1. "1.0" encoding="UTF-8"?>
    2. "text/xsl" href="configuration.xsl"?>
    3. dfs.replication
    4. 1

    启动上传功能发现上传的文件副本为1

    ③代码里设置副本后,启动上传功能发现上传的文件副本为2

    ④参数优先级为
     hdfs-default.xml => hdfs-site.xml =>在项目资源目录下的配置 文件 =》代码里面的配置文件

     3、下载

    1. @Test
    2. public void testGet() throws IOException {
    3. /*
    4. 参数解读:
    5. 参数一:标识删除原数据
    6. 参数二:原数据路径
    7. 参数三:目的地路径
    8. 参数四:
    9. */
    10. fileSystem.copyToLocalFile(false,new Path("hdfs://hadoop102/xiyou/huaguoshan"),new Path("/Users/sunxiangdao/java/study/folder/hadoop/"),false);
    11. }

    启动后发现多出crc文件,是用作hdfs与本地校验,校验后正确才能解析文件,当校验改为true就不校验

    删除

    1. @Test
    2. public void testRm() throws IOException {
    3. /*
    4. 参数解读:
    5. 参数一:要删除的路径
    6. 参数二:是否递归删除
    7. 删除文件
    8. */
    9. // fileSystem.delete(new Path("/正面.JPG"),false);
    10. //删除空文件
    11. // fileSystem.delete(new Path("/xiyou"),false);
    12. // 删除非空目录
    13. fileSystem.delete(new Path("/jinguo"), true);
    14. }

    移动和更名

    1. // 移动和更名文件
    2. @Test
    3. public void testMv() throws IOException {
    4. /*
    5. 参数解读:
    6. 参数一:原文件路径
    7. 参数二:目标文件路径
    8. */
    9. // fileSystem.rename(new Path("/input/word.txt"),new Path("/input/ss.txt"));
    10. // 文件更名和移动
    11. // fileSystem.rename(new Path("/input/ss.txt"),new Path("/cls.txt"));
    12. // 目录更名
    13. fileSystem.rename(new Path("/input"), new Path("/output"));
    14. }

    获取文件详细信息

    1. // 获取文件详细信息
    2. @Test
    3. public void fileDetail() throws IOException {
    4. // 获取所有文件信息
    5. RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/"), true);
    6. // 遍历文件
    7. while (listFiles.hasNext()){
    8. LocatedFileStatus fileStatus = listFiles.next();
    9. System.out.println("0----地址"+fileStatus.getPath()+"---0");
    10. System.out.println("0----权限"+fileStatus.getPermission());
    11. System.out.println("0----用户"+fileStatus.getOwner());
    12. System.out.println("0----分组"+fileStatus.getGroup());
    13. System.out.println("0----大小"+fileStatus.getLen());
    14. System.out.println("0----上次修改时间"+fileStatus.getModificationTime());
    15. System.out.println("0----副本"+fileStatus.getReplication());
    16. System.out.println("0----块大小"+fileStatus.getBlockSize());
    17. System.out.println("0----名称"+fileStatus.getPath().getName());
    18. // 获取块信息
    19. BlockLocation[] blockLocations = fileStatus.getBlockLocations();
    20. System.out.println("0----块服务器地址"+Arrays.toString(blockLocations));
    21. }
    22. }

    判断文件和文件夹

    1. @Test
    2. public void testFile() throws IOException {
    3. FileStatus[] listStatus = fileSystem.listStatus(new Path("/"));
    4. for (FileStatus status : listStatus) {
    5. if(status.isFile()){
    6. System.out.println("文件:"+status.getPath().getName());
    7. }else {
    8. System.out.println("目录:"+status.getPath().getName());
    9. }
    10. }
    11. }

    hdfs读写流程

  • 相关阅读:
    Dockerfile使用介绍(入门教程)
    Linux学习第19天:Linux并发与竞争实例: 没有规矩不成方圆
    树莓派通过网线连接电脑(校园网也能连接),实现SSH连接
    ROS2进阶第三章 -- 机器人语音交互之ros集成科大讯飞中文语音库,实现语音控制机器人小车
    Linux学习第34天:Linux LCD 驱动实验(一):星星之火可以燎原
    go入门快速学习笔记(有java基础)
    Spring-FactoryBean的源码——简略解析(下)
    controller-informer
    【Unity2D】关卡编辑好帮手——TileMap
    vue3.4的更新,保证你看的明明白白
  • 原文地址:https://blog.csdn.net/ONLYYD/article/details/128087228