Hadoop之HDFS的Shell操作
1、基本語法
bin/hadoop fs 具體命令 或者 bin/hdfs dfs 具體命令
dfs 是 fs 的實現類。
2、命令大全
[[email protected] 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:輸出這個命令引數
[[email protected] hadoop-2.7.2]$ bin/hadoop fs -help rm
2)-ls:顯示目錄資訊
[[email protected] hadoop-2.7.2]$ hadoop fs -ls /
3)-mkdir:在HDFS上建立目錄
[[email protected] hadoop-2.7.2]$ hadoop fs -mkdir -p /sanguo/shuguo
4)-moveFromLocal:從本地剪下貼上到HDFS
[[email protected] hadoop-2.7.2]$ touch test.txt [[email protected] hadoop-2.7.2]$ hadoop fs -moveFromLocal ./test.txt /hadoop/test
5)-appendToFile:追加一個檔案到已經存在到檔案末尾
[[email protected] hadoop-2.7.2]$ touch liubei.txt [[email protected] hadoop-2.7.2]$ vi liubei.txt 輸入 san gu mao lu [[email protected] hadoop-2.7.2]$ hadoop fs -appendToFile liubei.txt /sanguo/shuguo/kongming.txt
6)-cat:顯示檔案內容
[[email protected] hadoop-2.7.2]$ hadoop fs -cat /sanguo/shuguo/kongming.txt
7)-chgrp、-chmod、-chown:同Linux檔案系統中的用法一樣,修改檔案所屬許可權
[[email protected] hadoop-2.7.2]$ hadoop fs -chmod 666 /sanguo/shuguo/kongming.txt [[email protected] hadoop-2.7.2]$ hadoop fs -chown atguigu:atguigu /sanguo/shuguo/kongming.txt
注:hadoop檔案系統中的檔案並不能執行,因此沒有執行許可權。
8)-copyFromLocal:從本地檔案系統中拷貝檔案到HDFS路徑中去
[[email protected] hadoop-2.7.2]$ hadoop fs -copyFromLocal README.txt /
9)-copyToLocal:從HDFS拷貝到本地
[[email protected] hadoop-2.7.2]$ hadoop fs -copyToLocal /sanguo/shuguo/kongming.txt ./
10)-cp:從HDFS的一個路徑拷貝到HDFS的另一個路徑
[[email protected] hadoop-2.7.2]$ hadoop fs -cp /sanguo/shuguo/kongming.txt /zhuge.txt
11)-mv:在HDFS目錄中移動檔案
[[email protected] hadoop-2.7.2]$ hadoop fs -mv /zhuge.txt /sanguo/shuguo/
12)-get:等同於copyToLocal,就是從HDFS下載檔案到本地
[[email protected] hadoop-2.7.2]$ hadoop fs -get /sanguo/shuguo/kongming.txt ./
13)-getmerge:合併下載多個檔案,比如HDFS的目錄 /user/hadoop/test 下有多個檔案:log1,log2,log3...
[[email protected] hadoop-2.7.2]$ hadoop fs -getmerge /user/atguigu/test/* ./zaiyiqi.txt
14)-put:等同於 copyFromLocal
[[email protected] hadoop-2.7.2]$ hadoop fs -put ./zhangsan.txt /user/hadoop/test/
15)-tail:顯示一個檔案的末尾
[[email protected] hadoop-2.7.2]$ hadoop fs -tail /sanguo/shuguo/kongming.txt
16)-rm:刪除檔案或者資料夾
[[email protected] hadoop-2.7.2]$ hadoop fs -rm /user/hadoop/test/jinlian2.txt
[[email protected] hadoop-2.7.2]$ hadoop fs -rm -R /user/hadoop/test #遞迴刪除資料夾(無論資料夾下是否有檔案都會刪除)
17)-rmdir:刪除空目錄
[[email protected] hadoop-2.7.2]$ hadoop fs -mkdir /test
18)-du:統計資料夾的大小資訊
[[email protected] hadoop-2.7.2]$ hadoop fs -du -s -h /user/hadoop/test 2.7 K /user/hadoop/test [[email protected] 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中檔案的副本數量
[[email protected] hadoop-2.7.2]$ hadoop fs -setrep 10 /sanguo/shuguo/kongming.txt
注意:這裡設定的副本數只是記錄在NameNode 的元資料中,是否真的會有這麼多副本,還得看 DataNode 的數量。因為目前只有3臺裝置,最多也就3個副本,只有節點數增加到10臺時,副本數才能達到10.