其他分享
首页 > 其他分享> > HDFS的基本操作

HDFS的基本操作

作者:互联网

前提要求:已成功搭建Hadoop集群
【实验描述】
Hadoop提供也hadoop分布式文件系统交互的命令,通过了解Hadoop shell命令的用法。掌握对hadoop分布式系统的操作;以下是一些基础命令操作。

  1. 查看hdfs分布式系统根目录下所有文件和文件夹信息
[root@hadoop1 ~]# hdfs dfs -ls /
  1. 利用hdfs dfs –mkdir 命令在hdfs上创建test1、test2和test3目录。
[root@hadoop1 ~]# hdfs dfs -mkdir /test1
[root@hadoop1 ~]# hdfs dfs -mkdir /test2
[root@hadoop1 ~]# hdfs dfs -mkdir /test3
[root@hadoop1 ~]# hdfs dfs -ls /
Found 3 items
drwxr-xr-x   - root supergroup          0 2021-11-12 10:18 /test1
drwxr-xr-x   - root supergroup          0 2021-11-12 10:18 /test2
drwxr-xr-x   - root supergroup          0 2021-11-12 10:20 /test3
  1. 在本地系统编辑文件test1.txt,操作命令如下:
[root@hadoop1 ~]# vim test1.txt
添加以下内容:
filename test1.txt

#同样操作创建test2.txt和test3.txt文件。
#利用hdfs dfs –put命令上传文件test1.txt到hdfs的/test1目录;上传文件test2.txt到hdfs的/test2.txt目录;上传文件test3.txt到hdfs的/test3.txt目录,操作命令如下:
[root@hadoop1 ~]# hdfs dfs -put test1.txt /test1
[root@hadoop1 ~]# hdfs dfs -put test2.txt /test2
[root@hadoop1 ~]# hdfs dfs -put test3.txt /test3

#上传成功后,利用hdfs dfs –ls命令进行查看
[root@hadoop1 ~]# hdfs dfs -ls /test1
Found 1 items
-rw-r--r--   3 root supergroup         19 2021-11-12 10:40 /test1/test1.txt
[root@hadoop1 ~]# hdfs dfs -ls /test2
Found 1 items
-rw-r--r--   3 root supergroup         19 2021-11-12 10:40 /test2/test2.txt
[root@hadoop1 ~]# hdfs dfs -ls /test3
Found 1 items
-rw-r--r--   3 root supergroup         19 2021-11-12 10:41 /test3/test3.txt
  1. 利用hdfs dfs –text命令查看文件的内容
[root@hadoop1 ~]# hdfs dfs -text /test1/test1.txt
filename test1.txt
#或命令hdfs dfs –cat查看文件内容
[root@hadoop1 ~]# hdfs dfs -cat /test1/test1.txt
filename test1.txt
  1. 利用hdfs dfs –appendToFile命令实现对hdfs分布式系统上文件内容的追加
在本地编辑testtmp.txt文件,文件内容为“filename testtmp.txt”,将testtmp.txt文件的内容追加到hdfs分布式系统的/test1/test1.txt文件后。
[root@hadoop1 ~]# hdfs dfs -appendToFile testtmp.txt /test1/test1.txt
#查看/test1/test1.txt文件内容
[root@hadoop1 ~]# hdfs dfs -text /test1/test1.txt			
filename test1.txt
filename testtmp.txt
  1. 利用hdfs dfs –cp命令将hdfs分布式系统下/test1/test1.txt文件复制到/test2/目录中
[root@hadoop1 ~]# hdfs dfs -ls /test2
Found 2 items
-rw-r--r--   3 root supergroup         40 2021-11-12 10:57 /test2/test1.txt
-rw-r--r--   3 root supergroup         19 2021-11-12 10:40 /test2/test2.txt
  1. 利用hdfs dfs –rm命令将hdfs分布式系统下/test2/test1.txt文件删除
[root@hadoop1 ~]# hdfs dfs -rm /test2/test1.txt
Deleted /test2/test1.txt
[root@hadoop1 ~]# hdfs dfs -ls /test2
Found 1 items			
-rw-r--r--   3 root supergroup         19 2021-11-12 10:40 /test2/test2.txt
  1. 利用hdfs dfs –mv 命令将hdfs分布式系统上/test2/test2.txt文件移动到/test1目录中
[root@hadoop1 ~]# hdfs dfs -ls /test1
Found 1 items
-rw-r--r--   3 root supergroup         40 2021-11-12 10:55 /test1/test1.txt
[root@hadoop1 ~]# hdfs dfs -mv /test2/test2.txt /test1
[root@hadoop1 ~]# hdfs dfs -ls /test1
Found 2 items
-rw-r--r--   3 root supergroup         40 2021-11-12 10:55 /test1/test1.txt
-rw-r--r--   3 root supergroup         19 2021-11-12 10:40 /test1/test2.txt
[root@hadoop1 ~]# hdfs dfs -ls /test2    // 该目录下的文件已经删除
  1. 利用hdfs dfs -moveFromLocal 命令将本地文件testtmp.txt文件从本地移动到hdfs 系统的/test2目录中(本地文件会被删除)
[root@hadoop1 ~]# hdfs dfs -ls /test2
[root@hadoop1 ~]# hdfs dfs -moveFromLocal testtmp.txt /test2
[root@hadoop1 ~]# hdfs dfs -ls /test2
Found 1 items
-rw-r--r--   3 root supergroup         21 2021-11-12 11:02 /test2/testtmp.txt
同时在本地利用ls命令进行查看,会发现testtmp.txt文件被删除。
  1. 在本地系统的根目录下建立test文件夹,利用hdfs dfs –get命令将hdfs分布式系统上/test1/test1.txt文件下载到本地根目录下的test文件夹,操作命令如下。
[root@hadoop1 ~]# mkdir /test
[root@hadoop1 ~]# cd /test
[root@hadoop1 test]# ls
[root@hadoop1 test]# hdfs dfs -get /test1/test1.txt
[root@hadoop1 test]# ls
test1.txt
[root@hadoop1 test]# hdfs dfs -ls /test1
Found 2 items
-rw-r--r--   3 root supergroup         40 2021-11-12 10:55 /test1/test1.txt
-rw-r--r--   3 root supergroup         19 2021-11-12 10:40 /test1/test2.txt
  1. 将hdfs分布式系统下/test2目录下的文件,最后删除/test2目录。
[root@hadoop1 test]# hdfs dfs -rm /test2/*.*
Deleted /test2/testtmp.txt
[root@hadoop1 test]# hdfs dfs -rmdir /test2
[root@hadoop1 test]# hdfs dfs -ls /
Found 2 items
drwxr-xr-x   - root supergroup          0 2021-11-12 11:00 /test1
drwxr-xr-x   - root supergroup          0 2021-11-12 10:41 /test3
  1. 快照的基本操作
    具体要求为:HDFS文件系统的根目录下存在一个/test1的文件目录,要求开启该目录的可创建快照功能,并为该目录文件创建快照,快照名称为import-data,使用相关命令查看该快照文件的列表信息。同时删除/test1目录下的文件,并利用快照进行恢复。
#开启目录的可创建快照功能
[root@hadoop1 /]# hadoop dfsadmin -allowSnapshot /test1 
DEPRECATED: Use of this script to execute hdfs command is deprecated.
Instead use the hdfs command for it.
Allowing snaphot on /test1 succeeded
[root@hadoop1 /]# hdfs dfs -createSnapshot /test1 import-data  
//建立快照
Created snapshot /test1/.snapshot/import-data
[root@hadoop1 /]# hadoop fs -ls /test1/.snapshot    //查看快照文件
Found 1 items
drwxr-xr-x   - root supergroup          0 2021-11-12 08:31 /test1/.snapshot/import-data
[root@hadoop1 /]# hdfs dfs -ls /test1
Found 2 items
-rw-r--r--   3 root supergroup         40 2021-11-12 10:55 /test1/test1.txt
-rw-r--r--   3 root supergroup         19 2021-11-12 10:40 /test1/test2.txt
[root@hadoop1 /]# hdfs dfs -rm -r /test1/*.txt   //删除文件
Deleted /test1/test1.txt
Deleted /test1/test2.txt
[root@hadoop1 /]# hdfs dfs -ls /test1      // 可以看到文件已经删除
[root@hadoop1 /]# hdfs dfs -cp -ptopax /test1/.snapshot/import-data/*.txt /test1					//恢复快照到/test1目录
[root@hadoop1 /]# hdfs dfs -ls /test1
Found 2 items
-rw-r--r--   3 root supergroup         40 2021-11-12 10:55 /test1/test1.txt
-rw-r--r--   3 root supergroup         19 2021-11-12 10:40 /test1/test2.txt

标签:test1,hdfs,HDFS,dfs,hadoop1,基本操作,txt,root
来源: https://blog.csdn.net/weixin_46329906/article/details/121517395