Hadoop Shell命令大全

hadoop支持命令行操作HDFS文件系统,并且支持shell-like命令与HDFS文件系统交互,对于大多数程序猿/媛来说,shell-like命令行操作都是比较熟悉的,其实这也是Hadoop的极大便利之一,至少对于想熟悉乃至尽快熟练操作HDFS的人来说。

由于平时在工作中经常用到Hadoop Shell命令来操作HDFS上的文件,有时候因为Hadoop Shell命令不熟悉,需要重新查找;或者需要某个命令的参数;再或者需要知晓相似命令的差异;于是便有了本文,对于Hadoop Shell命令的总结,方便自己以后得工作,也算是对类似工作的总结。

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

下面详细解释一下命令的操作,这些命令和Unix下的命令差不多,有些看了就明白其含义了。

hadoop fs -ls <path>

返回文件path的统计信息,信息包括:
permissions number_of_replicas userid groupid filesize modification_date modification_time filename

hadoop fs -lsr <path>

这个是ls的recursive版本,类似于ls -R命令和ls的区别。

hadoop fs -du URI

显示文件的大小。

hadoop fs -dus URI

类似于du -s:显示文件及其子目录的总体大小。

hadoop fs -df <path>

显示Hadoop所使用的文件系统的大小。

hadoop fs -count [-q] <path>

显示path 下的目录数量,文件大小等信息。默认情况下显示一下信息:
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

hadoop fs -mv <src> <dst>

从src移动到dst,允许多个源移动到同一个dst,dst必须是目录。

hadoop fs -cp <src> ... <dst>

拷贝多个源到dst,限制是dst必须是目录。

hadoop fs -rm [-skipTrash] <path>

删除文件,不能删除目录
-skipTrash:直接删除文件,不再往.Trash里面放。

hadoop fs -rmr [-skipTrash] <path>

这个能够迭代删除目录及其下面的文件
-skipTrash:直接删除文件,不再往.Trash里面放。

hadoop fs -expunge

清空Trash,Trash的概念详见http://hadoop.apache.org/docs/r1.0.4/hdfs_design.html,参考下Trash的含义

When a file is deleted by a user or an application, it is not immediately removed from HDFS. Instead, HDFS first renames it to a file in the /trash directory. The file can be restored quickly as long as it remains in /trash. A file remains in /trash for a configurable amount of time. After the expiry of its life in /trash, the NameNode deletes the file from the HDFS namespace. The deletion of a file causes the blocks associated with the file to be freed. Note that there could be an appreciable time delay between the time a file is deleted by a user and the time of the corresponding increase in free space in HDFS.

A user can Undelete a file after deleting it as long as it remains in the /trash directory. If a user wants to undelete a file that he/she has deleted, he/she can navigate the /trash directory and retrieve the file. The /trash directory contains only the latest copy of the file that was deleted. The /trash directory is just like any other directory with one special feature: HDFS applies specified policies to automatically delete files from this directory. The current default policy is to delete files from /trash that are more than 6 hours old. In the future, this policy will be configurable through a well defined interface.

文件传输:

hadoop fs -put <localsrc> ... <dst>

复制单个、或者多个本地FS的目录或者文件到目标文件系统。

hadoop fs -copyFromLocal <localsrc> ... <dst>

和put命令类似,唯一的限制是src必须是本地的文件。

hadoop fs -moveFromLocal <localsrc> ... <dst>

和put命令类似,这个命令会在localsrc执行完成后,删除本地的文件,注意是删除本地的文件。

hadoop fs -get [-ignoreCrc] <localsrc> ... <dst>

拷贝FS上的src到本地的dst目录
-ignoreCrc:拷贝时忽略crc检验,如果想拷贝CRCs,需要添加-crc参数。

hadoop fs -getmerge <src> <localdst> [addnl]

src是源目录,localdst是本地目标文件,将源目录的所有文件连接成本地目标文件。addnl是可选的,用于指定在每个文件结尾添加一个换行符。

hadoop fs -cat <src>

将src的内容输出到stdout,和Unix下面的cat功能类似。

hadoop fs -text <src>

把src文件按照text输出,zip或者TextRecordInputStream格式的也可以按照text输出。

hadoop fs -copyToLocal [-ignoreCrc] [-crc] <src> <localdst>

和get命令类似,唯一的限制是dst必须是本地文件系统的文件。

hadoop fs -moveToLocal [-crc] <src> <localdst>

输出:'-moveToLocal' is not implemented yet,还没有实现。

hadoop fs -mkdir <path>

创建path文件夹,如果path的父目录不存在,会迭代创建,类似于mkdir -p命令。

hadoop fs -setrep [-R] <rep> <path/file>

修改HDFS文件或者目录的Replication数目,对于重要的文件,需要增加副本数目,确保不会丢失或者损坏。
-R参数表示迭代更新,目录下面的副本数目同时更新。

hadoop fs -touchz <path>

创建一个大小为0的文件。

hadoop fs -test -[ezd] <path>

测试文件的目录属性,-e:测试文件是否存在;-z:文件大小是否为0;-d:测试是否是目录。

hadoop fs -stat [format] <path>

返回目录的统计信息。

hadoop fs -tail [-f] <file>

显示文件的最后1kb信息,-f参数和Unix下面的参数一样。

hadoop fs -chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...
hadoop fs -chown [-R] [OWNER][:[GROUP]] PATH...
hadoop fs -chgrp [-R] GROUP PATH...

这三个是权限操作命令,和Unix命令下的功能类似。

Hadoop Shell命令还是比较简单的,只是其中的差别必须等到用的时候才能体会,本文只是做个备忘,将工作中常用的命令整理下。

转自:http://isilic.iteye.com/blog/1770036

上一篇:hadoop 常见 命令


下一篇:Gradle学习笔记(1)创建简单的Java项目