wiki:waue/2009/0403

Version 13 (modified by waue, 15 years ago) (diff)

--

HDFS shell 的用法

  • bin/hadoop dfs <args> ,下面則列出 <args> 的用法
  • 以下操作預設的目錄在 /user/<$username>/ 下
    $ bin/hadoop dfs -ls input
    Found 4 items
    -rw-r--r--   2 waue supergroup  115045564 2009-04-02 11:51 /user/waue/input/1.txt
    -rw-r--r--   2 waue supergroup     987864 2009-04-02 11:51 /user/waue/input/2.txt
    -rw-r--r--   2 waue supergroup    1573048 2009-04-02 11:51 /user/waue/input/3.txt
    -rw-r--r--   2 waue supergroup   25844527 2009-04-02 11:51 /user/waue/input/4.txt
    
  • 完整的路徑則是 hdfs://node:port/path 如:
    $ bin/hadoop dfs -ls hdfs://gm1.nchc.org.tw:9000/user/waue/input
    Found 4 items
    -rw-r--r--   2 waue supergroup  115045564 2009-04-02 11:51 /user/waue/input/1.txt
    -rw-r--r--   2 waue supergroup     987864 2009-04-02 11:51 /user/waue/input/2.txt
    -rw-r--r--   2 waue supergroup    1573048 2009-04-02 11:51 /user/waue/input/3.txt
    -rw-r--r--   2 waue supergroup   25844527 2009-04-02 11:51 /user/waue/input/4.txt
    

-cat

  • 將路徑指定文件的內容輸出到stdout
    $ bin/hadoop dfs -cat quota/hadoop-env.sh
    

-chgrp

  • 改變文件所屬的組
    $ bin/hadoop dfs -chgrp -R waue own
    

-chmod

  • 改變文件的權限
    $ bin/hadoop dfs -chmod -R 755 own
    

-chown

  • 改變文件的擁有者
    $ bin/hadoop dfs -chown -R waue own
    

-copyFromLocal, -put

  • 從local放檔案到hdfs
    $ bin/hadoop dfs -put input dfs_input
    

-copyToLocal, -get

  • 把hdfs上得檔案下載到 local
    $ bin/hadoop dfs -get dfs_input input1
    

-cp

  • 將文件從hdfs原本路徑複製到hdfs目標路徑
    $ bin/hadoop dfs -cp own waue
    

-du

  • 顯示目錄中所有文件的大小
    $ bin/hadoop dfs -du input
    
    Found 4 items
    115045564   hdfs://gm1.nchc.org.tw:9000/user/waue/input/1.txt
    987864      hdfs://gm1.nchc.org.tw:9000/user/waue/input/2.txt
    1573048     hdfs://gm1.nchc.org.tw:9000/user/waue/input/3.txt
    25844527    hdfs://gm1.nchc.org.tw:9000/user/waue/input/4.txt
    

-dus

  • 顯示該目錄/文件的總大小
    $ bin/hadoop dfs -dus input
    
    hdfs://gm1.nchc.org.tw:9000/user/waue/input	143451003
    

-expunge

  • 清空垃圾桶
    $ bin/hadoop fs -expunge
    

-getmerge

  • 將來源目錄<src>下所有的文件都集合到本地端一個<localdst>檔案內
  • bin/hadoop fs -getmerge <src> <localdst>
    $ echo "this is one; " >> in1/input
    $ echo "this is two; " >> in1/input2
    $ bin/hadoop dfs -put in1 in1
    $ bin/hadoop dfs -getmerge in1 merge.txt
    $ cat ./merge.txt
    

-ls

  • 列出文件或目錄的資訊
  • 文件名 <副本數> 文件大小 修改日期 修改時間 權限 用戶ID 組ID
  • 目錄名 <dir> 修改日期 修改時間 權限 用戶ID 組ID
    $ bin/hadoop dfs -ls
    

-lsr

  • ls命令的遞迴版本
    $ bin/hadoop dfs -lsr /
    

-mkdir

  • 建立資料夾
    $ bin/hadoop dfs -mkdir a b c
    

-moveFromLocal

  • 將local端的資料夾剪下移動到hdfs上
    $ bin/hadoop dfs -moveFromLocal in1 in2
    

-mv

  • 更改資料的名稱
    $ bin/hadoop dfs -mv in2 in3
    

-rm

  • 刪除指定的檔案(不可資料夾)
    $ bin/hadoop dfs -rm in1/input
    

-rmr

  • 遞迴刪除資料夾(包含在內的所有檔案)
    $ bin/hadoop dfs -rmr in1
    

-setrep

  • 設定副本係數
  • bin/hadoop fs -setrep [-R] [-w] <rep> <path/file>
    $ bin/hadoop fs -setrep -w 2 -R input 
    Replication 2 set: hdfs://gm1.nchc.org.tw:9000/user/waue/input/1.txt
    Replication 2 set: hdfs://gm1.nchc.org.tw:9000/user/waue/input/2.txt
    Replication 2 set: hdfs://gm1.nchc.org.tw:9000/user/waue/input/3.txt
    Replication 2 set: hdfs://gm1.nchc.org.tw:9000/user/waue/input/4.txt
    Waiting for hdfs://gm1.nchc.org.tw:9000/user/waue/input/1.txt ... done
    Waiting for hdfs://gm1.nchc.org.tw:9000/user/waue/input/2.txt ... done
    Waiting for hdfs://gm1.nchc.org.tw:9000/user/waue/input/3.txt ... done
    Waiting for hdfs://gm1.nchc.org.tw:9000/user/waue/input/4.txt ... done
    

-stat

  • 印出時間資訊
    $ bin/hadoop fs -stat input
    2009-04-02 03:51:29
    

-tail

  • 將文件的最後1k內容輸出
  • 用法 : bin/hadoop fs -tail [-f] 檔案 (-f 參數用來顯示如果檔案增大,則秀出被append上得內容)
    $ bin/hadoop dfs -tail input/1.txt
    

-test

  • 測試檔案, -e 檢查文件是否存在(1=存在, 0=否), -z 檢查文件是否為空(1=空, 0=不為空), -d 檢查是否為目錄(1=存在, 0=否)
    • 要用echo $? 來看回傳值為 0 or 1
  • 用法: bin/hadoop fs -test -[ezd] URI

$ bin/hadoop dfs -test -e /user/waue/input/5.txt
$ bin/hadoop dfs -test -z /user/waue/input/5.txt
test: File does not exist: /user/waue/input/5.txt
$ bin/hadoop dfs -test -d /user/waue/input/5.txt

test: File does not exist: /user/waue/input/5.txt

-text

  • 將檔案(如壓縮檔, textrecordinputstream)輸出為純文字格式
  • hadoop fs -text <src>
    $ hadoop dfs -text macadr-eth1.txt.gz
    00:1b:fc:61:75:b1
    00:1b:fc:58:9c:23
    
  • ps : 目前沒支援zip的函式庫
    $ bin/hadoop dfs -text b/a.txt.zip
    PK
    ���:��H{
            a.txtUT	b��Ib��IUx��sssss
    test
    PK
    ���:��H{
    ��a.txtUTb��IUxPK@C
    

-touchz

  • 建立一個空文件
    $ bin/hadoop dfs -touchz b/kk
    $ bin/hadoop dfs -test -z b/kk
    $ echo $?
    1
    $ bin/hadoop dfs -test -z b/a.txt.zip
    $ echo $?
    0