1. 程式人生 > >[Hadoop]HDFS shell命令

[Hadoop]HDFS shell命令

參考自:http://hadoop.apache.org/docs/r2.7.1/hadoop-project-dist/hadoop-common/FileSystemShell.html#appendToFile

hadoop 版本號:2.7.1

本文是根據官網提供的文件,筆者自己寫的總結,涵蓋了HDFS shell的所有命令。水平有限,錯誤在所難免。歡迎批評指正。

(1)-appendToFile

用法: hadoop fs -appendToFile <localsrc> ... <dst>

作用:附加檔案到指定檔案後。

示例:

  • hadoop fs -appendToFile localfile /user/hadoop/hadoopfile
  • hadoop fs -appendToFile localfile1 localfile2 /user/hadoop/hadoopfile
  • hadoop fs -appendToFile localfile hdfs://nn.example.com/hadoop/hadoopfile
  • hadoop fs -appendToFile - hdfs://nn.example.com/hadoop/hadoopfile Reads the input from stdin.
(2)-cat

用法:hadoop fs -cat URI [URI ...]

作用:檢視檔案內容(可以檢視本地和HDFS上的內容)。


示例:

  • hadoop fs -cat hdfs://nn1.example.com/file1 hdfs://nn2.example.com/file2
  • hadoop fs -cat file:///file3 /user/hadoop/file4
(3)-checksum

用法: hadoop fs -checksum URI

作用:檢視檔案校驗和。(例子顯示了MD5)、

示例:

  • hadoop fs -checksum hdfs://nn1.example.com/file1
  • hadoop fs -checksum file:///etc/hosts
(4)-chgrp

用法: hadoop fs -chgrp [-R] GROUP URI [URI ...]

作用:改變檔案所屬的組。(Change group association of files.)

使用-R 將使改變在目錄結構下遞迴進行。

(5)-chmod

作用:改變檔案訪問許可權。

用法:hadoop fs -chmod [-R] <MODE[,MODE]... | OCTALMODE> URI [URI ...]

這裡可以參考 linux下檔案系統的chmod的用法,基本類似。

(6)-chown

作用:hadoop fs -chown [-R] [OWNER][:[GROUP]] URI [URI ]

用法:改變檔案的所有者。使用-R 將使改變在目錄結構下遞迴進行。命令的使用者必須是超級使用者。

(7)-copyFromLocal

用法:hadoop fs -copyFromLocal <localsrc> URI

作用:類似於put命令,和put不同的是,拷貝的源地址必須是本地檔案地址。

  -f 引數 當拷貝的目標檔案存在時,進行覆蓋。

示例:

[email protected]:~$ hadoop fs -copyFromLocal testFlatMap.txt /1.txt
copyFromLocal: `/1.txt': File exists


這個時候加上-f引數。即可覆蓋。

[email protected]:~$ hadoop fs -copyFromLocal -f testFlatMap.txt /1.txt


(8)-copyToLocal

用法: hadoop fs -copyToLocal [-ignorecrc] [-crc] URI <localdst>

作用:類似於get指令。和get不同的是,拷貝的目的地址必須是本地檔案地址。

(9)-count

作用:計算paths下的目錄數,檔案數和位元組數。

用法: hadoop fs -count [-q] [-h] [-v] <paths>

  • hadoop fs -count hdfs://nn1.example.com/file1 hdfs://nn2.example.com/file2
  • hadoop fs -count -q hdfs://nn1.example.com/file1
  • hadoop fs -count -q -h hdfs://nn1.example.com/file1
  • hdfs dfs -count -q -h -v hdfs://nn1.example.com/file1
(10)-cp
用法:hadoop fs -cp [-f] [-p | -p[topax]] URI [URI ...] <dest>
作用:拷貝,HDFS檔案系統中進行的拷貝操作。 -f 引數選項:當檔案存在時,進行覆蓋。 -p 引數選項:將許可權、所屬組、時間戳、ACL以及XAttr等也進行拷貝。下面是官網的描述。
  • The -p option will preserve file attributes [topx] (timestamps, ownership, permission, ACL, XAttr). If -p is specified with no arg, then preserves timestamps, ownership, permission. If -pa is specified, then preserves permission also because ACL is a super-set of permission. Determination of whether raw namespace extended attributes are preserved is independent of the -p flag.
(11)-df
用法:hadoop fs -df [-h] URI [URI ...]
作用:顯示剩餘空間。 示例: [email protected]:~$ hadoop fs -df -h / Filesystem               Size     Used  Available  Use% hdfs://localhost:9000  18.6 G  282.1 K      9.3 G    0%
(12)-dus
作用:顯示檔案長度概要。該方法以及被捨去,等價於 -du -s 方法。見(11)
(13)-expunge
作用:清空回收站(廢紙簍)。Refer to the HDFS Architecture Guide for more information on the Trash feature.
(14)-find
作用:查詢滿足表示式的檔案和資料夾。沒有配置path的話,預設的就是全部目錄/;如果表示式沒有配置,則預設為-print。 用法: hadoop fs -find <path> ... <expression> ... -name pattern 不區分大小寫,對大小寫不敏感 -iname pattern 對大小寫敏感。 -print 列印。 -print0 列印在一行,如下圖所示。
(15)-get
作用:從HDFS上拷貝檔案到本地。 用法:hadoop fs -get [-ignorecrc] [-crc] <src> <localdst> 示例:

Example:

  • hadoop fs -get /user/hadoop/file localfile
(16)getfacl
(該條命令沒有搞明白,歡迎交流,下面貼出官網的說法) 作用:顯示檔案和資料夾的ACLs(Access Control Lists)。如果目錄有預設的ACL,則顯示之。 -R引數:遞迴顯示。 用法:

 hadoop fs -getfattr [-R] -n name | -d [-e en] <path>

Displays the extended attribute names and values (if any) for a file or directory.

Options:

  • -R: Recursively list the attributes for all files and directories.
  • -n name: Dump the named extended attribute value.
  • -d: Dump all extended attribute values associated with pathname.
  • -e encoding: Encode values after retrieving them. Valid encodings are “text”, “hex”, and “base64”. Values encoded as text strings are enclosed in double quotes ("), and values encoded as hexadecimal and base64 are prefixed with 0x and 0s, respectively.
  • path: The file or directory.
示例:
  • hadoop fs -getfattr -d /file
  • hadoop fs -getfattr -R -n user.myAttr /dir
(17)-getmerge
作用:將原始檔地址的內容合併到目標檔案上,將覆蓋目標檔案的內容。 用法: hadoop fs -getmerge <src> <localdst>
(18)-help
作用:幫助文件
(19)-ls
作用:檢視檔案,與linux下ls命令基本類似。 用法:hadoop fs -ls [-d] [-h] [-R] [-t] [-S] [-r] [-u] <args> 選項:      -d:以純檔案的形式展示目錄; [email protected]:~/testcommand$ hadoop fs -ls -d / drwxrwxrwx   - taodj supergroup          0 2015-11-27 07:00 /      -h:顯示為人眼更易識別的單位(原來是位元組)。      -R:遞迴展示 注:官網上提供的Options比較多,但是測試了,並不行(我所用的版本號不存在問題的),檢視help文件,發現help中所述與官網並不符合。懷疑是官網提供資訊有誤。
(20)-lsr
作用:已經被捨去,效果等同於-ls -R
(21)-mkdir
作用:建立資料夾。 選項:      -p:建立父目錄。類似於Unix的mkdir -p命令。
示例: [email protected]:~/testcommand$ hadoop fs -mkdir /arthur/test1/test2 mkdir: `/arthur/test1/test2': No such file or directory [email protected]:~/testcommand$ hadoop fs -mkdir -p /arthur/test1/test2
(22)-moveFromLocal
用法:hadoop fs -moveFromLocal <localsrc> <dst> 作用:類似於put命令,不同put命令的是,該操作是移動(意思就是localsrc將被刪除)。localsrc應是本地檔案。
(23)-moveToLocal
用法:hadoop fs -moveToLocal [-crc] <src> <dst> 作用:該命令尚未實現,顯示“Not implemented yet”。
(24)-mv 
用法:移動檔案。 作用: hadoop fs -mv URI [URI ...] <dest> 示例: [email protected]:~/testcommand$ hadoop fs -mv /wordcount.java /testFlatMap.txt /test1 (將前面兩個檔案移動到指定目錄下)。
(25)-put
用法: hadoop fs -put <localsrc> ... <dst> 作用:將本地的檔案上傳(複製)到HDFS是dst目錄下。
(26)-rm
用法:hadoop fs -rm [-f] [-r |-R] [-skipTrash] URI [URI ...] 作用:刪除檔案。 選項:      -f:不顯示診斷資訊和錯誤資訊(檔案不存在,路徑不正確均不會顯示資訊)。 [email protected]:~/testcommand$ hadoop fs -rm -f /111.txt [email protected]:~/testcommand$ hadoop fs -rm -f 111.txt
(27)-rmdir
用法:hadoop fs -rmdir [--ignore-fail-on-non-empty] URI [URI ...] 作用:刪除資料夾。 選項:      —ignore-fail-on-non-empty:使用它的時候,忽略因資料夾非空刪除失敗的資訊。
(28)-rmr
作用:該方法已經被捨去。和-rm -r效果一樣。遞迴刪除。
(29)-setfacl
用法:hadoop fs -setfacl [-R] [-b |-k -m |-x <acl_spec> <path>] |[--set <acl_spec> <path>] 作用:設定檔案和資料夾的ACLs。 示例:
  • hadoop fs -setfacl -m user:hadoop:rw- /file
  • hadoop fs -setfacl -x user:hadoop /file
  • hadoop fs -setfacl -b /file
  • hadoop fs -setfacl -k /dir
  • hadoop fs -setfacl --set user::rw-,user:hadoop:rw-,group::r--,other::r-- /file
  • hadoop fs -setfacl -R -m user:hadoop:r-x /dir
  • hadoop fs -setfacl -m default:user:hadoop:r-x /dir
(30)-setrep
用法:hadoop fs -setrep [-R] [-w] <numReplicas> <path> 作用:改變檔案的目標副本系數,放入REP中。選項-R將遞迴的改變PATH指定的目錄中所有檔案的目標副本系數。副本系數需要一定的時間才能達到目標值。選項-w將等待副本系數以與目標值相匹配。
(31)-stat
用法: hadoop fs -stat [format] <path> ... 作用:根據一定格式列印檔案/資料夾的統計資訊。 檔案大小 (%b), 型別 (%F), 所有者所在組 (%g), 名字 (%n), 塊大小 (%o), 副本 (%r), 使用者名稱(%u), 修改時間 (%y, %Y)。預設的是%y。 示例: hadoop fs -stat "%F %u:%g %b %y %n" /file
(32)-tail
用法:hadoop fs -tail [-f] URI 作用:輸出檔案最後1kb的內容。 選項:      -f:和unix中tail -f命令類似,當檔案內容更新時,輸出將會改變,具有實時性。 示例:用一個場景測試下。首先HDFS的/目錄下有檔案mpwtest1.txt 命令:hadoop fs -tail -f /mpwtest1.txt 開啟另外一個終端。輸入命令: hadoop fs -appendToFile mpwtest2.txt /mpwtest1.txt 可以發現 視窗1 有變化。
(33)-text
用法:hadoop fs -text <src> 作用:將HDFS中檔案以文字形式輸出(包括zip包,jar包等形式) 示例:hadoop fs -text /wc.jar
(34)-touchz
用法: hadoop fs -touchz URI [URI ...] 作用:建立一個空檔案。 示例:hadoop fs -touchz /hello.jar
(35)-truncate
用法: hadoop fs -truncate [-w] <length> <paths>

作用:將檔案按照length進行擷取。可以理解成擷取[1/length]部分。

選項:-w:

(對於該指令僅僅是個人理解,不一定正確)

如果其他應用在使用檔案時候,-w可以保證使用不會出現問題並且可以正常截斷。

比如兩個埠:一個埠執行命令: hadoop fs -tail -f /test1to10.txt

另一埠執行:hadoop fs -cat /test1to10.txt。而此時tail的埠並沒有受到影響。

示例:

  • hadoop fs -truncate 55 /user/hadoop/file1 /user/hadoop/file2
  • hadoop fs -truncate -w 127 hdfs://nn1.example.com/user/hadoop/file1
(36)-usage
用法: hadoop fs -usage command 作用:返回命令的help資訊。

相關推薦

[Hadoop]HDFS shell命令

參考自:http://hadoop.apache.org/docs/r2.7.1/hadoop-project-dist/hadoop-common/FileSystemShell.html#appe

Hadoop hdfs Shell命令 HDFS操作命令

       呼叫檔案系統(FS)Shell命令應使用 bin/hadoop fs 的形式。 所有的的FS shell命令使用URI路徑作為引數。URI格式是scheme://authority/path。對HDFS檔案系統,scheme是hdfs,對本地檔案系統,scheme是file。其中scheme和

Hadoop學習記錄(二、hdfs shell命令

在/usr/local/hadoop-2.9.2/bin目錄下執行命令 1.檢視根目錄: ./hdfs dfs -ls / 2.檔案上傳:上傳到根目錄 ./hdfs dfs -put /tmp/test.txt /   3.檢視檔案內容 ./hdfs df

Hadoop 2.x Hdfs Shell 命令

    簡單介紹下HDFS的shell命令,和Hadoop 1.x差不多 a)        建立檔案目錄:hdfs dfs -mkdir /data04 b)       檢視:    hdfs dfs -ls / c)        上傳檔案:hdfs dfs -pu

分散式系統詳解--框架(Hadoop-基本shell命令

               分散式系統詳解--框架(Hadoop-基本shell命令)         前面的文章我們已經將一個叢集搭建好了,現在就需要知道一些關於hadoo

hadoop[3]-shell命令操作

hdfs的shell操作: 大致介紹(http://hadoop.apache.org/docs/r1.0.4/cn/hdfs_user_guide.html#Shell%E5%91%BD%E4%BB%A4)操作檔案命令格式格式: hadoop fs 操作命令 引數 bin/hadoop fs

hadoop hdfs基礎命令

Hadopp hdfs元件基礎命令 首行預設 hadoop fs 選項名稱 使用格式 含義 -ls -ls <路徑> 檢視指定路徑的當前目錄結構

Hadoop fs shell命令彙總

FS Shell 呼叫檔案系統(FS)Shell命令應使用 bin/hadoop fs 的形式。 所有的的FS shell命令使用URI路徑作為引數。URI格式是scheme://authority/path。對HDFS檔案系統,scheme是hdfs,對本地檔案系統,sch

Hadoop——HDFS Shell操作

  一.查詢目錄下的檔案     1.查詢根目錄下的檔案 Hadoop fs -ls /        2.查詢資料夾下的檔案 Hadoop fs -ls /input 二.建立資料夾 had

Hadoop hdfs 常用命令

概述 前言 呼叫檔案系統(FS)Shell命令應使用 bin/hadoop fs 的形式。但也可以用hadoop dfs ,兩者效果一樣。(hadoop dfs命令已不再建議使用) 常用命令 -help 功能:輸出這個命令引數手冊 -ls 功能:顯示目錄資訊 示

Hadoop hdfs常用命令

以下是2.6版本以後的命令 1. 檔案操作 (1) 列出HDFS下的檔案  hdfs dfs -ls (2) 列出HDFS檔案下名為in的文件中的檔案 hdfs dfs -ls in (3) 上傳檔案 將當前目錄下的test1檔案上傳到HDFS上並重命

Hadoop 常用shell命令

Hadoop 常用shell命令 1. 檔案操作 檢視檔案 hadoop fs -ls / [[email protected] logs]# hadoop fs -ls / Found 2 items drwxr-xr-x - root supe

有關HDFS程式設計:HDFS Shell命令列常見操作

在/usr/local/hadoop/etc/hadoop 目錄下:幫助相關命令1.hdfs dfs     可以顯示hdfs常用命令[email protected]:/usr/local/hadoop/etc/hadoop$ hdfs dfs Usage: h

Hadoop2 HDFS shell命令

      呼叫檔案系統(FS)Shell命令應使用 bin/hadoop fs <args>的形式。 所有的的FS shell命令使用URI路徑作為引數。URI格式是scheme://authority/path。對HDFS檔案系統,scheme是hdfs,對本地檔案系統,scheme是file

HDFS】Hadoop2.x HDFS shell命令

1. hdfs dfs -appendToFile <localsrc> ... <dst> 可同時上傳多個檔案到HDFS裡面 2.  hdfs dfs -cat URI [URI ...] 檢視檔案內容 3. hdfs dfs -chgrp

hadoop hdfs dfs 命令講解

hdfs dfs命令 appendToFile Usage: hdfs dfs -appendToFile <localsrc> ... <dst> 追加一個或者多個檔案到hdfs制定檔案中.也可以從命令列讀取輸入. · hdfs dfs -appe

Hadoop fs shell命令

FS Shell 呼叫檔案系統(FS)Shell命令應使用 bin/hadoop fs 的形式。 所有的的FS shell命令使用URI路徑作為引數。URI格式是scheme://authority/path 。對 HDFS檔案系統,scheme是hdfs

Hadoop Shell命令(基於linux操作系統上傳下載文件到hdfs文件系統基本命令學習)

指南 統計信息 ... err nor 清空 ext -- als Apache-->hadoop的官網文檔命令學習:http://hadoop.apache.org/docs/r1.0.4/cn/hdfs_shell.html FS Shell 調用文件系統

Hadoop---HDFSShell命令

HDFS:是將客戶端的大檔案存放在很多節點的資料塊中。     NameNode:NameNode的作用是管理檔案目錄結構,是管理資料節點的。     NameNode維護兩套資料,一套是檔案目錄與資料塊之間的關係(資料是靜態,放

hadoop】 1009-hdfsshell命令

1.HDFS shell      1.0檢視幫助           hadoop fs -help <cmd>      1.1上傳           hadoop fs -put <linux上檔案> <hdfs上的路徑>                     [