Hadoop的HDFS操作

  1. 在本地创建目录 /home/marry ,并在该目录下创建三个空文件,文件名分别为1.txt,2.txt,3.txt

  2. ```
    root@master:/home# mkdir marry
    root@master:/home# cd marry
    root@master:/home/marry# touch 1.txt
    root@master:/home/marry# touch 2.txt
    root@master:/home/marry# touch 3.txt

    
    3.   在HDFS上创建目录 /demo/test; /demo/test1 /demo/test2 /demo/test3 /demo/test4
    
    4.   ![](https://strongwillpro.oss-cn-beijing.aliyuncs.com/img/实验四2.PNG)
    
    5.   ```
         root@master:/home/marry# hadoop fs -mkdir -p /demo/test
         root@master:/home/marry# hadoop fs -mkdir  /demo/test1
         root@master:/home/marry# hadoop fs -mkdir  /demo/test2
         root@master:/home/marry# hadoop fs -mkdir  /demo/test3
         root@master:/home/marry# hadoop fs -mkdir  /demo/test4
  3. 将本地的 1.txt 上传到 HDFS 的 /demo/test1 下,将 2.txt 和 3.txt 上传到 /demo/test 下

  4. ```
    root@master:/home/marry# hadoop fs -put /home/marry/1.txt /demo/test1
    root@master:/home/marry# hadoop fs -put /home/marry/2.txt /home/marry/3.txt /demo/test

    
    9.   ![](https://strongwillpro.oss-cn-beijing.aliyuncs.com/img/实验四3.PNG)
    
    10.   ![](https://strongwillpro.oss-cn-beijing.aliyuncs.com/img/实验四4.PNG)
    
    11.   ![](https://strongwillpro.oss-cn-beijing.aliyuncs.com/img/实验四5.PNG)
    
    12.   ![](https://strongwillpro.oss-cn-beijing.aliyuncs.com/img/实验四6.PNG)
    
    13.   将hello world写入到本地的1.txt文件中,再将其内容追加到HDFS的1.txt中,并查看是否追加成功
    
    14.   ![](https://strongwillpro.oss-cn-beijing.aliyuncs.com/img/20221011211422.png)
    
    15.   ```
          root@master:/home/marry# echo hello world > 1.txt
          root@master:/home/marry# hadoop fs -appendToFile 1.txt /demo/test1/1.txt
          root@master:/home/marry# hadoop fs -cat /demo/test1/1.txt
  5. 将HDFS中的2.txt重命名为4.txt,并将4.txt复制到/demo/test4下

  6. ```
    root@master:/home/marry# hadoop fs -mv /demo/test/2.txt /demo/test/4.txt
    root@master:/home/marry# hadoop fs -cp /demo/test/4.txt /demo/test4

    
    19.   将HDFS中的3.txt移动到/demo/test3下,文件名为33.txt
    
    20.   ![](https://strongwillpro.oss-cn-beijing.aliyuncs.com/img/20221011212341.png)
    
    21.   ```
          root@master:/home/marry# hadoop fs -mv /demo/test/3.txt /demo/test3/33.txt
  7. 将HDFS上的4.txt文件复制到本地的/home/marry下。

  8. ```
    root@master:/home/marry# hadoop fs -get /demo/test4/4.txt /home/marry

    
    25.   删除HDFS上的4.txt文件。
    
    26.   ![](https://strongwillpro.oss-cn-beijing.aliyuncs.com/img/20221011212717.png)
    
    27.   ```
          root@master:/home/marry# hadoop fs -rm /demo/test4/4.txt