基本语法
hadoop fs [选项] / hdfs dfs [选项]
命令
[xiaobai@hadoop102 ~]$ 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 [-immediate]]
[-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 [-f] [-p] [-l] [-d] <localsrc> ... <dst>]
[-moveToLocal <src> <localdst>]
[-mv <src> ... <dst>]
[-put [-f] [-p] [-l] [-d] [-t <thread count>] <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 -[defswrz] <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. 前提
1). 启动Hadoop集群:
hadoop102启动hdfs:
[xiaobai@hadoop102 hadoop-3.2.2]$ sbin/start-dfs.sh
[xiaobai@hadoop103 hadoop-3.2.2]$ sbin/start-yarn.sh
2). -help:输出命令参数:
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -help rm
3). 创建/weather文件夹:
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -mkdir /weather
2. 上传
1). -moveFromLocal:从本地剪贴
到HDFS(本地无,HDFS有);
[xiaobai@hadoop102 hadoop-3.2.2]$ vim monday.txt
输入:
monday is a rainy day
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -moveFromLocal ./monday.txt /weather
2). -copyFromLocal:从本地文件系统中拷贝
文件到HDFS路径(本地有,HDSF有);
[xiaobai@hadoop102 hadoop-3.2.2]$ vim Tuesday.txt
输入:
Tuesday is a sunny day
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -copyFromLocal Tuesday.txt /weather
3). -put:与copyFromLocal同,生产环境中用put(本地有,HDSF有);
[xiaobai@hadoop102 hadoop-3.2.2]$ vim Wednesday.txt
输入:
Wednesday balabala
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -put ./Wednesday.txt /weather
4). appendToFile:追加一个文件到另一个已经存在的文件末尾(HDFS 只能追加不能修改);
[xiaobai@hadoop102 hadoop-3.2.2]$ vim myfeeling
输入:
I was not in a good mood that day.
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -appendToFile ./myfeeling /weather/Wednesday.txt
3. 下载
1). -copyToLocal:从HDFS拷贝到本地(本地有,HDSF有);
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -copyToLocal /weather/monday.txt ./
2). -get:于copyToLocal同,生产环境中用get(本地有,HDSF有);
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -get /weather/monday.txt ./monday2.txt
4. HDFS的直接操作
1). -ls: 显示目录信息;
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -ls /weather
2). -cat: 显示文件内容;
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -cat /weather/Wednesday.txt
3). -chgrp / chmod / -chown: 与Linux文件系统中的用法相同,修改文件所属的权限;
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -chmod 666 /weather/Tuesday.txt
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -chown xiaobai:xiaobai /weather/monday.txt
4). -mkdir: 创建路径;
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -mkdir/sunny
5). -cp: 在HDFS中的路径拷贝;
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -cp /weather/Wednesday.txt /sunny
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -rm /sunny/monday.txt
6). -mv: 在HDFS目录中移动文件;
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -mv /weather/monday.txt /sunny
7). -tail: 显示一个文件的末尾1kb的数据(末尾文件为最新文件/重要文件);
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -tail /sunny/Wednesday.txt
8). -rm: 删除文件/文件夹;
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -rm /sunny/monday.txt
9). -rm -r: 递归删除目录及目录里的内容;
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -rm -r /sunny
10). -du: 统计文件夹的大小信息;
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -du -s -h /weather
99 297 /weather
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -du -h /weather
23 69 /weather/Tuesday.txt
54 162 /weather/Wednesday.txt
22 66 /weather/monday.txt
tips:99表示文件大小,297表示99*3个副本;/weather 表示查看的目录;
11). -setup: 设置HDFS中文件的副本数量;
[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop fs -setrep 6 /weather/monday.txt
Replication 6 set: /weather/monday.txt
tips:此处设置的副本数只是记录在NameNode的元数据中,不一定会有这么多副本,只有DataNode的数量可以得到保证时才会达到设置的副本数,否则,副本数只等到下次DataNode足够时才会继续拷贝副本。因为目前只有3ux设备,最多3x副本,只有节点数增加到6x时,副本数才能达到6.