2012-04-09
Hadoop HDFS SHELL TIPS
Hadoop | |
![]()
Hadoop Articles
今までHadoop関連で紹介した記事は以下のものです。それぞれMapReduceについては紹介したのですが、HDFSの操作については記述していなかったので今回まとめてみました。
HDFS SHELL
OverViews
The FileSystem (FS) shell is invoked by bin/hadoop fs <args>. 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
- 基本的にはHDFS上のファイルおよびローカルのファイルに対する操作コマンドです。参照系/更新系のコマンドがあります。
- hadoop fs と毎回コマンドとして打つのが面倒なのでalias hdfs='hadoop fs'などとするとキータイプが短くて済みます。
- 似た動作をするコマンドが複数あるのでどれか一つを覚えると良いと思います。例えばcopyFromLocalはputとほぼ同義です。
- 下で実際に実行したデータについては 「魔法少女まどか☆マギカ」の台詞をJavaScriptでMapReduceしてGoogle Chart APIでグラフ出力したよ! - Yuta.Kikuchiの日記
ここで用いた内容です。
enviroment
$ hadoop version Hadoop 0.20.2-cdh3u3 Compiled by root on Tue Mar 20 13:45:46 PDT 2012hadoop 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
$ 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.txtchmod
- 使い方: hadoop fs -chmod [-R] <MODE[,MODE]... | OCTALMODE> URI [URI …]
- permissionを変更します。-Rを指定すると再帰的に変更をします。実行ユーザはfileのownerかsuper-userが指定しなければなりません。その他の情報はhttp://hadoop.apache.org/common/docs/r0.20.0/hdfs_permissions_guide.htmlに記載しています。
$ 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.txtchown
- 使い方: 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
$ 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.txtcopyToLocal
$ 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.txtcount
- 使い方: hadoop fs -count [-q] <paths>
- 指定パス上でパターンにマッチしたディレクトリやファイル,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_incp
$ 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.txtdu
$ 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_worddus
- 使い方: hadoop fs -dus <args>
- fileサイズのサマリーを表示します。
$ hdfs -dus hdfs://localhost/user/yuta 420474expunge
- 使い方: hadoop fs -expunge
- 空を消します。その他の情報はhttp://hadoop.apache.org/common/docs/r0.20.0/hdfs_design.html に記載されています。
- このコマンドは実行結果が確認できていません。
$hdfs -expungeget
- 使い方: hadoop fs -get [-ignorecrc] [-crc] <src> <localdst>
- 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.txtgetmerge
- 使い方: hadoop fs -getmerge <src> <localdst> [addnl]
- ファイルをmergeして出力します。addnlを指定するとそれぞれのファイルの末尾に新しい行ラインを追加する事ができます。
- 下の実行結果がうまく確認できていません。
$ hdfs -getmerge hdfs://localhost/user/yuta/madmagi_in/word.txt copylocal/test.txt addnlls
- 使い方: hadoop fs -ls <args>
- 以下のフォーマットに従ったファイルに関する出力を行います。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.txtlsr
$ 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.jarmkdir
$ 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/childmoveFromLocal
$ 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.txtmoveToLocal
$ hdfs -moveToLocal hdfs://localhost/user/yuta/localfiles localfiles Option '-moveToLocal' is not implemented yet.mv
- 使い方: hadoop fs -mv URI [URI …] <dest>
- 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.txtput
- 使い方: hadoop fs -put <localsrc> ... <dst>
- 単体や複数のファイルを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.txtrm
$ hdfs -rm hdfs://localhost/user/yuta/madmagi_in/0byte.txt Deleted hdfs://localhost/user/yuta/madmagi_in/0byte.txtrmr
$ hdfs -rmr hdfs://localhost/user/yuta/parent Deleted hdfs://localhost/user/yuta/parentsetrep
- 使い方: hadoop fs -setrep [-R] <path>
- ファイルのレプリケーション要素を変更します。-Rを付ける事で再帰的に変更が出来ます。
- このコマンドは実行結果が確認できていません。
$ hdfs -setrep -w 3 -R hdfs://localhost/user/yuta/parent/childstat
$ hdfs -stat hdfs://localhost/user/yuta/madmagi_in/ 2012-04-08 00:54:03tail
$ 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.txttext
$ 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
$ 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
- 41 https://www.google.co.jp/
- 33 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=19&ved=0CG8QFjAIOAo&url=http://d.hatena.ne.jp/yutakikuchi/20120409/1333934476&ei=v_3OT9LZLYjamAWM38CKCg&usg=AFQjCNH77-aXc3lAotS23CwTMV316Y4wOw
- 24 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&ved=0CHAQFjAC&url=http://d.hatena.ne.jp/yutakikuchi/20120409/1333934476&ei=-TmnT-XdNMuOmQWOw9DhBA&usg=AFQjCNH77-aXc3lAotS23CwTMV316Y4wOw&sig2=XlslEXnh6M79OLuljkzF1w
- 18 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=0CDwQFjAB&url=http://d.hatena.ne.jp/yutakikuchi/20120409/1333934476&ei=t6GCT4eWIY2amQXJrJDlBw&usg=AFQjCNH77-aXc3lAotS23CwTMV316Y4wOw
- 13 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&ved=0CFsQFjAE&url=http://d.hatena.ne.jp/yutakikuchi/20120409/1333934476&ei=9HCaT8HJIeOgmQX9tpSYDg&usg=AFQjCNH77-aXc3lAotS23CwTMV316Y4wOw
- 12 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&ved=0CGcQFjAD&url=http://d.hatena.ne.jp/yutakikuchi/20120409/1333934476&ei=ycbnT_KXDJGhiAebzu1Y&usg=AFQjCNH77-aXc3lAotS23CwTMV316Y4wOw&sig2=eDXHAYi5fJZzsic9znCjSQ
- 6 http://www.google.co.jp/url?sa=t&rct=j&q=hdfs+ファイルコピー 空白&source=web&cd=3&ved=0CFUQFjAC&url=http://d.hatena.ne.jp/yutakikuchi/20120409/1333934476&ei=XWEWUKbtBYeeiAf
- 5 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&frm=1&source=web&cd=8&ved=0CIkBEBYwBw&url=http://d.hatena.ne.jp/yutakikuchi/20120409/1333934476&ei=An7FT723JIjcmAXR1ZXdBQ&usg=AFQjCNH77-aXc3lAotS23CwTMV316Y4wOw&sig2=4GcGN6QzzmTMpA95XxKRjg
- 5 http://www.google.co.jp/url?sa=t&rct=j&q=hdfs コマンド ディレクトリ&source=web&cd=4&ved=0CF0QFjAD&url=http://d.hatena.ne.jp/yutakikuchi/20120409/1333934476&ei=d0e8
- 4 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=6&ved=0CFAQFjAF&url=http://d.hatena.ne.jp/yutakikuchi/20120409/1333934476&ei=5siMT4uWIonJmQXg582HDA&usg=AFQjCNH77-aXc3lAotS23CwTMV316Y4wOw&sig2=6gjzfbIadY1dlIZGBfXWVA



