大数据技术之Hadoop(HDFS)第2章 HFDS的Shell操作

1)基本语法

bin/hadoop fs 具体命令

2命令大全

[atguigu@hadoop102 hadoop-2.7.2]$ bin/hadoop fs

[-appendToFile ... ]

        [-cat [-ignoreCrc] ...]

        [-checksum ...]

        [-chgrp [-R] GROUP PATH...]

        [-chmod [-R] PATH...]

        [-chown [-R] [OWNER][:[GROUP]] PATH...]

        [-copyFromLocal [-f] [-p] ... ]

        [-copyToLocal [-p] [-ignoreCrc] [-crc] ... ]

        [-count [-q] ...]

        [-cp [-f] [-p] ... ]

        [-createSnapshot []]

        [-deleteSnapshot ]

        [-df [-h] [ ...]]

        [-du [-s] [-h] ...]

        [-expunge]

        [-get [-p] [-ignoreCrc] [-crc] ... ]

        [-getfacl [-R] ]

        [-getmerge [-nl] ]

        [-help [cmd ...]]

        [-ls [-d] [-h] [-R] [ ...]]

        [-mkdir [-p] ...]

        [-moveFromLocal ... ]

        [-moveToLocal ]

        [-mv ... ]

        [-put [-f] [-p] ... ]

        [-renameSnapshot ]

        [-rm [-f] [-r|-R] [-skipTrash] ...]

        [-rmdir [--ignore-fail-on-non-empty]

...]

        [-setfacl [-R] [{-b|-k} {-m|-x } ]|[--set ]]

        [-setrep [-R] [-w] ...]

        [-stat [format] ...]

        [-tail [-f] ]

        [-test -[defsz] ]

        [-text [-ignoreCrc] ...]

        [-touchz ...]

        [-usage [cmd ...]]

3)常用命令实操

(0)启动Hadoop集群(方便后续的测试

[atguigu@hadoop102 hadoop-2.7.2]$ sbin/start-dfs.sh

[atguigu@hadoop103 hadoop-2.7.2]$ sbin/start-yarn.sh

(1)-help输出这个命令参数

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -help rm

(2-ls: 显示目录信息

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -ls /

(3-mkdir:在hdfs上创建目录

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -mkdir -p /sanguo/shuguo

4)-moveFromLocal从本地剪切粘贴到hdfs

[atguigu@hadoop102 hadoop-2.7.2]$ touch kongming.txt

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs  -moveFromLocal  ./kongming.txt  /sanguo/shuguo

5)--appendToFile  :追加一个文件到已经存在的文件末尾

[atguigu@hadoop102 hadoop-2.7.2]$ touch liubei.txt

[atguigu@hadoop102 hadoop-2.7.2]$ vi liubei.txt

输入

san gu mao lu

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -appendToFile liubei.txt /sanguo/shuguo/kongming.txt

  1. -cat :显示文件内容

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -cat /sanguo/shuguo/kongming.txt

7)-tail:显示一个文件的末尾

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -tail /sanguo/shuguo/kongming.txt

8)-chgrp 、-chmod、-chown:linux文件系统中的用法一样,修改文件所属权限

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs  -chmod  666  /sanguo/shuguo/kongming.txt

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs  -chown  atguigu:atguigu   /sanguo/shuguo/kongming.txt

9)-copyFromLocal:从本地文件系统中拷贝文件到hdfs路径去

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -copyFromLocal README.txt /

(10)-copyToLocal:从hdfs拷贝到本地

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -copyToLocal /sanguo/shuguo/kongming.txt ./

(11)-cp :从hdfs的一个路径拷贝到hdfs的另一个路径

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -cp /sanguo/shuguo/kongming.txt /zhuge.txt

(12)-mv:在hdfs目录中移动文件

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -mv /zhuge.txt /sanguo/shuguo/

(13)-get:等同于copyToLocal,就是从hdfs下载文件到本地

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -get /sanguo/shuguo/kongming.txt ./

(14)-getmerge  :合并下载多个文件,比如hdfs的目录 /aaa/下有多个文件:log.1, log.2,log.3,...

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -getmerge /user/atguigu/test/* ./zaiyiqi.txt

(15)-put:等同于copyFromLocal

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -put ./zaiyiqi.txt /user/atguigu/test/

(16)-rm:删除文件或文件夹

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -rm /user/atguigu/test/jinlian2.txt

(17)-rmdir:删除空目录

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -mkdir /test

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -rmdir /test

18-du统计文件夹的大小信息

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -du -s -h /user/atguigu/test

2.7 K  /user/atguigu/test

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -du  -h /user/atguigu/test

1.3 K  /user/atguigu/test/README.txt

15     /user/atguigu/test/jinlian.txt

1.4 K  /user/atguigu/test/zaiyiqi.txt

19-setrep:设置hdfs中文件的副本数量

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -setrep 10 /sanguo/shuguo/kongming.txt

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

 

本教程由尚硅谷教育大数据研究院出品,如需转载请注明来源。
 

 

 

你可能感兴趣的:(Java)