Y's note

Web技術・プロダクトマネジメント・そして経営について

本ブログの更新を停止しており、今後は下記Noteに記載していきます。
https://note.com/yutakikuchi/

Hadoop HDFS SHELL TIPS

Hadoop Articles

今までHadoop関連で紹介した記事は以下のものです。それぞれMapReduceについては紹介したのですが、HDFSの操作については記述していなかったので今回まとめてみました。

HDFS SHELL

HDFS File System Shell Guide はてなブックマーク - HDFS File System Shell Guide

  • cat
  • chgrp
  • chmod
  • chown
  • copyFromLocal
  • copyToLocal
  • count
  • cp
  • du
  • dus
  • expunge
  • get
  • getmerge
  • ls
  • lsr
  • mkdir
  • moveFromLocal
  • moveToLocal
  • mv
  • put
  • rm
  • rmr
  • setrep
  • stat
  • tail
  • test
  • text
  • touchz

OverViews

The FileSystem (FS) shell is invoked by bin/hadoop fs . All FS shell commands take path URIs as arguments. The URI format is scheme://autority/path. For HDFS the scheme is hdfs, and for the local filesystem the scheme is file. The scheme and authority are optional. If not specified, the default scheme specified in the configuration is used. An HDFS file or directory such as /parent/child can be specified as hdfs://namenodehost/parent/child or simply as /parent/child (given that your configuration is set to point to hdfs://namenodehost). Most of the commands in FS shell behave like corresponding Unix commands. Differences are described with each of the commands. Error information is sent to stderr and the output is sent to stdout

  • FileSystemは bin/hadoop fs <コマンド引数>によって起動されます。全てのFS ShellコマンドはURIsを引数として用います。URIのフォーマットはscheme://autority/pathです。HDFSにとってschemeはhdfsであり、全てのlocal filesystemにとってschemaはファイルです。schemeとauthorityはオプションであり、指定されなくても設定上のdefaultのschemeが設定されます。HDFSのファイルやディレクトリで例えば/parent/childのようなものはhdfs://namenodehost/parent/childやシンプルに/parent/child(hdfs://namenodehostを設定で与えている事)として指定する事が可能です。ほとんどのFS ShellコマンドはUnixCommandsと似合った形で動作します。差異はそれぞれのコマンドの箇所に記述します。Error情報は標準エラーと標準出力の両方に送られます。
  • HDFS SHELLの多くのコマンドに共通して言える事は成功時に0を返し、errorでは-1を返します。

HDFS SHELL Commands

enviroment
$ hadoop version 
Hadoop 0.20.2-cdh3u3
Compiled by root on Tue Mar 20 13:45:46 PDT 2012
hadoop fs

Usage一覧が表示されます。

$ hadoop fs
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>]
           [-tail [-f] <file>]
           [-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...]
           [-chown [-R] [OWNER][:[GROUP]] PATH...]
           [-chgrp [-R] GROUP PATH...]
           [-help [cmd]]

Generic options supported are
-conf <configuration file>     specify an application configuration file
-D <property=value>            use value for given property
-fs <local|namenode:port>      specify a namenode
-jt <local|jobtracker:port>    specify a job tracker
-files <comma separated list of files>    specify comma separated files to be copied to the map reduce cluster
-libjars <comma separated list of jars>    specify comma separated jar files to include in the classpath.
-archives <comma separated list of archives>    specify comma separated archives to be unarchived on the compute machines.

The general command line syntax is
bin/hadoop command [genericOptions] [commandOptions]
cat
  • 使い方: hadoop fs -cat URI [URI …]
  • 標準出力へのコピーです。
$ hdfs -cat hdfs://localhost/user/yuta/madmagi_in/ma.txt
 ん っ ん … あっ … ! あっ … ! ひどい … 仕方 ない よ 。 彼女 一 人 で は 荷 が 重 すぎ た でも 、 彼女 も 覚悟 の 上 だろ 
chgrp
  • 使い方: hadoop fs -chgrp [-R] GROUP URI [URI …]
  • fileに対してgroupの割り当てを変更します。-Rを指定すると再帰的にgroupを変更します。実行ユーザはfileのownerやsuper-userを指定しなければなりません。その他の情報はhttp://hadoop.apache.org/common/docs/r0.20.0/hdfs_permissions_guide.htmlに記載しています。
$ hdfs -chgrp yuta hdfs://localhost/user/yuta/localfiles/empty.txt
$ hdfs -lsr hdfs://localhost/user/yuta/localfiles                   
-rw-r--r--   1 yuta yuta                0 2012-04-08 10:33 /user/yuta/localfiles/empty.txt
-rw-r--r--   1 yuta supergroup          0 2012-04-08 10:32 /user/yuta/localfiles/test.txt
chmod
$ hdfs -chmod 777 hdfs://localhost/user/yuta/localfiles/empty.txt
$ hdfs -lsr hdfs://localhost/user/yuta/localfiles                
-rw-rw-rw-   1 yuta yuta                0 2012-04-08 10:33 /user/yuta/localfiles/empty.txt
-rw-r--r--   1 yuta supergroup          0 2012-04-08 10:32 /user/yuta/localfiles/test.txt
chown
  • 使い方: hadoop fs -chown [-R] [OWNER][:[GROUP]] URI [URI ]
  • ファイルのownerを変更します。-Rを指定すると再帰的に変更をします。実行ユーザはfileのownerかsuper-userが指定しなければなりません。その他の情報はhttp://hadoop.apache.org/common/docs/r0.20.0/hdfs_permissions_guide.htmlに記載しています。
$ hdfs -chown -R superuser hdfs://localhost/user/yuta/localfiles/
copyFromLocal
  • 使い方: hadoop fs -copyFromLocal URI
  • putコマンドと良く似ていてHDFS上にファイルを配置します。
$ hdfs -copyFromLocal localfiles hdfs://localhost/user/yuta/
$ hdfs -lsr hdfs://localhost/user/yuta/                     
drwxr-xr-x   - yuta supergroup          0 2012-04-08 10:47 /user/yuta/localfiles
-rw-r--r--   1 yuta supergroup          0 2012-04-08 10:47 /user/yuta/localfiles/test.txt
copyToLocal
  • 使い方: hadoop fs -copyToLocal [-ignorecrc] [-crc] URI
  • getコマンドと良く似ていてHDSF上からファイルをコピーします。
$ hdfs -copyToLocal hdfs://localhost/user/yuta/remotefiles remotefiles
$ ls remotefiles 
合計 20
drwxr-xr-x 2 yuta yuta 4096  4月  8 10:49 .
drwxrwxrwx 8 yuta yuta 4096  4月  8 10:49 ..
-rw-r--r-- 1 yuta yuta    0  4月  8 10:49 test.txt
count
  • 使い方: hadoop fs -count [-q]
  • 指定パス上でパターンにマッチしたディレクトリやファイル,byte数等をカウントします。出力カラムは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
$ hdfs -count hdfs://localhost/user/yuta/madmagi_in/      
           1            2             205706 hdfs://localhost/user/yuta/madmagi_in
cp
  • 使い方: hadoop fs -cp URI [URI …]
  • 元ファイルから対象ファイルにコピーします。コピー先にディレクトリを指定すれば複数の元ファイルを指定できます。
$ hdfs -cp hdfs://localhost/user/yuta/madmagi_in/ma.txt hdfs://localhost/user/yuta/madmagi_in/ma2.txt
$ hdfs -ls hdfs://localhost/user/yuta/madmagi_in/       
Found 3 items
-rw-r--r--   1 yuta supergroup     104440 2012-03-26 01:16 /user/yuta/madmagi_in/ma.txt
-rw-r--r--   1 yuta supergroup     104440 2012-04-08 09:51 /user/yuta/madmagi_in/ma2.txt
-rw-r--r--   1 yuta supergroup     101266 2012-03-26 01:16 /user/yuta/madmagi_in/word.txt
du
  • 使い方: hadoop fs -du URI [URI …]
  • ディレクトリに含まれるファイルのサイズを表示します。
$ hdfs -du 
Found 3 items
205706      hdfs://localhost/user/yuta/madmagi_in
89841       hdfs://localhost/user/yuta/madmagi_out_ma
124927      hdfs://localhost/user/yuta/madmagi_out_word
dus
  • 使い方: hadoop fs -dus
  • fileサイズのサマリーを表示します。
$ hdfs -dus
hdfs://localhost/user/yuta	420474
expunge
$hdfs -expunge
get
  • 使い方: hadoop fs -get [-ignorecrc] [-crc]
  • localにファイルをコピーします。-iginorecrcでCRCchecksum確認を無視してコピーします。CRCを利用したい場合は-crcと指定します。
$ hdfs -get hdfs://localhost/user/yuta/madmagi_in copylocal
$ ls -1 copylocal                                       
合計 236
drwxr-xr-x 2 yuta yuta   4096  4月  8 09:03 .
drwxrwxrwx 6 yuta yuta   4096  4月  8 09:03 ..
-rw-r--r-- 1 yuta yuta 104440  4月  8 09:03 ma.txt
-rw-r--r-- 1 yuta yuta 101266  4月  8 09:03 word.txt
getmerge
  • 使い方: hadoop fs -getmerge [addnl]
  • ファイルをmergeして出力します。addnlを指定するとそれぞれのファイルの末尾に新しい行ラインを追加する事ができます。
  • 下の実行結果がうまく確認できていません。
$ hdfs -getmerge hdfs://localhost/user/yuta/madmagi_in/word.txt copylocal/test.txt addnl
ls
  • 使い方: hadoop fs -ls
  • 以下のフォーマットに従ったファイルに関する出力を行います。permissions number_of_replicas userid groupid filesize modification_date modification_time filename。ディレクトリの場合は以下のフォーマットに従いリストで返します。permissions userid groupid modification_date modification_time dirname。
$ hdfs -ls hdfs://localhost/user/yuta/madmagi_in/                               
Found 2 items
-rw-r--r--   1 yuta supergroup     104440 2012-03-26 01:16 /user/yuta/madmagi_in/ma.txt
-rw-r--r--   1 yuta supergroup     101266 2012-03-26 01:16 /user/yuta/madmagi_in/word.txt
lsr
  • 使い方: hadoop fs -lsr
  • lsの再帰的版です。unixのls -Rと似ています。
$ hdfs -lsr hdfs://localhost/user/yuta/            
drwxr-xr-x   - yuta supergroup          0 2012-03-26 01:17 /user/yuta/madmagi_in
-rw-r--r--   1 yuta supergroup     104440 2012-03-26 01:16 /user/yuta/madmagi_in/ma.txt
-rw-r--r--   1 yuta supergroup     101266 2012-03-26 01:16 /user/yuta/madmagi_in/word.txt
drwxr-xr-x   - yuta supergroup          0 2012-03-26 02:11 /user/yuta/madmagi_out_ma
-rw-r--r--   1 yuta supergroup          0 2012-03-26 02:11 /user/yuta/madmagi_out_ma/_SUCCESS
drwxr-xr-x   - yuta supergroup          0 2012-03-26 02:10 /user/yuta/madmagi_out_ma/_logs
drwxr-xr-x   - yuta supergroup          0 2012-03-26 02:10 /user/yuta/madmagi_out_ma/_logs/history
-rw-r--r--   1 yuta supergroup      50482 2012-03-26 02:10 /user/yuta/madmagi_out_ma/_logs/history/localhost_1332691893518_job_201203260111_0018_conf.xml
-rw-r--r--   1 yuta supergroup      15065 2012-03-26 02:10 /user/yuta/madmagi_out_ma/_logs/history/localhost_1332691893518_job_201203260111_0018_yuta_streamjob2162462095464428994.jar
mkdir
  • 使い方: hadoop fs -mkdir
  • URIのパスに対してディレクトリを作成します。これはunixのmkdir -pの動作に似ています。
$ hdfs -mkdir parent/child
$ hdfs -lsr 
drwxr-xr-x   - yuta supergroup          0 2012-04-08 09:28 /user/yuta/parent
drwxr-xr-x   - yuta supergroup          0 2012-04-08 09:28 /user/yuta/parent/child
moveFromLocal
  • 使い方: hadoop fs -moveFromLocal
  • putコマンドと良く似ていてローカルファイルをHDFS上に移動させます。
$ hdfs -moveFromLocal localfiles hdfs://localhost/user/yuta/
$ hdfs -lsr                                                 
drwxr-xr-x   - yuta supergroup          0 2012-04-08 10:54 /user/yuta/localfiles
-rw-r--r--   1 yuta supergroup          0 2012-04-08 10:54 /user/yuta/localfiles/test.txt
moveToLocal
  • 使い方: hadoop fs -moveToLocal [-crc]
  • また実装が済んでいないというメッセージが表示されます。
  • このコマンドは実行結果が確認できていません。
$ hdfs -moveToLocal hdfs://localhost/user/yuta/localfiles localfiles
Option '-moveToLocal' is not implemented yet.
mv
  • 使い方: hadoop fs -mv URI [URI …]
  • fileを目的の箇所に移動させます。ディレクトリに対して複数のファイルを指定する事が可能です。filesystemをまたぐような移動は許可されていません。
$ hdfs -mv hdfs://localhost/user/yuta/madmagi_in/ma2.txt hdfs://localhost/user/yuta/madmagi_in/ma_test.txt
$ hdfs -ls hdfs://localhost/user/yuta/madmagi_in/ 
Found 3 items
-rw-r--r--   1 yuta supergroup     104440 2012-03-26 01:16 /user/yuta/madmagi_in/ma.txt
-rw-r--r--   1 yuta supergroup     104440 2012-04-08 09:51 /user/yuta/madmagi_in/ma_test.txt
-rw-r--r--   1 yuta supergroup     101266 2012-03-26 01:16 /user/yuta/madmagi_in/word.txt
put
  • 使い方: hadoop fs -put ...
  • 単体や複数のファイルをHDFS上にコピーします。また標準入力からinputしてHDFS上のファイルに書き込む事が出来ます。
$ hdfs -put localfiles hdfs://localhost/user/yuta/
$ hdfs -lsr hdfs://localhost/user/yuta/localfiles
-rw-r--r--   1 yuta supergroup          0 2012-04-08 10:32 /user/yuta/localfiles/test.txt
rm
  • 使い方: hadoop fs -rm URI [URI …]
  • 引数に与えられたファイルを削除します。空じゃないディレクトリやファイルを削除するだけです。再帰的な削除を行うにはrmrを使います。
$ hdfs -rm hdfs://localhost/user/yuta/madmagi_in/0byte.txt 
Deleted hdfs://localhost/user/yuta/madmagi_in/0byte.txt
rmr
  • 使い方: hadoop fs -rmr URI [URI …]
  • 再帰的に削除します。
$ hdfs -rmr hdfs://localhost/user/yuta/parent      
Deleted hdfs://localhost/user/yuta/parent
setrep
  • 使い方: hadoop fs -setrep [-R]
  • ファイルのレプリケーション要素を変更します。-Rを付ける事で再帰的に変更が出来ます。
  • このコマンドは実行結果が確認できていません。
$ hdfs -setrep -w 3 -R  hdfs://localhost/user/yuta/parent/child
stat
  • 使い方: hadoop fs -stat URI [URI …]
  • パスのstatus情報を表示します。
$ hdfs -stat hdfs://localhost/user/yuta/madmagi_in/
2012-04-08 00:54:03
tail
  • 使い方: hadoop fs -tail [-f] URI
  • 標準出力の最後のkilobyteを表示します。unixコマンドと同じように-fオプションを使う事ができます。
$ hdfs -tail  hdfs://localhost/user/yuta/madmagi_in/ma.txt 
し ふうん ―― やっぱり 理解 でき ない なあ 、 人間 の 価値 観 は 今夜 は つくづく 瘴気 が 濃い ね 魔 獣 ども も 、 次 から 次 へ と 湧い て くる ―― 幾ら 倒し て も キリ が ない ボヤい た って 仕方 ない わ 。 さあ 、 行く わ よ がんばっ て 
test
  • 使い方: hadoop fs -test -[ezd] URI
  • -e:ファイルが存在するかチェック、-z:ファイルが0バイトかチェック、-d:パスがディレクトリかチェック
  • このコマンドは実行結果が確認できていません。
$ hdfs -test -z  hdfs://localhost/user/yuta/madmagi_in/ma.txt
text
  • 使い方:hadoop fs -text
  • textモードにして出力します。zipやTextRecordInputStreamの形式を許可しています。
$ hdfs -text hdfs://localhost/user/yuta/madmagi_in/ma.txt    
12/04/08 10:19:25 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
12/04/08 10:19:25 WARN snappy.LoadSnappy: Snappy native library not loaded
 ん っ ん … あっ … ! あっ … ! ひどい … 仕方 ない よ 。 彼女 一 人 で は 荷 が 重 すぎ た でも 、 彼女 も 覚悟 の 上 だろ
touchz
  • 使い方: hadoop fs -touchz URI [URI …]
  • 0バイトのファイルを作成します。
$ hdfs -touchz hdfs://localhost/user/yuta/madmagi_in/0byte.txt 
$ hdfs -ls hdfs://localhost/user/yuta/madmagi_in/       
Found 4 items
-rw-r--r--   1 yuta supergroup          0 2012-04-08 10:16 /user/yuta/madmagi_in/0byte.txt
-rw-r--r--   1 yuta supergroup     104440 2012-03-26 01:16 /user/yuta/madmagi_in/ma.txt
-rw-r--r--   1 yuta supergroup     104440 2012-04-08 09:51 /user/yuta/madmagi_in/ma_test.txt
-rw-r--r--   1 yuta supergroup     101266 2012-03-26 01:16 /user/yuta/madmagi_in/word.txt