Hadoop之HDFS的Shell操作
1、基本语法
bin/hadoop fs 具体命令 或者 bin/hdfs dfs 具体命令
dfs 是 fs 的实现类。
2、命令大全
[hadoop@hadoop102 hadoop-2.7.2]$ 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] <localsrc> ... <dst>] [-copyToLocal [-p] [-ignoreCrc] [-crc] <src> ... <localdst>] [-count [-q] [-h] <path> ...] [-cp [-f] [-p | -p[topax]] <src> ... <dst>] [-createSnapshot <snapshotDir> [<snapshotName>]] [-deleteSnapshot <snapshotDir> <snapshotName>] [-df [-h] [<path> ...]] [-du [-s] [-h] <path> ...] [-expunge] [-find <path> ... <expression> ...] [-get [-p] [-ignoreCrc] [-crc] <src> ... <localdst>] [-getfacl [-R] <path>] [-getfattr [-R] {-n name | -d} [-e en] <path>] [-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] [-l] <localsrc> ... <dst>] [-renameSnapshot <snapshotDir> <oldName> <newName>] [-rm [-f] [-r|-R] [-skipTrash] <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 ...]] Generic options supported are -conf <configuration file> specify an application configuration file -D <property=value> use value for given property -fs <local|namenode:port> specify a namenode -jt <local|resourcemanager:port> specify a ResourceManager -files <comma separated list of files> specify comma separated files to be copied to the map reduce cluster -libjars <comma separated list of jars> specify comma separated jar files to include in the classpath. -archives <comma separated list of archives> specify comma separated archives to be unarchived on the compute machines. The general command line syntax is bin/hadoop command [genericOptions] [commandOptions]
3、常用命令操作
1)-help:输出这个命令参数
[hadoop@hadoop102 hadoop-2.7.2]$ bin/hadoop fs -help rm
2)-ls:显示目录信息
[hadoop@hadoop102 hadoop-2.7.2]$ hadoop fs -ls /
3)-mkdir:在HDFS上创建目录
[hadoop@hadoop102 hadoop-2.7.2]$ hadoop fs -mkdir -p /sanguo/shuguo
4)-moveFromLocal:从本地剪切粘贴到HDFS
[hadoop@hadoop102 hadoop-2.7.2]$ touch test.txt [hadoop@hadoop102 hadoop-2.7.2]$ hadoop fs -moveFromLocal ./test.txt /hadoop/test
5)-appendToFile:追加一个文件到已经存在到文件末尾
[hadoop@hadoop102 hadoop-2.7.2]$ touch liubei.txt [hadoop@hadoop102 hadoop-2.7.2]$ vi liubei.txt 输入 san gu mao lu [hadoop@hadoop102 hadoop-2.7.2]$ hadoop fs -appendToFile liubei.txt /sanguo/shuguo/kongming.txt
6)-cat:显示文件内容
[hadoop@hadoop102 hadoop-2.7.2]$ hadoop fs -cat /sanguo/shuguo/kongming.txt
7)-chgrp、-chmod、-chown:同Linux文件系统中的用法一样,修改文件所属权限
[hadoop@hadoop102 hadoop-2.7.2]$ hadoop fs -chmod 666 /sanguo/shuguo/kongming.txt [hadoop@hadoop102 hadoop-2.7.2]$ hadoop fs -chown atguigu:atguigu /sanguo/shuguo/kongming.txt
注:hadoop文件系统中的文件并不能执行,因此没有执行权限。
8)-copyFromLocal:从本地文件系统中拷贝文件到HDFS路径中去
[hadoop@hadoop102 hadoop-2.7.2]$ hadoop fs -copyFromLocal README.txt /
9)-copyToLocal:从HDFS拷贝到本地
[hadoop@hadoop102 hadoop-2.7.2]$ hadoop fs -copyToLocal /sanguo/shuguo/kongming.txt ./
10)-cp:从HDFS的一个路径拷贝到HDFS的另一个路径
[hadoop@hadoop102 hadoop-2.7.2]$ hadoop fs -cp /sanguo/shuguo/kongming.txt /zhuge.txt
11)-mv:在HDFS目录中移动文件
[hadoop@hadoop102 hadoop-2.7.2]$ hadoop fs -mv /zhuge.txt /sanguo/shuguo/
12)-get:等同于copyToLocal,就是从HDFS下载文件到本地
[hadoop@hadoop102 hadoop-2.7.2]$ hadoop fs -get /sanguo/shuguo/kongming.txt ./
13)-getmerge:合并下载多个文件,比如HDFS的目录 /user/hadoop/test 下有多个文件:log1,log2,log3...
[hadoop@hadoop102 hadoop-2.7.2]$ hadoop fs -getmerge /user/atguigu/test/* ./zaiyiqi.txt
14)-put:等同于 copyFromLocal
[hadoop@hadoop102 hadoop-2.7.2]$ hadoop fs -put ./zhangsan.txt /user/hadoop/test/
15)-tail:显示一个文件的末尾
[hadoop@hadoop102 hadoop-2.7.2]$ hadoop fs -tail /sanguo/shuguo/kongming.txt
16)-rm:删除文件或者文件夹
[hadoop@hadoop102 hadoop-2.7.2]$ hadoop fs -rm /user/hadoop/test/jinlian2.txt
[hadoop@hadoop102 hadoop-2.7.2]$ hadoop fs -rm -R /user/hadoop/test #递归删除文件夹(无论文件夹下是否有文件都会删除)
17)-rmdir:删除空目录
[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -mkdir /test
18)-du:统计文件夹的大小信息
[hadoop@hadoop102 hadoop-2.7.2]$ hadoop fs -du -s -h /user/hadoop/test 2.7 K /user/hadoop/test [atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -du -h /user/hadoop/test 1.3 K /user/hadoop/test/README.txt 15 /user/hadoop/test/jinlian.txt 1.4 K /user/hadoop/test/zaiyiqi.txt
19)-setrep:设置HDFS中文件的副本数量
[hadoop@hadoop102 hadoop-2.7.2]$ hadoop fs -setrep 10 /sanguo/shuguo/kongming.txt
注意:这里设置的副本数只是记录在NameNode 的元数据中,是否真的会有这么多副本,还得看 DataNode 的数量。因为目前只有3台设备,最多也就3个副本,只有节点数增加到10台时,副本数才能达到10.