@[toc]
6.HDFS的Shell操作(开发重点)
6.1基本语法
hadoop fs 具体命令 OR hdfs dfs 具体命令
两个是完全相同的
6.2命令大全
[atguigu@hadoop102 hadoop-3.1.3]$ bin/hadoop fs
[-appendToFile <localsrc> ... <dst>]
[-cat [-ignoreCrc] <src> ...]
[-chgrp [-R] GROUP PATH...]
[-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...]
[-chown [-R] [OWNER][:[GROUP]] PATH...]
[-copyFromLocal [-f] [-p] <localsrc> ... <dst>]
[-copyToLocal [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
[-count [-q] <path> ...]
[-cp [-f] [-p] <src> ... <dst>]
[-df [-h] [<path> ...]]
[-du [-s] [-h] <path> ...]
[-get [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
[-getmerge [-nl] <src> <localdst>]
[-help [cmd ...]]
[-ls [-d] [-h] [-R] [<path> ...]]
[-mkdir [-p] <path> ...]
[-moveFromLocal <localsrc> ... <dst>]
[-moveToLocal <src> <localdst>]
[-mv <src> ... <dst>]
[-put [-f] [-p] <localsrc> ... <dst>]
[-rm [-f] [-r|-R] [-skipTrash] <src> ...]
[-rmdir [--ignore-fail-on-non-empty] <dir> ...]
<acl_spec> <path>]]
[-setrep [-R] [-w] <rep> <path> ...]
[-stat [format] <path> ...]
[-tail [-f] <file>]
[-test -[defsz] <path>]
[-text [-ignoreCrc] <src> ...]
6.3常用命令实操
6.3.1准备工作
6.3.1.1启动Hadoop集群(方便后续的测试)
- [summer@hadoop102 hadoop-3.1.3]$ sbin/start-dfs.sh
- [summer@hadoop103 hadoop-3.1.3]$ sbin/start-yarn.sh
6.3.1.2-help:输出这个命令参数
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -help rm
6.3.1.3创建/sanguo文件夹
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -mkdir /sanguo
6.3.2上传
6.3.2.1-moveFromLocal:从本地剪切粘贴到HDFS
- [summer@hadoop102 hadoop-3.1.3]$ vim shuguo.txt
输入的内容为shuguo
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -moveFromLocal ./shuguo.txt /sanguo
因为这个功能为剪切功能,所以创建的shuguo.txt文件就没有了而到了HDFS上
6.3.2.2-copyFromLocal:从本地文件系统中拷贝文件到HDFS路径去
- [summer@hadoop102 hadoop-3.1.3]$ vim weiguo.txt
内容为weiguo
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -copyFromLocal ./weiguo.txt /sanguo
这个操作是复制,所以本地创建的weiguo.txt文件不会没有
6.3.2.3-put:等同于copyFromLocal,生产环境更习惯用put
- [summer@hadoop102 hadoop-3.1.3]$ vim wuguo.txt
内容是wuguo
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -put wuguo.txt /sanguo
这个操作是复制,所以本地创建的wuguo.txt文件不会没有
6.3.2.4-appendToFile:追加一个文件到已经存在的文件末尾
- [summer@hadoop102 hadoop-3.1.3]$ vim liubei.txt
内容为liubei
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -appendToFile liubei.txt /sanguo/shuguo.txt
内容追加到了shuguo.txt文档里面了
6.3.3下载
6.3.3.1-copyToLocal:从HDFS拷贝到本地
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -copyToLocal /sanguo/shuguo.txt ./
6.3.3.2-get:等同于copyToLocal,生产环境更习惯用get
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -get /sanguo/shuguo.txt ./shuguo2.txt
6.3.4HDFS直接操作
6.3.4.1-ls: 显示目录信息
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -ls /
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -ls /sanguo
6.3.4.2-cat:显示文件内容
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -cat /sanguo/shuguo.txt
6.3.4.3-chgrp、-chmod、-chown:Linux文件系统中的用法一样,修改文件所属权限
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -chmod 666 /sanguo/shuguo.txt
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -chown summer:summer /sanguo/shuguo.txt
6.3.4.4-mkdir:创建路径
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -mkdir /jinguo
6.3.4.5-cp:从HDFS的一个路径拷贝到HDFS的另一个路径
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -cp /sanguo/shuguo.txt /jinguo
6.3.4.6-mv:在HDFS目录中移动文件
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -mv /sanguo/wuguo.txt /jinguo
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -mv /sanguo/weiguo.txt /jinguo
6.3.4.7-tail:显示一个文件的末尾1kb的数据
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -tail /jinguo/shuguo.txt
6.3.4.8-rm:删除文件或文件夹
[summer@hadoop102 hadoop-3.1.3]$ hadoop fs -rm /sanguo/shuguo.txt
6.3.4.9-rm -r:递归删除目录及目录里面内容
[summer@hadoop102 hadoop-3.1.3]$ hadoop fs -rm -r /sanguo
6.3.4.10-du统计文件夹的大小信息
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -du -s -h /jinguo
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -du -h /jinguo
6.3.4.11-setrep:设置HDFS中文件的副本数量
- [summer@hadoop102 hadoop-3.1.3]$ hadoop fs -setrep 10 /jinguo/shuguo.txt
这里设置的副本数只是记录在NameNode的元数据中,是否真的会有这么多副本,还得看DataNode的数量。因为目前只有3台设备,最多也就3个副本,只有节点数的增加到10台时,副本数才能达到10。