hadoop hdfs常用shell命令

144 阅读2分钟

仅供自己学习和大家参考


查看帮助命令

[lqs@bdc112 hadoop-3.1.3]$ bin/hadoop fs
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] [-s <sleep interval>] <file>]
	[-test -[defsz] <path>]
	[-text [-ignoreCrc] <src> ...]
	[-touch [-a] [-m] [-t TIMESTAMP ] [-c] <path> ...]
	[-touchz <path> ...]
	[-truncate [-w] <length> <path> ...]
	[-usage [cmd ...]]

Generic options supported are:
-conf <configuration file>        specify an application configuration file
-D <property=value>               define a value for a given property
-fs <file:///|hdfs://namenode:port> specify default filesystem URL to use, overrides 'fs.defaultFS' property from configurations.
-jt <local|resourcemanager:port>  specify a ResourceManager
-files <file1,...>                specify a comma-separated list of files to be copied to the map reduce cluster
-libjars <jar1,...>               specify a comma-separated list of jar files to be included in the classpath
-archives <archive1,...>          specify a comma-separated list of archives to be unarchived on the compute machines

The general command line syntax is:
command [genericOptions] [commandOptions]

上传

1、-moveFromLocal:从本地剪切粘贴到 HDFS

[lqs@bdc112 hadoop-3.1.3]$ vim test.txt
输入:
test
test1
test2
[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -moveFromLocal ./test.txt /test

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

[lqs@bdc112 hadoop-3.1.3]$ vim test1.txt
输入:
test1.txt
[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -copyFromLocal test1.txt /test

3、-put:等同于 copyFromLocal,生产环境更习惯用 put

[lqs@bdc112 hadoop-3.1.3]$ vim test2.txt
输入:
test2.txt
[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -put ./test2.txt /test

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

[lqs@bdc112 hadoop-3.1.3]$ vim test3.txt
输入:
test3.txt
[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -appendToFile test3.txt /test/test2.txt

下载

1、-copyToLocal:从 HDFS 拷贝到本地

[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -moveFromLocal test.txt /test

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

[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -get /test/test2.txt ./test2-1.txt

#显示上面追加的内容
[lqs@bdc112 hadoop-3.1.3]$ cat test2-1.txt 
test2.txt
test3.txt

HDFS的 直接操作

1、-ls: 显示目录信息

[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -ls /test
Found 3 items
-rw-r--r--   3 lqs supergroup         17 2021-12-10 10:29 /test/test.txt
-rw-r--r--   3 lqs supergroup         10 2021-12-10 13:11 /test/test1.txt
-rw-r--r--   3 lqs supergroup         20 2021-12-10 13:14 /test/test2.txt

2、-cat:显示文件内容

[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -cat /test/test2.txt
2021-12-10 13:19:52,404 INFO sasl.SaslDataTransferClient: SASL encryption trust check: localHostTrusted = false, remoteHostTrusted = false
test2.txt
test3.txt

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

[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -chmod 777 /test/test.txt
[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -ls /test
-rwxrwxrwx   3 lqs supergroup         17 2021-12-10 10:29 /test/test.txt

[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -chown root:root /test/test1.txt
[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -ls /test
-rw-r--r--   3 root root               10 2021-12-10 13:11 /test/test1.txt

4、-mkdir:创建路径

[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -mkdir /test1

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

[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -cp /test/test.txt /test1
2021-12-10 13:23:34,988 INFO sasl.SaslDataTransferClient: SASL encryption trust check: localHostTrusted = false, remoteHostTrusted = false
2021-12-10 13:23:35,096 INFO sasl.SaslDataTransferClient: SASL encryption trust check: localHostTrusted = false, remoteHostTrusted = false

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

[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -mv /test/test1.txt /test1

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

[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -tail /test/test.txt
2021-12-10 13:25:34,761 INFO sasl.SaslDataTransferClient: SASL encryption trust check: localHostTrusted = false, remoteHostTrusted = false
test
test1
test2

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

[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -rm /test/test.txt
2021-12-10 13:26:08,316 INFO fs.TrashPolicyDefault: Moved: 'hdfs://bdc112:8020/test/test.txt' to trash at: hdfs://bdc112:8020/user/lqs/.Trash/Current/test/test.txt

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

[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -rm -r /test1
2021-12-10 13:26:54,488 INFO fs.TrashPolicyDefault: Moved: 'hdfs://bdc112:8020/test1' to trash at: hdfs://bdc112:8020/user/lqs/.Trash/Current/test1

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

[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -du -s -h /test
20  60  /test
[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -du -h /test
20  60  /test/test2.txt
#说明:20 表示文件大小;60 表示 20*3 个副本;/jinguo 表示查看的目录

11 -setrep:设置 HDFS 中文件的副本数量

[lqs@bdc112 hadoop-3.1.3]$ hadoop fs -setrep 10 /test/test2.txt
Replication 10 set: /test/test2.txt