HDFS的Shell操作

1、基本语法

hadoop fs命令的官方解释为“This command is documented in the File System Shell Guide. It is a synonym for hdfs dfs when HDFS is in use.”。即fs是一种通用的文件系统操作命令,当文件系统使用hdfs时,hadoop fshdfs dfs命令等效。

2、命令大全

[root@node01 file]# 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]

3、常用命令实操

[root@node01 file]# hadoop fs -mkdir /test

3.1、上传

1)-moveFromLocal:从本地剪切粘贴到 HDFS
[root@node01 file]# vim wordCount.txt
aa bb cc
aa bb
[root@node01 file]# hadoop fs -moveFromLocal wordCount.txt /test
2023-04-03 10:22:45,612 INFO sasl.SaslDataTransferClient: SASL encryption trust check: localHostTrusted = false, remoteHostTrusted = false
2)-copyFromLocal:从本地文件系统中拷贝文件到 HDFS 路径去
[root@node01 file]# vim wordCount1.txt
aa bb cc
aa bb
[root@node01 file]# hadoop fs -copyFromLocal wordCount1.txt /test
2023-04-03 10:26:01,696 INFO sasl.SaslDataTransferClient: SASL encryption trust check: localHostTrusted = false, remoteHostTrusted = false
3)-put:等同于 copyFromLocal,生产环境更习惯用 put
[root@node01 file]# hadoop fs -put wordCount1.txt /test
2023-04-03 10:26:01,696 INFO sasl.SaslDataTransferClient: SASL encryption trust check: localHostTrusted = false, remoteHostTrusted = false
4)-appendToFile:追加一个文件到已经存在的文件末尾
[root@node01 file]# hadoop fs -appendToFile wordCount1.txt /test/wordCount1.txt
2023-04-03 10:28:09,250 INFO sasl.SaslDataTransferClient: SASL encryption trust check: localHostTrusted = false, remoteHostTrusted = false

3.2、下载

1)-copyToLocal:从 HDFS 拷贝到本地
[root@node01 file]# hadoop fs -copyToLocal /test/wordCount.txt ./
2023-04-03 10:31:26,955 INFO sasl.SaslDataTransferClient: SASL encryption trust check: localHostTrusted = false, remoteHostTrusted = false
2)-get:等同于 copyToLocal,生产环境更习惯用 get
[root@node01 file]# hadoop fs -get /test/wordCount.txt ./
2023-04-03 10:32:09,688 INFO sasl.SaslDataTransferClient: SASL encryption trust check: localHostTrusted = false, remoteHostTrusted = false

3.3、HDFS 系统操作

1)-ls: 显示目录信息

[root@node01 file]# hadoop fs -ls /test
Found 2 items
-rw-r--r--   2 root supergroup         15 2023-04-03 10:22 /test/wordCount.txt
-rw-r--r--   2 root supergroup         30 2023-04-03 10:28 /test/wordCount1.txt

2)-cat:显示文件内容

[root@node01 file]# hadoop fs -cat /test/wordCount.txt
2023-04-03 10:44:54,811 INFO sasl.SaslDataTransferClient: SASL encryption trust check: localHostTrusted = false, remoteHostTrusted = false
aa bb cc
aa bb

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

[root@node01 file]# hadoop fs -chmod u+x /test/wordCount.txt 
[root@node01 file]# hadoop fs -ls /test
Found 2 items
-rwxr--r--   2 root supergroup         15 2023-04-03 10:22 /test/wordCount.txt
-rw-r--r--   2 root supergroup         30 2023-04-03 10:28 /test/wordCount1.txt
[root@node01 file]# 

4)-mkdir:创建路径

[root@node01 file]# hadoop fs -mkdir /test/t1

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

[root@node01 file]# hadoop fs -cp /test/wordCount.txt /test/t1
2023-04-03 10:47:46,391 INFO sasl.SaslDataTransferClient: SASL encryption trust check: localHostTrusted = false, remoteHostTrusted = false
2023-04-03 10:47:46,792 INFO sasl.SaslDataTransferClient: SASL encryption trust check: localHostTrusted = false, remoteHostTrusted = false

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

[root@node01 file]# hadoop fs -mv /test/wordCount1.txt /test/t1

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

[root@node01 file]# hadoop fs -tail /test/wordCount.txt
2023-04-03 10:48:56,402 INFO sasl.SaslDataTransferClient: SASL encryption trust check: localHostTrusted = false, remoteHostTrusted = false
aa bb cc
aa bb

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

[root@node01 file]# hadoop fs -rm /test/t1/wordCount.txt
Deleted /test/t1/wordCount.txt

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

[root@node01 file]# hadoop fs -rm -r /test/t1
Deleted /test/t1

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

[root@node01 file]# hadoop fs -du -s -h /test
15  30  /test
[root@node01 file]# hadoop fs -du -h /test
15  30  /test/wordCount.txt

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

[root@node01 file]# hadoop fs -setrep 1 /test/wordCount.txt
Replication 1 set: /test/wordCount.txt

这里设置的副本数只是记录在 NameNode 的元数据中,是否真的会有这么多副本,还得 看 DataNode 的数量。因为目前只有 3 台设备,最多也就 3 个副本,只有节点数的增加到 10 台时,副本数才能达到 10。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

精分小助手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值