Hadoop常用命令

獵手家園發表於2016-08-26

1、檢視Hadoop HDFS支援的所有命令

hdfs dfs

2、檢視指定目錄下內容

hdfs dfs –ls [檔案目錄]

eg:hdfs dfs –ls /user/hive

3、迴圈列出目錄、子目錄及檔案資訊

hdfs dfs –lsr [檔案目錄]

eg:hdfs dfs –lsr /user/hive

4、檢視某個已存在檔案

hdfs dfs –cat [file_path]

eg:hdfs dfs -cat /user/hive/data.txt

5、檢視最後1KB的內容

hdfs dfs –tail [file_path]

eg:hdfs dfs –tail /user/hive/test.txt

6、將本地檔案儲存至hadoop

hdfs dfs –put [本地地址] [hadoop目錄]

eg:hdfs dfs –put /home/tmp/file.txt /user/tmp

7、將hadoop上某個檔案down至本地已有目錄下

hdfs dfs -get [檔案目錄] [本地目錄]

eg:hdfs dfs –get /user/tmp/ok.txt /home/tmp

8、刪除hadoop上指定檔案

hdfs dfs –rm [-r] [-f] [檔案地址]

eg:hdfs dfs –rm /user/tmp/ok.txt

9、刪除hadoop上指定資料夾(包含子目錄等)

hdfs dfs –rm [-r] [-f] [目錄地址]

eg:hdfs dfs –rmr /user/tmp

10、在hadoop指定目錄內建立新目錄

hdfs dfs –mkdir [Hadoop目錄]

eg:hdfs dfs –mkdir /user/tmp

11、在hadoop指定目錄下新建一個空檔案

使用touchz命令:

hdfs dfs –touchz [file_path]

eg:hdfs dfs -touchz /user/new.txt

12、將hadoop上某個檔案重新命名

使用mv命令:

hdfs dfs –mv [file_path] [new_file_path]

eg:hdfs dfs –mv /user/test.txt /user/ok.txt (將test.txt重新命名為ok.txt)

13、將hadoop指定目錄下所有內容儲存為一個檔案,同時down至本地

hdfs dfs –getmerge [Hadoop目錄] [本地目錄]

eg:hdfs dfs –getmerge /user /home/t

14、將正在執行的hadoop作業kill掉

hdfs job –kill [job-id]

15、hdfs dfs –copyFromLocal test.txt /user/sunlightcs/test.txt

從本地檔案系統複製檔案到HDFS檔案系統,等同於put命令

16、hdfs dfs –copyToLocal /user/sunlightcs/test.txt test.txt

從HDFS檔案系統複製檔案到本地檔案系統,等同於get命令

17、hdfs dfs –chgrp [-R] /user/sunlightcs

修改HDFS系統中/user/sunlightcs目錄所屬群組,選項-R遞迴執行,跟linux命令一樣

18、hdfs dfs –chown [-R] /user/sunlightcs

修改HDFS系統中/user/sunlightcs目錄擁有者,選項-R遞迴執行

19、hdfs dfs –chmod [-R] MODE /user/sunlightcs

修改HDFS系統中/user/sunlightcs目錄許可權,MODE可以為相應許可權的3位數或+/-{rwx},選項-R遞迴執行

20、hdfs dfs –count [-q] PATH

檢視PATH目錄下,子目錄數、檔案數、檔案大小、檔名/目錄名

21、hdfs dfs –cp SRC [SRC …] DST

將檔案從SRC複製到DST,如果指定了多個SRC,則DST必須為一個目錄

22、hdfs dfs –du PATH

顯示該目錄中每個檔案或目錄的大小

23、hdfs dfs –dus PATH

類似於du,PATH為目錄時,會顯示該目錄的總大小

24、hdfs dfs –expunge

清空回收站,檔案被刪除時,它首先會移到臨時目錄.Trash/中,當超過延遲時間之後,檔案才會被永久刪除

25、hdfs dfs –getmerge SRC [SRC …] LOCALDST [addnl]

獲取由SRC指定的所有檔案,將它們合併為單個檔案,並寫入本地檔案系統中的LOCALDST,選項addnl將在每個檔案的末尾處加上一個換行符

26、hdfs dfs –touchz PATH

建立長度為0的空檔案

27、hdfs dfs –test –[ezd] PATH

對PATH進行如下型別的檢查:

-e PATH是否存在,如果PATH存在,返回0,否則返回1

-z 檔案是否為空,如果長度為0,返回0,否則返回1

-d 是否為目錄,如果PATH為目錄,返回0,否則返回1

28、hdfs dfs –text PATH

顯示檔案的內容,當檔案為文字檔案時,等同於cat,檔案為壓縮格式(gzip以及hadoop的二進位制序列檔案格式)時,會先解壓縮

29、hdfs dfs –help ls

檢視某個[ls]命令的幫助文件

相關文章