HDFS 的 Shell 操作
1 基本语法
hadoop fs 具体命令 OR hdfs dfs 具体命令
1.1 命令大全
root@datanode:/usr/local/hadoop-2.8.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] <localsrc> ... <dst>]
[-copyToLocal [-f] [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
[-count [-q] [-h] [-v] [-t [<storage type>]] [-u] [-x] <path> ...]
[-cp [-f] [-p | -p[topax]] [-d] <src> ... <dst>]
[-createSnapshot <snapshotDir> [<snapshotName>]]
[-deleteSnapshot <snapshotDir> <snapshotName>]
[-df [-h] [<path> ...]]
[-du [-s] [-h] [-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>]
[-help [cmd ...]]
[-ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [<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> ...]
[-touchz <path> ...]
[-truncate [-w] <length> <path> ...]
[-usage [cmd ...]]
2 常用命令操作
- -help:输出这个命令参数
hadoop fs -help rm
- 创建/test文件夹
hadoop fs -mkdir /test
2.1 上传
- -moveFromLocal:从本地剪切粘贴到 HDFS
hadoop fs -moveFromLocal ./shuguo.txt /test
- -copyFromLocal:从本地文件系统中拷贝文件到 HDFS 路径去
hadoop fs -copyFromLocal ./shuguo.txt /test
- -put:等同于 copyFromLocal,生产环境更习惯用 put
hadoop fs -put ./shuguo.txt /test
- -appendToFile:追加一个文件到已经存在的文件末尾
hadoop fs -appendToFile liubei.txt /test/shuguo.txt
- 强制覆盖
# -f表示强制覆盖
hadoop fs -put -f ./shuguo.txt /test
2.2 下载
- -copyToLocal:从 HDFS 拷贝到本地
hadoop fs -copyToLocal /test/shuguo.txt ./
- -get:等同于 copyToLocal,生产环境更习惯用 get
hadoop fs -get /test/shuguo.txt ./
2.3 HDFS 直接操作
- ls: 显示目录信息
hadoop fs -ls /test
- -cat:显示文件内容
hadoop fs -cat /test/shuguo.txt
- -chgrp、-chmod、-chown:Linux 文件系统中的用法一样,修改文件所属权限
hadoop fs -chmod 666 /test/shuguo.txt
hadoop fs -chown atguigu:atguigu /sanguo/shuguo.txt
- -mkdir:创建路径
- -cp:从 HDFS 的一个路径拷贝到 HDFS 的另一个路径
- -mv:在 HDFS 目录中移动文件
- -tail:显示一个文件的末尾 1kb 的数据
- -rm:删除文件或文件夹
- -rm -r:递归删除目录及目录里面内容
- -du 统计文件夹的大小信息
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -du -s -h /jinguo
27 81 /jinguo
[atguigu@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
- -setrep:设置 HDFS 中文件的副本数量
hadoop fs -setrep 10 /jinguo/shuguo.txt