標籤:des style blog http java color
hadoop支援命令列操作HDFS檔案系統,並且支援shell-like命令與HDFS檔案系統互動,對於大多數程式猿/媛來說,shell-like命令列操作都是比較熟悉的,其實這也是Hadoop的極大便利之一,至少對於想熟悉乃至儘快熟練操作HDFS的人來說。
由於平時在工作中經常用到Hadoop Shell命令來操作HDFS上的檔案,有時候因為Hadoop Shell命令不熟悉,需要重新尋找;或者需要某個命令的參數;再或者需要知曉相似命令的差異;於是便有了本文,對於Hadoop Shell命令的總結,方便自己以後得工作,也算是對類似工作的總結。
hadoop fs FsShell Usage: java FsShell [-ls <path>] [-lsr <path>] [-df [<path>]] [-du <path>] [-dus <path>] [-count[-q] <path>] [-mv <src> <dst>] [-cp <src> <dst>] [-rm [-skipTrash] <path>] [-rmr [-skipTrash] <path>] [-expunge] [-put <localsrc> ... <dst>] [-copyFromLocal <localsrc> ... <dst>] [-moveFromLocal <localsrc> ... <dst>] [-get [-ignoreCrc] [-crc] <src> <localdst>] [-getmerge <src> <localdst> [addnl]] [-cat <src>] [-text <src>] [-copyToLocal [-ignoreCrc] [-crc] <src> <localdst>] [-moveToLocal [-crc] <src> <localdst>] [-mkdir <path>] [-setrep [-R] [-w] <rep> <path/file>] [-touchz <path>] [-test -[ezd] <path>] [-stat [format] <path>] [-snapshot <path>] [-tail [-f] <file>] [-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...] [-chown [-R] [OWNER][:[GROUP]] PATH...] [-chgrp [-R] GROUP PATH...] [-help [cmd]]
下面詳細解釋一下命令的操作,這些命令和Unix下的命令差不多,有些看了就明白其含義了。
hadoop fs -ls <path>
返迴文件path的統計資訊,資訊包括:
permissions number_of_replicas userid groupid filesize modification_date modification_time filename
hadoop fs -lsr <path>
這個是ls的recursive版本,類似於ls -R命令和ls的區別。
hadoop fs -du URI
顯示檔案的大小。
hadoop fs -dus URI
類似於du -s:顯示檔案及其子目錄的總體大小。
hadoop fs -df <path>
顯示Hadoop所使用的檔案系統的大小。
hadoop fs -count [-q] <path>
顯示path 下的目錄數量,檔案大小等資訊。預設情況下顯示一下資訊:
DIR_COUNT, FILE_COUNT, CONTENT_SIZE FILE_NAME
添加-q資訊後,輸出的資訊更多:
QUOTA, REMAINING_QUATA, SPACE_QUOTA, REMAINING_SPACE_QUOTA, DIR_COUNT, FILE_COUNT, CONTENT_SIZE, FILE_NAME
hadoop fs -mv <src> <dst>
從src移動到dst,允許多個源移動到同一個dst,dst必須是目錄。
hadoop fs -cp <src> ... <dst>
拷貝多個源到dst,限制是dst必須是目錄。
hadoop fs -rm [-skipTrash] <path>
刪除檔案,不能刪除目錄
-skipTrash:直接刪除檔案,不再往.Trash裡面放。
hadoop fs -rmr [-skipTrash] <path>
這個能夠迭代刪除目錄及其下面的檔案
-skipTrash:直接刪除檔案,不再往.Trash裡面放。
hadoop fs -expunge
清空Trash,Trash的概念詳見http://hadoop.apache.org/docs/r1.0.4/hdfs_design.html,參考下Trash的含義
When a file is deleted by a user or an application, it is not immediately removed from HDFS. Instead, HDFS first renames it to a file in the /trash directory. The file can be restored quickly as long as it remains in /trash. A file remains in /trash for a configurable amount of time. After the expiry of its life in /trash, the NameNode deletes the file from the HDFS namespace. The deletion of a file causes the blocks associated with the file to be freed. Note that there could be an appreciable time delay between the time a file is deleted by a user and the time of the corresponding increase in free space in HDFS.A user can Undelete a file after deleting it as long as it remains in the /trash directory. If a user wants to undelete a file that he/she has deleted, he/she can navigate the /trash directory and retrieve the file. The /trash directory contains only the latest copy of the file that was deleted. The /trash directory is just like any other directory with one special feature: HDFS applies specified policies to automatically delete files from this directory. The current default policy is to delete files from /trash that are more than 6 hours old. In the future, this policy will be configurable through a well defined interface.
檔案傳輸:
hadoop fs -put <localsrc> ... <dst>
複製單個、或者多個本地FS的目錄或者檔案到目標檔案系統。
hadoop fs -copyFromLocal <localsrc> ... <dst>
和put命令類似,唯一的限制是src必須是本地的檔案。
hadoop fs -moveFromLocal <localsrc> ... <dst>
和put命令類似,這個命令會在localsrc執行完成後,刪除本地的檔案,注意是刪除本地的檔案。
hadoop fs -get [-ignoreCrc] <localsrc> ... <dst>
拷貝FS上的src到本地的dst目錄
-ignoreCrc:拷貝時忽略crc檢驗,如果想拷貝CRCs,需要添加-crc參數。
hadoop fs -getmerge <src> <localdst> [addnl]
src是來源目錄,localdst是本地目標檔案,將來源目錄的所有檔案串連成本地目標檔案。addnl是可選的,用於指定在每個檔案結尾添加一個分行符號。
hadoop fs -cat <src>
將src的內容輸出到stdout,和Unix下面的cat功能類似。
hadoop fs -text <src>
把src檔案按照text輸出,zip或者TextRecordInputStream格式的也可以按照text輸出。
hadoop fs -copyToLocal [-ignoreCrc] [-crc] <src> <localdst>
和get命令類似,唯一的限制是dst必須是本地檔案系統的檔案。
hadoop fs -moveToLocal [-crc] <src> <localdst>
輸出:‘-moveToLocal‘ is not implemented yet,還沒有實現。
hadoop fs -mkdir <path>
建立path檔案夾,如果path的父目錄不存在,會迭代建立,類似於mkdir -p命令。
hadoop fs -setrep [-R] <rep> <path/file>
修改HDFS檔案或者目錄的Replication數目,對於重要的檔案,需要增加副本數目,確保不會丟失或者損壞。
-R參數表示迭代更新,目錄下面的副本數目同時更新。
hadoop fs -touchz <path>
建立一個大小為0的檔案。
hadoop fs -test -[ezd] <path>
測試檔案的目錄屬性,-e:測試檔案是否存在;-z:檔案大小是否為0;-d:測試是否是目錄。
hadoop fs -stat [format] <path>
返回目錄的統計資訊。
hadoop fs -tail [-f] <file>
顯示檔案的最後1kb資訊,-f參數和Unix下面的參數一樣。
hadoop fs -chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...hadoop fs -chown [-R] [OWNER][:[GROUP]] PATH...hadoop fs -chgrp [-R] GROUP PATH...
這三個是許可權操作命令,和Unix命令下的功能類似。
Hadoop Shell命令還是比較簡單的,只是其中的差別必須等到用的時候才能體會,本文只是做個備忘,將工作中常用的命令整理下。
轉自:http://isilic.iteye.com/blog/1770036