[toc]

一、命令汇总

1. 基本语法

hadoop fs 具体命令 OR hdfs dfs 具体命令 两个是完全相同的。

2. 命令大全

[liudd@hadoop102 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 ...]]

二、常用命令实操

1. 准备工作

  1. 启动 hdfs
    [liudd@hadoop102 hadoop-3.1.3]$ sbin/start-dfs.sh
  2. -help:输出这个命令参数
    [liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -help rm
  3. 创建/sanguo文件夹
    [liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -mkdir /sanguo
    image.png

2. 上传

  1. -moveFromLocal:从本地剪切粘贴到 HDFS
[liudd@hadoop102 hadoop-3.1.3]$ vim shuguo.txt
# 输入:
shuguo

[liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -moveFromLocal shuguo.txt /sanguo

image.png

image.png

  1. -copyFromLocal:从本地文件系统中拷贝文件到 HDFS 路径去
[liudd@hadoop102 hadoop-3.1.3]$ vim weiguo.txt
# 输入:
weiguo

[liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -copyFromLocal weiguo.txt /sanguo

image.png

image.png

  1. -put:等同于 copyFromLocal,生产环境更习惯用 put
[liudd@hadoop102 hadoop-3.1.3]$ vim wuguo.txt
# 输入:
wuguo
[liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -put ./wuguo.txt /sanguo

image.png

image.png

  1. -appendToFile:追加一个文件到已经存在的文件末尾
[liudd@hadoop102 hadoop-3.1.3]$ vim liubei.txt
# 输入:
liubei

[liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -appendToFile liubei.txt /sanguo/shuguo.txt

image.png

image.png

3. 下载

  1. -copyToLocal:从 HDFS 拷贝到本地
[liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -copyToLocal /sanguo/shuguo.txt .

image.png

  1. -get:等同于 copyToLocal,生产环境更习惯用 get
[liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -get /sanguo/shuguo.txt ./shuguo2.txt

image.png

4. HDFS 直接操作

  1. -ls: 显示目录信息
[liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -ls /sanguo

image.png

  1. -cat:显示文件内容
[liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -cat /sanguo/shuguo.txt

image.png

  1. -chgrp、-chmod、-chown:Linux 文件系统中的用法一样,修改文件所属权限
[liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -chmod 666 /sanguo/shuguo.txt
[liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -chown liudd:liudd 
/sanguo/shuguo.txt

image.png

image.png

  1. -mkdir:创建路径
[liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -mkdir /jinguo
  1. -cp:从 HDFS 的一个路径拷贝到 HDFS 的另一个路径
[liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -cp /sanguo/shuguo.txt /jinguo

image.png

image.png

  1. -mv:在 HDFS 目录中移动文件
[liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -mv /sanguo/wuguo.txt /jinguo
[liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -mv /sanguo/weiguo.txt /jinguo

image.png

image.png

  1. -tail:显示一个文件的末尾 1kb 的数据
[liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -tail /jinguo/shuguo.txt

image.png

  1. -rm:删除文件或文件夹
[liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -rm /sanguo/shuguo.txt
  1. -rm -r:递归删除目录及目录里面内容
[liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -rm -r /sanguo

image.png

  1. -du 统计文件夹的大小信息
[liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -du -s -h /jinguo
27 81 /jinguo
[liudd@hadoop102 hadoop-3.1.3]$ hadoop fs -du -h /jinguo
14 42 /jinguo/shuguo.txt
7 21 /jinguo/weiguo.txt
6 18 /jinguo/wuguo.tx

image.png

说明:27 表示文件大小;81 表示 27*3 个副本;/jinguo 表示查看的目录
11. -setrep:设置 HDFS 中文件的副本数量

[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -setrep 10 /jinguo/shuguo.txt

image.png

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

Q.E.D.


只有创造,才是真正的享受,只有拚搏,才是充实的生活。