天天看點

第2章 HFDS的Shell操作

2.1 基本文法

hadoop fs 具體指令
           

hdfs dfs 具體指令
           

2.2 指令大全

具體指令如下

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] [-v] [-x] <path> ...]
	[-cp [-f] [-p | -p[topax]] <src> ... <dst>]
	[-createSnapshot <snapshotDir> [<snapshotName>]]
	[-deleteSnapshot <snapshotDir> <snapshotName>]
	[-df [-h] [<path> ...]]
	[-du [-s] [-h] [-x] <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 [-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] <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> ...]
	[-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]
           

2.3 常用指令

注意:如果提示root權限不能進行寫操作,請切換到hdfs使用者操作

2.3.1 -help

幫助指令

注意:hadoop腳本、hdfs腳本,在hadoop安裝目錄的bin目錄下

2.3.2 -ls

顯示目錄資訊

hadoop fs -ls /
           

hdfs dfs –ls /
           

2.3.3 -mkdir

在hdfs上建立目錄

hadoop fs -mkdir -p /teaching/hdfs
           

hdfs dfs -mkdir -p /teaching/hdfs
           

2.3.4 -moveFromLocal

從本地剪切後粘貼到hdfs

touch test.txt
hadoop fs  -moveFromLocal  ./test.txt  /teaching/hdfs
           

hdfs dfs –moveFromLocal ./test.txt /teaching/hdfs
           

注意:執行過程為先複制,後删除,如果hdfs使用者不具有删除本地檔案權限則複制到hdfs能成功,删除本地檔案不能成功

2.3.5 -appendToFile

追加一個檔案到已經存在的檔案末尾

touch test2.txt
vi test2.txt
# 輸入
hello hdfs
           
hadoop fs -appendToFile test2.txt /teaching/hdfs/test.txt
           

hdfds dfs -appendToFile test2.txt /teaching/hdfs/test.txt
           

2.3.6 -cat

檢視檔案内容

hadoop fs -cat /teaching/hdfs/test.txt
           

hdfs dfs –cat /teaching/hdfs/test.txt
           

2.3.7 -tail

顯示檔案最後1kb内容

hadoop fs -tail /teaching/hdfs/test.txt
           

hdfs dfs -tail /teaching/hdfs/test.txt
           

2.3.8 -chgrp、-chmod、-chown

修改檔案所屬組、權限、使用者

  1. 修改HDFS存儲的檔案/teaching/hdfs/tt.txt檔案所屬組,預設組:supergroup選項-R遞歸執行
$ hdfs dfs -ls /teaching/hdfs
-rw-r--r--   3 hdfs supergroup         11 2019-08-12 14:40 /teaching/hdfs/tt.txt
$ hdfs dfs -chgrp lubin-group /teaching/hdfs/tt.txt
$ hdfs dfs -ls /teaching/hdfs
-rw-r--r--   3 hdfs lubin-group         11 2019-08-12 14:40 /teaching/hdfs/tt.txt
           
  1. 修改HDFS存儲的檔案/teaching/hdfs/tt.txt檔案的權限
$ hdfs dfs -chmod 666 /teaching/hdfs/tt.txt
$ hdfs dfs -ls /teaching/hdfs
-rw-rw-rw-   3 hdfs lubin-group         11 2019-08-12 14:40 /teaching/hdfs/tt.txt
           
  1. 修改HDFS存儲的檔案/teaching/hdfs/tt.txt檔案所屬的使用者群組,lubin1為使用者,lubin2為組
$ hdfs dfs -chown lubin1:lubin2 /teaching/hdfs/tt.txt
$ hdfs dfs -ls /teaching/hdfs/tt.txt
-rw-rw-rw-   3 lubin1 lubin2         11 2019-08-12 14:40 /teaching/hdfs/tt.txt
           

2.3.9 -copyFromLocal

從本地檔案系統拷貝檔案到hdfs路徑

hadoop fs -copyFromLocal README.txt /teaching/hdfs
           

hdfs dfs -copyFromLocal test3.txt /teaching/hdfs
           

2.3.10 -copyToLocal

從hdfs拷貝到本地

hadoop fs -copyToLocal /teaching/hdfs/test.txt /tmp
           

hdfs dfs –copyToLocal /teaching/hdfs/test.txt /tmp
           

2.3.11 -cp

從hdfs的一個路徑拷貝到hdfs的另一個路徑

hadoop fs -cp /teaching/hdfs/test3.txt /teaching/hdfs2
           

hdfs dfs -cp /teaching/hdfs/test3.txt /teaching/hdfs2
           

2.3.12 -mv

在hdfs目錄中移動檔案

hadoop fs -mv /teaching/hdfs/tt.txt /teaching/hdfs2
           

hdfs dfs -mv /teaching/hdfs/tt.txt /teaching/hdfs2
           

2.3.13 -get

等同于copyToLocal,從hdfs下載下傳檔案到本地

hadoop fs -get /teaching/hdfs/test3.txt /tmp
           

hdfs dfs -get /teaching/hdfs/test3.txt /tmp
           

2.3.14 -getmerge

合并下載下傳多個檔案(多個檔案的内容合并到一個檔案中),比如hdfs的目錄/aaa/下有多個檔案:log.1, log.2,log.3,…

hadoop fs -getmerge  /teaching/hdfs/log*.txt /tmp/log.txt
           

hdfs dfs -getmerge /teaching/hdfs/log*.txt /tmp/log.txt
           

2.3.15 -put

等同于copyFromLocal

hadoop fs -put log1.txt /teaching/hdfs
           

hdfs dfs –put log1.txt /teaching/hdfs
           

2.3.16 -rm

删除檔案或檔案夾

hadoop fs -rm /teaching/hdfs2/log1.txt
           

hdfs dfs –rm /teaching/hdfs2/log2.txt
           

注意:如果使用者沒有删除的權限,可以使用如下方式删除:su hdfs -c “hadoop fs -rm /test/test.txt”

2.3.17 -rmdir

删除空目錄

hadoop fs -rmdir /test
           

hdfs dfs –rmdir /test
           

注意:上述指令隻能删除空目錄,删除非空目錄用rm指令

2.3.18 -du

統計檔案夾的大小資訊

hadoop fs -du -s -h /teaching/hdfs
           

hdfs dfs -du -s -h /teaching/hdfs
58  174  /teaching/hdfs
           

注意:-s顯示總(摘要)大小,去掉-s則顯示每個比對的檔案大小;

-h以人類可讀的方式格式化大小,不是用位元組去展示;

第一列顯示目錄下總檔案大小,第二列标示該目錄下所有檔案在叢集上的總存儲大小(與副本數有關),第三列是查詢的目錄名稱

2.3.19 -setrep

設定hdfs中檔案的副本數量

hadoop fs -setrep 10 /teaching/hdfs/test.txt
           

hdfs dfs –setrep 2 /teaching/hdfs /test.txt
           
第2章 HFDS的Shell操作

設定的副本數隻是記錄在NameNode的中繼資料中,是否真的會有這麼多副本,還得看DataNode的數量。因為目前隻有3台裝置,最多也就3個副本,如果設定為10,隻有節點數的增加到10台時,副本數才能達到10。