【Hadoop】HDFS的常见shell操作

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

Hadoop Distributed File System简称 HDFS是一个分布式文件系统。我们可以在命令行中对hdfs进行操作形式上类似于linux文件系统的操作但本质上相去甚远。其命令格式为
bin/hdfs dfs -<命令参数> hdfs://authority/path

文章目录

1. 帮助文档

查看总的帮助文档的命令为hdfs dfs。在帮助文档中可以看到很多熟悉的命令如lscatmkdir等。

[root@bigData01 hadoop-3.2.0]# hdfs dfs
Usage: hadoop fs [generic options]
	[-appendToFile <localsrc> ... <dst>]
	[-cat [-ignoreCrc] <src> ...]
	[-checksum <src> ...]
	[-chgrp [-R] GROUP PATH...]
	[-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...]
	[-chown [-R] [OWNER][:[GROUP]] PATH...]
	[-copyFromLocal [-f] [-p] [-l] [-d] [-t <thread count>] <localsrc> ... <dst>]
	[-copyToLocal [-f] [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
	[-count [-q] [-h] [-v] [-t [<storage type>]] [-u] [-x] [-e] <path> ...]
	[-cp [-f] [-p | -p[topax]] [-d] <src> ... <dst>]
	[-createSnapshot <snapshotDir> [<snapshotName>]]
	[-deleteSnapshot <snapshotDir> <snapshotName>]
	[-df [-h] [<path> ...]]
	[-du [-s] [-h] [-v] [-x] <path> ...]
	[-expunge]
	[-find <path> ... <expression> ...]
	[-get [-f] [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
	[-getfacl [-R] <path>]
	[-getfattr [-R] {-n name | -d} [-e en] <path>]
	[-getmerge [-nl] [-skip-empty-file] <src> <localdst>]
	[-head <file>]
	[-help [cmd ...]]
	[-ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [-e] [<path> ...]]
	[-mkdir [-p] <path> ...]
	[-moveFromLocal <localsrc> ... <dst>]
	[-moveToLocal <src> <localdst>]
	[-mv <src> ... <dst>]
	[-put [-f] [-p] [-l] [-d] <localsrc> ... <dst>]
	[-renameSnapshot <snapshotDir> <oldName> <newName>]
	[-rm [-f] [-r|-R] [-skipTrash] [-safely] <src> ...]
	[-rmdir [--ignore-fail-on-non-empty] <dir> ...]
	[-setfacl [-R] [{-b|-k} {-m|-x <acl_spec>} <path>]|[--set <acl_spec> <path>]]
	[-setfattr {-n name [-v value] | -x name} <path>]
	[-setrep [-R] [-w] <rep> <path> ...]
	[-stat [format] <path> ...]
	[-tail [-f] <file>]
	[-test -[defsz] <path>]
	[-text [-ignoreCrc] <src> ...]
	[-touch [-a] [-m] [-t TIMESTAMP ] [-c] <path> ...]
	[-touchz <path> ...]
	[-truncate [-w] <length> <path> ...]
	[-usage [cmd ...]]
······

2. 查询指定路径信息

查询指定路径信息的参数格式为
hdfs dfs -ls hdfs://authority/path/也可以写为hdfs dfs -ls /
这一串url之所以可以省略是因为hdfs在执行的时候会根据HDOOP_HOME自动识别配置文件中的fs.defaultFS属性补全。

[root@bigData01 hadoop-3.2.0]#  hdfs dfs -ls hdfs://bigData01:9000/
Found 1 items
-rw-r--r--   1 root supergroup       1361 2023-01-17 18:34 /README.txt
[root@bigData01 hadoop-3.2.0]#  hdfs dfs -ls /
Found 1 items
-rw-r--r--   1 root supergroup       1361 2023-01-17 18:34 /README.txt

递归显示所有目录信息使用参数-R

[root@bigData01 hadoop-3.2.0]# hdfs dfs -ls /
Found 4 items
-rw-r--r--   1 root supergroup     150569 2023-01-17 18:48 /LICENSE.txt
-rw-r--r--   1 root supergroup       1361 2023-01-17 18:34 /README.txt
drwxr-xr-x   - root supergroup          0 2023-01-18 10:54 /a
drwxr-xr-x   - root supergroup          0 2023-01-18 10:52 /test
[root@bigData01 hadoop-3.2.0]# hdfs dfs -ls -R /
-rw-r--r--   1 root supergroup     150569 2023-01-17 18:48 /LICENSE.txt
-rw-r--r--   1 root supergroup       1361 2023-01-17 18:34 /README.txt
drwxr-xr-x   - root supergroup          0 2023-01-18 10:54 /a
drwxr-xr-x   - root supergroup          0 2023-01-18 10:54 /a/c
drwxr-xr-x   - root supergroup          0 2023-01-18 10:52 /test

3. 从本地上传文件

从本地上传文件到hdfs的参数格式为
hdfs dfs -put hdfs://authority/path/也可以写为hdfs dfs -put /

[root@bigData01 hadoop-3.2.0]#  hdfs dfs -put LICENSE.txt /
[root@bigData01 hadoop-3.2.0]#  hdfs dfs -ls /
Found 2 items
-rw-r--r--   1 root supergroup     150569 2023-01-17 18:48 /LICENSE.txt
-rw-r--r--   1 root supergroup       1361 2023-01-17 18:34 /README.txt

4. 浏览本地文件

浏览本地文件的参数格式为
hdfs dfs -cat <文件地址>

[root@bigData01 hadoop-3.2.0]#  hdfs dfs -cat /README.txt
For the latest information about Hadoop, please visit our website at:

   http://hadoop.apache.org/

and our wiki, at:

   http://wiki.apache.org/hadoop/

This distribution includes cryptographic software.  The country in 
which you currently reside may have restrictions on the import,
······

5. 下载文件到本地

下载文件到本地的参数格式为
hdfs dfs -get <文件名> <地址>
其中参数中的地址项若给定的是一个文件夹地址那么就会将文件下载到该文件夹内并保持原名若给定的是一个具体的文件地址那么就会将该文件下载到该地址并改名。

[root@bigData01 hadoop-3.2.0]# hdfs dfs -get /README.txt ~
[root@bigData01 hadoop-3.2.0]# ll ~
total 28
-rw-------. 1 root root  2606 Mar 26  2021 anaconda-ks.cfg
-rw-r--r--. 1 root root     0 Jan 17 10:40 a.txt
-rw-r--r--. 1 root root    90 Jan 15 14:59 hello.txt
-rwxr-xr-x. 1 root root 13846 Apr 20  2021 pd
-rw-r--r--. 1 root root  1361 Jan 18 10:20 README.txt
[root@bigData01 hadoop-3.2.0]# hdfs dfs -get /README.txt ~/readme.txt
[root@bigData01 hadoop-3.2.0]# ll ~
total 32
-rw-------. 1 root root  2606 Mar 26  2021 anaconda-ks.cfg
-rw-r--r--. 1 root root     0 Jan 17 10:40 a.txt
-rw-r--r--. 1 root root    90 Jan 15 14:59 hello.txt
-rwxr-xr-x. 1 root root 13846 Apr 20  2021 pd
-rw-r--r--. 1 root root  1361 Jan 18 10:26 readme.txt
-rw-r--r--. 1 root root  1361 Jan 18 10:20 README.txt

6. 创建文件夹

创建文件夹使用mkdir命令参数格式为
hdfs dfs -mkdir <地址>

[root@bigData01 hadoop-3.2.0]# hdfs dfs -mkdir /test
[root@bigData01 hadoop-3.2.0]# hdfs dfs -ls /
Found 3 items
-rw-r--r--   1 root supergroup     150569 2023-01-17 18:48 /LICENSE.txt
-rw-r--r--   1 root supergroup       1361 2023-01-17 18:34 /README.txt
drwxr-xr-x   - root supergroup          0 2023-01-18 10:52 /test

递归创建多级目录使用参数-p

[root@bigData01 hadoop-3.2.0]# hdfs dfs -mkdir /a/c
mkdir: `hdfs://bigData01:9000/a': No such file or directory
[root@bigData01 hadoop-3.2.0]# hdfs dfs -mkdir -p /a/c
[root@bigData01 hadoop-3.2.0]# hdfs dfs -ls /
Found 4 items
-rw-r--r--   1 root supergroup     150569 2023-01-17 18:48 /LICENSE.txt
-rw-r--r--   1 root supergroup       1361 2023-01-17 18:34 /README.txt
drwxr-xr-x   - root supergroup          0 2023-01-18 10:54 /a
drwxr-xr-x   - root supergroup          0 2023-01-18 10:52 /test

7. 删除文件/文件夹

删除操作使用rm参数参数格式为
hdfs dfs -rm <文件名>
若要删除文件夹需要使用-r参数会将目录进行递归删除。

[root@bigData01 hadoop-3.2.0]# hdfs dfs -rm /README.txt
Deleted /README.txt
[root@bigData01 hadoop-3.2.0]# hdfs dfs -rm /a
rm: `/a': Is a directory
[root@bigData01 hadoop-3.2.0]# hdfs dfs -rm -r /a
Deleted /a

8. 综合应用

统计根目录下文件数量并显示文件大小。

8.1 统计文件数量

使用管道命令将hdfs dfs -ls /命令的执行结果发给grep命令筛选出文件实际上就是减去第一行的Found 2 items再将过滤结果使用wc -l命令统计行数。

[root@bigData01 hadoop-3.2.0]# hdfs dfs -ls /
Found 2 items
-rw-r--r--   1 root supergroup     150569 2023-01-17 18:48 /LICENSE.txt
drwxr-xr-x   - root supergroup          0 2023-01-18 10:52 /test
[root@bigData01 hadoop-3.2.0]# hdfs dfs -ls /|grep /|wc -l
2

8.2 显示各文件大小

hdfs dfs -ls /|grep /输出的数据第5列为文件大小第8列为文件名因此使用awk命令将这两列输出即可。

[root@bigData01 hadoop-3.2.0]# hdfs dfs -ls /|grep /
-rw-r--r--   1 root supergroup     150569 2023-01-17 18:48 /LICENSE.txt
drwxr-xr-x   - root supergroup          0 2023-01-18 10:52 /test
[root@bigData01 hadoop-3.2.0]# hdfs dfs -ls /|grep /|awk '{print $8,$5}'
/LICENSE.txt 150569
/test 0
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: shellHadoop

“【Hadoop】HDFS的常见shell操作” 的相关文章