天天看點

hadoop指令搜集

随機傳回指定行數的樣本資料

hadoop fs -cat /test/gonganbu/scene_analysis_suggestion/* | shuf -n 5

傳回前幾行的樣本資料

hadoop fs -cat /test/gonganbu/scene_analysis_suggestion/* | head -100

傳回最後幾行的樣本資料

hadoop fs -cat /test/gonganbu/scene_analysis_suggestion/* | tail -5

檢視文本行數

hadoop fs -cat hdfs://172.16.0.226:8020/test/sys_dict/sysdict_case_type.csv |wc -l

檢視檔案大小(機關byte)

hadoop fs -du hdfs://172.16.0.226:8020/test/sys_dict/*

hadoop fs -count hdfs://172.16.0.226:8020/test/sys_dict/*

-- 檢視hadoop fs幫助資訊

[root@hadp-master sbin]# hadoop fs

Usage: hadoop fs [generic options]

[-appendToFile ... ]

[-cat [-ignoreCrc] ...]

[-checksum ...]

[-chgrp [-R] GROUP PATH...]

[-chmod [-R] PATH...]

[-chown [-R] [OWNER][:[GROUP]] PATH...]

[-copyFromLocal [-f] [-p] [-l] ... ]

[-copyToLocal [-p] [-ignoreCrc] [-crc] ... ]

[-count [-q] [-h] ...]

[-cp [-f] [-p | -p[topax]] ... ]

[-createSnapshot []]

[-deleteSnapshot ]

[-df [-h] [ ...]]

[-du [-s] [-h] ...]

[-expunge]

[-find ... ...]

[-get [-p] [-ignoreCrc] [-crc] ... ]

[-getfacl [-R] ]

[-getfattr [-R] {-n name | -d} [-e en] ]

[-getmerge [-nl] ]

[-help [cmd ...]]

[-ls [-d] [-h] [-R] [ ...]]

[-mkdir [-p] ...]

[-moveFromLocal ... ]

[-moveToLocal ]

[-mv ... ]

[-put [-f] [-p] [-l] ... ]

[-renameSnapshot ]

[-rm [-f] [-r|-R] [-skipTrash] ...]

[-rmdir [--ignore-fail-on-non-empty]

...]

[-setfacl [-R] [{-b|-k} {-m|-x } ]|[--set ]]

[-setfattr {-n name [-v value] | -x name} ]

[-setrep [-R] [-w] ...]

[-stat [format] ...]

[-tail [-f] ]

[-test -[defsz] ]

[-text [-ignoreCrc] ...]

[-touchz ...]

[-truncate [-w] ...]

[-usage [cmd ...]]

注意:

以下指令均是在,Linux 指令行視窗界面操作。

[ ]表示可選參數,<>表示必須參數。

開始使用指令前,必須啟動Hadoop

(1)-appendToFile

用法: hadoop fs -appendToFile ...

作用:是将一個或者多個檔案添加到HDFS系統中。

示例:

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] URI [URI ...]

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

(6)-chown

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

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

(7)-copyFromLocal

用法:hadoop fs -copyFromLocal URI

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

-f 參數 當拷貝的目标檔案存在時,進行覆寫。

示例:

[root@two1 fanrui]# hadoop fs -copyFromLocal testFlatMap.txt /1.txt

copyFromLocal: `/1.txt\': File exists

這個時候加上-f參數。即可覆寫。

[root@two1 fanrui]# hadoop fs -copyFromLocal -f testFlatMap.txt /1.txt

(8)-copyToLocal

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

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

(9)-count

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

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

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 ...]

作用:拷貝,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.

示例:

[root@two1 fanrui]# hadoop fs -cp -p /tmp/fan /tmp/fan1

(11)-df

用法:hadoop fs -df [-h] URI [URI ...]

作用:顯示剩餘空間。

示例:

[root@two1 fanrui]# hadoop fs -df /

Filesystem Size Used Available Use%

hdfs://localhost:9000 37626667008 311296 24792702976 0%

(12)-dus

作用:顯示檔案長度概要。該方法已經被舍去,等價于 -du -s 方法。見(11)

(13)-expunge

作用:從垃圾桶目錄永久删除超過保留門檻值的檢查點中的檔案,并建立新檢查點。

用法:hadoop fs -expunge

(14)-find

作用:查找滿足表達式的檔案和檔案夾。沒有配置path的話,預設的就是全部目錄/;如果表達式沒有配置,則預設為-print。

用法: hadoop fs -find ... ...

-name pattern 所要查找檔案的檔案名。

-iname pattern 所要查找的檔案名,不區分大小寫。

-print 列印。

-print0 列印在一行,如下圖所示。

示例:

hadoop fs -find / -name test -print

(15)-get

作用:從HDFS上拷貝檔案到本地。

用法:hadoop fs -get [-ignorecrc] [-crc]

示例:

hadoop fs -get /user/hadoop/file localfile

hadoop fs -get hdfs://nn.example.com/user/hadoop/file localfile

(16)getfacl

作用:顯示檔案和檔案夾的ACLs(Access Control Lists)。如果目錄有預設的ACL,則顯示之。

-R參數:遞歸顯示。

用法:

hadoop fs -getfacl [-R]

Options:

-R: List the ACLs of all files and directories recursively.

path: File or directory to list.

示例:

hadoop fs -getfacl /file

hadoop fs -getfacl -R /dir

Exit Code:

Returns 0 on success and non-zero on error.

(17)getfattr

作用:顯示檔案或目錄的擴充屬性名和值(如果有的話)

用法:hadoop fs -getfattr [-R] -n name | -d [-e en]

Options:

-R:遞歸顯示檔案夾和檔案。

-n name:轉儲命名的擴充屬性值。

-d:轉儲與路徑名相關聯的所有擴充屬性值。

-e en: 檢索後的值進行編碼。 有效的編碼是 “text”, “hex”, and “base64”. 值編碼作為文本字元串是用雙引号括起來的(“),值編碼作為16進制和64進制,字首分别為 0x 和 0s。

path:檔案或檔案夾路徑。

示例:

hadoop fs -getfattr -d /file

hadoop fs -getfattr -R -n user.myAttr /dir

(18)-getmerge

作用:是将HDFS上一個目錄中所有的檔案合并到一起輸出到一個本地檔案上。

用法:hadoop fs -getmerge [-nl]

示例:

hadoop fs -getmerge -nl /src /opt/output.txt

hadoop fs -getmerge -nl /src/file1.txt /src/file2.txt /output.txt

(19)-help

作用:幫助文檔

用法:hadoop fs -help

(20)-ls

作用:檢視檔案,與linux下ls指令基本類似。

用法:hadoop fs -ls [-d] [-h] [-R]

選項:

-d:隻展示查詢展示目錄;

-h:顯示為人眼更易識别的機關(原來是位元組)。

-R:遞歸展示,顯示所有的檔案夾及檔案

示例:

hadoop fs -ls -d /

hadoop fs -ls -h /

hadoop fs -ls -R /

-lsr

作用:已經被舍去,效果等同于-ls -R

(21)-mkdir

作用:建立檔案夾。

用法:hadoop fs -mkdir [-p]

選項:

-p:建立父目錄。類似于Unix的mkdir -p指令。

示例:

hadoop fs -mkdir /user/hadoop/dir1 /user/hadoop/dir2

hadoop fs -mkdir hdfs://nn1.example.com/user/hadoop/dir hdfs://nn2.example.com/user/hadoop/dir

(22)-moveFromLocal

用法:hadoop fs -moveFromLocal

作用:類似于put指令,不同put指令的是,該操作是移動(意思就是localsrc将被删除)。localsrc應是本地檔案。

(23)-moveToLocal

用法:hadoop fs -moveToLocal [-crc]

作用:該指令尚未實作,顯示“Not implemented yet”。

(24)-mv

用法:移動檔案。

作用: hadoop fs -mv URI [URI ...]

示例:

hadoop fs -mv /user/hadoop/file1 /user/hadoop/file2

hadoop fs -mv hdfs://nn.example.com/file1 hdfs://nn.example.com/file2 hdfs://nn.example.com/file3 hdfs://nn.example.com/dir1

(25)-put

用法: hadoop fs -put ...

作用:将本地的檔案上傳(複制)到HDFS是dst目錄下。

示例:

hadoop fs -put localfile /user/hadoop/hadoopfile

hadoop fs -put localfile1 localfile2 /user/hadoop/hadoopdir

hadoop fs -put localfile hdfs://nn.example.com/hadoop/hadoopfile

hadoop fs -put - hdfs://nn.example.com/hadoop/hadoopfile Reads the input from stdin.

(26)-rm

用法:hadoop fs -rm [-f] [-r |-R] [-skipTrash] URI [URI ...]

作用:删除檔案。

選項:

The -f option will not display a diagnostic message or modify the exit status to reflect an error if the file does not exist.

The -R option deletes the directory and any content under it recursively.

The -r option is equivalent to -R.

The -skipTrash option will bypass trash, if enabled, and delete the specified file(s) immediately. This can be useful when it is necessary to delete files from an over-quota directory.

示例:

hadoop fs -rm hdfs://nn.example.com/file /user/hadoop/emptydir

(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 ] |[--set ]

作用:設定通路控制清單(ACL)的檔案和目錄。

選項:

-b:移除所有除了基本的ACL條目。使用者、組和其他的條目被保留為與權限位的相容性。

-k:删除預設的ACL。

-R: 遞歸應用于所有檔案和目錄的操作。

-m:修改ACL。新的項目添加到ACL,并保留現有的條目。

-x:删除指定的ACL條目。其他保留ACL條目。

–set:完全替換ACL,丢棄所有現有的條目。acl_spec必須包括使用者,組,和其他有權限位的相容性。

acl_spec:逗号分隔的ACL條目清單。

path:修改檔案或目錄。

示例:

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]

作用:改變檔案的目标副本系數,放入REP中。選項-R将遞歸的改變PATH指定的目錄中所有檔案的目标副本系數。副本系數需要一定的時間才能達到目标值。選項-w将等待副本系數以與目标值相比對。

示例:

hadoop fs -setrep -w 3 /user/hadoop/dir1

(31)-stat

用法: hadoop fs -stat [format] ...

作用:根據一定格式列印檔案/檔案夾的統計資訊。 檔案大小 (%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)-test

作用:判斷檔案資訊

用法:hadoop fs -test -[defsz] URI

選項:

-d:如果路徑是一個目錄,傳回0

-e:如果路徑已經存在,傳回0

-f: 如果路徑是一個檔案,傳回0

-s:如果路徑不是空,傳回0

-z:如果檔案長度為0,傳回0

URI:資源位址,可以是檔案也可以是目錄。

示例:

hadoop fs -test -e filename

(34)-text

用法:hadoop fs -text

作用:将HDFS中檔案以文本形式輸出(包括zip包,jar包等形式)

示例:hadoop fs -text /wc.jar

(35)-touchz

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

作用:建立一個空檔案。

示例:hadoop fs -touchz /hello.jar

(35)-truncate

用法: hadoop fs -truncate [-w]

作用:截斷指定長度比對的所有檔案内容。

選項:

-w:需要等待指令完成塊恢複。如果沒有-w選項,在恢複的過程中可能是未閉合的。

length:截斷處的值,如果是100,則表示在100B處截斷。

paths:檔案位址。

示例:

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指令搜集