hdfs 命令学习笔记
HDFS shell guide文档地址:
http://hadoop.apache.org/docs/r1.0.4/cn/hdfs_shell.html
- 入门
- 帮助相关命令
- 查看相关命令
- 文件及目录相关命令
- 统计相关命令
- 快照命令
入门
完成之前章节操作后,可以开始学习hdfs命令啦,输入 hadoop fs 。
[root@slave1 桌面]# hadoop fs
Usage: hadoop fs [generic options]
[-appendToFile <localsrc> ... <dst>]
[-cat [-ignoreCrc] <src> ...]
[-checksum <src> ...]
[-chgrp [-R] GROUP PATH...]
[-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...]
[-chown [-R] [OWNER][:[GROUP]] PATH...]
[-copyFromLocal [-f] [-p] [-l] [-d] <localsrc> ... <dst>]
[-copyToLocal [-f] [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
[-count [-q] [-h] [-v] [-t [<storage type>]] [-u] [-x] <path> ...]
[-cp [-f] [-p | -p[topax]] [-d] <src> ... <dst>]
[-createSnapshot <snapshotDir> [<snapshotName>]]
[-deleteSnapshot <snapshotDir> <snapshotName>]
[-df [-h] [<path> ...]]
[-du [-s] [-h] [-x] <path> ...]
[-expunge]
[-find <path> ... <expression> ...]
[-get [-f] [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
[-getfacl [-R] <path>]
[-getfattr [-R] {-n name | -d} [-e en] <path>]
[-getmerge [-nl] [-skip-empty-file] <src> <localdst>]
[-help [cmd ...]]
[-ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [<path> ...]]
[-mkdir [-p] <path> ...]
[-moveFromLocal <localsrc> ... <dst>]
[-moveToLocal <src> <localdst>]
[-mv <src> ... <dst>]
[-put [-f] [-p] [-l] [-d] <localsrc> ... <dst>]
[-renameSnapshot <snapshotDir> <oldName> <newName>]
[-rm [-f] [-r|-R] [-skipTrash] [-safely] <src> ...]
[-rmdir [--ignore-fail-on-non-empty] <dir> ...]
[-setfacl [-R] [{-b|-k} {-m|-x <acl_spec>} <path>]|[--set <acl_spec> <path>]]
[-setfattr {-n name [-v value] | -x name} <path>]
[-setrep [-R] [-w] <rep> <path> ...]
[-stat [format] <path> ...]
[-tail [-f] <file>]
[-test -[defsz] <path>]
[-text [-ignoreCrc] <src> ...]
[-touchz <path> ...]
[-truncate [-w] <length> <path> ...]
[-usage [cmd ...]]
Generic options supported are
-conf <configuration file> specify an application configuration file
-D <property=value> use value for given property
-fs <file:///|hdfs://namenode:port> specify default filesystem URL to use, overrides 'fs.defaultFS' property from configurations.
-jt <local|resourcemanager:port> specify a ResourceManager
-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
command [genericOptions] [commandOptions]
查询命令帮助指南
- usage :查看命令用法,如查看 ls 的。
[root@slave1 桌面]# hadoop fs -usage ls
Usage: hadoop fs [generic options] -ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [<path> ...]
- help :查看命令的详细帮助,如查看ls命令的帮助。
[root@slave1 桌面]# hadoop fs -help ls
-ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [<path> ...] :
List the contents that match the specified file pattern. If path is not
specified, the contents of /user/<currentUser> will be listed. For a directory a
list of its direct children is returned (unless -d option is specified).
Directory entries are of the form:
permissions - userId groupId sizeOfDirectory(in bytes)
modificationDate(yyyy-MM-dd HH:mm) directoryName
and file entries are of the form:
permissions numberOfReplicas userId groupId sizeOfFile(in bytes)
modificationDate(yyyy-MM-dd HH:mm) fileName
-C Display the paths of files and directories only.
-d Directories are listed as plain files.
-h Formats the sizes of files in a human-readable fashion
rather than a number of bytes.
-q Print ? instead of non-printable characters.
-R Recursively list the contents of directories.
-t Sort files by modification time (most recent first).
-S Sort files by size.
-r Reverse the order of the sort.
-u Use time of last access instead of modification for
display and sorting.
查看相关命令
- ls : 查看文件或目录,下例中:hdfs://localhost:9000是fs.defaultFS配置的值,hdfs://localhost:9000/即表示HDFS文件系统中根目录,如果使用的是HDFS文件系统, 可以简写为/。
[root@slave1 桌面]# hadoop fs -ls hdfs://192.168.248.132:9000/
Found 1 items
drwxr-xr-x - root supergroup 0 2017-05-26 16:06 hdfs://192.168.248.132:9000/user
[root@slave1 桌面]# hadoop fs -ls /
Found 1 items
drwxr-xr-x - root supergroup 0 2017-05-26 16:06 /user
选项-R:连同子目录的文件一起列出(我的目录里面没有子目录),例:
[root@slave1 桌面]# hadoop fs -ls -R /
-rw-r--r-- 2 root supergroup 14 2017-05-26 17:26 /input.txt
drwxr-xr-x - root supergroup 0 2017-05-26 16:06 /user
- cat :显示文件内容
[root@slave1 tmp]# hadoop fs -cat /input.txt
hello hadoop!
hello hadoop!
- text:将给定的文件以文本的格式输出,允许的格式zip、TextRecordInputStream、Avro。当文件为文本文件时,等同于cat。例:
[root@slave1 tmp]# hadoop fs -text /input.txt
hello hadoop!
hello hadoop!
- checksum:显示文件的校验和信息。因为需要和存储文件每个块的datanode互相通信,因此对大量的文件使用此命令效率可能会低
[root@slave1 tmp]# hadoop fs -checksum /input.zip
/input.zip MD5-of-0MD5-of-0CRC32 00000000000000000000000070bc8f4b72a86921468bf8e8441dce51
文件及目录相关命令
- touchz : 创建一个空文件,如果存在指定名称的非空文件,则返回错误。
[root@slave1 tmp]# hadoop fs -touchz /input.zip
[root@slave1 tmp]# hadoop fs -touchz /input
[root@slave1 tmp]# hadoop fs -ls /
Found 2 items
-rw-r--r-- 2 root supergroup 0 2017-05-27 10:09 /input
-rw-r--r-- 2 root supergroup 0 2017-05-27 10:00 /input.zip
- appendToFile: 向现有文件中追加内容。
[root@slave1 tmp]# hadoop fs -touchz /input.txt --创建一个input.txt文件
[root@slave1 tmp]# hadoop fs -text /input.txt --查看创建的input.txt的内容
[root@slave1 tmp]# hadoop fs -appendToFile ~/text/input.txt /input.txt -- 从本地文件读取内容,并且将内容添加到之前创建的文件中。
[root@slave1 tmp]# hadoop fs -text /input.txt
hello hadoop!
[root@slave1 tmp]# hadoop fs -appendToFile /test/input.txt /input.txt
[root@slave1 tmp]# hadoop fs -text /input.txt
hello hadoop!
hello hadoop!
- put: 从本地上传文件。(小编本来想一将txt传到制定文件夹,但是不知道为什么,/input识别出来不是文件夹,是文件,并且作为文件,往里面填写内容)
[hadoop@slave1 tmp]$ hadoop fs -put ~/Desktop/input1.txt /
[hadoop@slave1 tmp]$ hadoop fs -text /input1.txt --查看上传后的文件内容
hello hadoop!
选项-f:如果文件已经存在,覆盖已有文件
[hadoop@slave1 tmp]$ hadoop fs -put ~/Desktop/input1.txt /
put: /input1.txt': File exists --文件已存在时给出错误提示
[hadoop@slave1 tmp]$ hadoop fs -put -f ~/Desktop/input1.txt / --使用-f选项后没有再报错
选项-p:保留原文件的访问、修改时间,用户和组,权限属性
[root@slave1 tmp]# ll /test/input1.txt
-rw-r--r--. 1 root root 14 5月 26 17:19 /test/input1.txt
[root@slave1 tmp]# chmod 777 /test/input1.txt
[root@slave1 tmp]# hadoop fs -put -f /test/input1.txt /
[root@slave1 tmp]# hadoop fs -ls /input1.txt
-rw-r--r-- 2 root supergroup 14 2017-05-27 13:24 /input1.txt
[root@slave1 tmp]# hadoop fs -put -f -p /test/input1.txt /
[root@slave1 tmp]# hadoop fs -ls /input1.txt
-rwxrwxrwx 2 root root 14 2017-05-26 17:19 /input1.txt
- get: 从HDFS上下载文件到本地,与put不同,没有覆盖本地已有文件的选项。
[root@slave1 tmp]# hadoop fs -get /input1.txt /test
get: `/test/input1.txt': File exists
[root@slave1 tmp]# hadoop fs -get /input1.txt /test/input2.txt
[root@slave1 tmp]# cat /test/input2.txt
hello hadoop!
- getmerge: 将指定的HDFS中原目录下的文件合并成一个文件并下载到本地,源文件保留。
- 选项-nl:在每个文件的最后增加一个新行
[root@slave1 tmp]# hadoop fs -ls /test
Found 2 items
-rw-r--r-- 2 root supergroup 14 2017-05-27 13:51 /test/test.txt
-rw-r--r-- 2 root supergroup 18 2017-05-27 13:54 /test/test2.txt
[root@slave1 tmp]# hadoop fs -getmerge -nl /test /test/merge.txt
[root@slave1 tmp]# cat /test/merge.txt
hello hadoop!
welcome to hadoop
copyFromLocal: 从本地文件系统上传文件到HDFS,与put命令相同。
copyToLocal: 从HDFS下载文件到本地文件系统,与get命令相同。
moveFromLocal: 与put命令相同,只是上传成功后本地文件会被删除。
mv: 同linux的mv命令,移动或重命名文件。
[root@slave1 tmp]# hadoop fs -ls
ls: `.': No such file or directory
[root@slave1 tmp]# hadoop fs -ls /
Found 6 items
-rw-r--r-- 2 root supergroup 14 2017-05-27 11:16 /input
-rw-r--r-- 2 root supergroup 28 2017-05-27 10:15 /input.txt
**-rw-r--r-- 2 root supergroup 0 2017-05-27 10:00 /input.zip**
-rw-r--r-- 2 root supergroup 14 2017-05-27 11:10 /input1
-rwxrwxrwx 2 root root 14 2017-05-26 17:19 /input1.txt
drwxr-xr-x - root supergroup 0 2017-05-27 13:54 /test
[root@slave1 tmp]# hadoop fs -mv /input.zip /input1.zip
[root@slave1 tmp]# hadoop fs -ls /
Found 6 items
-rw-r--r-- 2 root supergroup 14 2017-05-27 11:16 /input
-rw-r--r-- 2 root supergroup 28 2017-05-27 10:15 /input.txt
-rw-r--r-- 2 root supergroup 14 2017-05-27 11:10 /input1
-rwxrwxrwx 2 root root 14 2017-05-26 17:19 /input1.txt
**-rw-r--r-- 2 root supergroup 0 2017-05-27 10:00 /input1.zip**
drwxr-xr-x - root supergroup 0 2017-05-27 13:54 /test
[root@slave1 tmp]# hadoop fs -mv /input1.zip /input
mv: `/input': File exists
[root@slave1 tmp]# hadoop fs -mv /input1.zip /input1
mv: `/input1': File exists
[root@slave1 tmp]# hadoop fs -mv /input1.zip /test
[root@slave1 tmp]# hadoop fs -ls -R /
-rw-r--r-- 2 root supergroup 14 2017-05-27 11:16 /input
-rw-r--r-- 2 root supergroup 28 2017-05-27 10:15 /input.txt
-rw-r--r-- 2 root supergroup 14 2017-05-27 11:10 /input1
-rwxrwxrwx 2 root root 14 2017-05-26 17:19 /input1.txt
drwxr-xr-x - root supergroup 0 2017-05-27 14:42 /test
**-rw-r--r-- 2 root supergroup 0 2017-05-27 10:00 /test/input1.zip**
-rw-r--r-- 2 root supergroup 14 2017-05-27 13:51 /test/test.txt
-rw-r--r-- 2 root supergroup 18 2017-05-27 13:54 /test/test2.txt
- cp:复制
选项-f:如果文件已存在,覆盖已有文件
mkdir:创建文件夹
[root@slave1 tmp]# hadoop fs -mkdir /input3
[root@slave1 tmp]# hadoop fs -ls /
Found 6 items
-rw-r--r-- 2 root supergroup 14 2017-05-27 11:16 /input
-rw-r--r-- 2 root supergroup 28 2017-05-27 10:15 /input.txt
-rw-r--r-- 2 root supergroup 14 2017-05-27 11:10 /input1
-rwxrwxrwx 2 root root 14 2017-05-26 17:19 /input1.txt
drwxr-xr-x - root supergroup 0 2017-05-27 14:54 /input3
drwxr-xr-x - root supergroup 0 2017-05-27 14:42 /test
选项-p:如果上层目录不存在,递归建立所需目录
[root@slave1 tmp]# hadoop fs -mkdir -p /input4/input4_1/input4_1_1
[root@slave1 tmp]# hadoop fs -ls -R /
-rw-r--r-- 2 root supergroup 14 2017-05-27 11:16 /input
-rw-r--r-- 2 root supergroup 28 2017-05-27 10:15 /input.txt
-rw-r--r-- 2 root supergroup 14 2017-05-27 11:10 /input1
-rwxrwxrwx 2 root root 14 2017-05-26 17:19 /input1.txt
drwxr-xr-x - root supergroup 0 2017-05-27 14:54 /input3
drwxr-xr-x - root supergroup 0 2017-05-27 14:56 /input4
drwxr-xr-x - root supergroup 0 2017-05-27 14:56 /input4/input4_1
drwxr-xr-x - root supergroup 0 2017-05-27 14:56 /input4/input4_1/input4_1_1
drwxr-xr-x - root supergroup 0 2017-05-27 14:42 /test
-rw-r--r-- 2 root supergroup 0 2017-05-27 10:00 /test/input1.zip
-rw-r--r-- 2 root supergroup 14 2017-05-27 13:51 /test/test.txt
-rw-r--r-- 2 root supergroup 18 2017-05-27 13:54 /test/test2.txt
- rm:删除文件
[root@slave1 tmp]# hadoop fs -rm /input
Deleted /input
选项-r:递归的删除,可以删除非空目录
[root@slave1 tmp]# hadoop fs -rm /input4
rm: `/input4': Is a directory -- 这里提示文件夹不能删
[root@slave1 tmp]# hadoop fs -rm -r /input4
Deleted /input4
- rmdir: 删除空目录
[root@slave1 tmp]# hadoop fs -ls -R /
-rw-r--r-- 2 root supergroup 28 2017-05-27 10:15 /input.txt
-rw-r--r-- 2 root supergroup 14 2017-05-27 11:10 /input1
-rwxrwxrwx 2 root root 14 2017-05-26 17:19 /input1.txt
drwxr-xr-x - root supergroup 0 2017-05-27 14:54 /input3
drwxr-xr-x - root supergroup 0 2017-05-27 14:42 /test
-rw-r--r-- 2 root supergroup 0 2017-05-27 10:00 /test/input1.zip
-rw-r--r-- 2 root supergroup 14 2017-05-27 13:51 /test/test.txt
-rw-r--r-- 2 root supergroup 18 2017-05-27 13:54 /test/test2.txt
[root@slave1 tmp]# hadoop fs -rmdir /test
rmdir: `/test': Directory is not empty --目录非空的话,会报错
选项–ignore-fail-on-non-empty:忽略非空删除失败时的提示。其实是没有删除的,但是也没有失败提醒。
[root@slave1 tmp]# hadoop fs -rmdir --ignore-fail-on-non-empty /test
[root@slave1 tmp]# hadoop fs -ls -R /
-rw-r--r-- 2 root supergroup 28 2017-05-27 10:15 /input.txt
-rw-r--r-- 2 root supergroup 14 2017-05-27 11:10 /input1
-rwxrwxrwx 2 root root 14 2017-05-26 17:19 /input1.txt
drwxr-xr-x - root supergroup 0 2017-05-27 14:54 /input3
drwxr-xr-x - root supergroup 0 2017-05-27 14:42 /test
-rw-r--r-- 2 root supergroup 0 2017-05-27 10:00 /test/input1.zip
-rw-r--r-- 2 root supergroup 14 2017-05-27 13:51 /test/test.txt
-rw-r--r-- 2 root supergroup 18 2017-05-27 13:54 /test/test2.txt
[root@slave1 tmp]#
- setrep: 改变一个文件的副本数
[root@slave1 tmp]# hadoop fs -stat %r /input1
2
[root@slave1 tmp]# hadoop fs -setrep 3 /input1
Replication 3 set: /input1
[root@slave1 tmp]# hadoop fs -stat %r /input1
3
选项-w:命令等待副本数调整完成
[root@slave1 tmp]# hadoop fs -setrep -w 1 /input1
Replication 1 set: /input1
Waiting for /input1 ... done
- setrep: 改变一个文件的副本数
- chgrp: 修改文件组。 选项-R:递归修,如果是目录,则递归的修改其下的文件及目录,更新到的文件组就算不存在,也能更新成功。
[root@slave1 tmp]# hadoop fs -ls -R /
-rw-r--r-- 2 root supergroup 28 2017-05-27 10:15 /input.txt
-rw-r--r-- 1 root supergroup 14 2017-05-27 11:10 /input1
-rwxrwxrwx 2 root root 14 2017-05-26 17:19 /input1.txt
drwxr-xr-x - root supergroup 0 2017-05-27 14:54 /input3
drwxr-xr-x - root supergroup 0 2017-05-27 14:42 /test
-rw-r--r-- 2 root supergroup 0 2017-05-27 10:00 /test/input1.zip
-rw-r--r-- 2 root supergroup 14 2017-05-27 13:51 /test/test.txt
-rw-r--r-- 2 root supergroup 18 2017-05-27 13:54 /test/test2.txt
[root@slave1 tmp]# hadoop fs -chgrp test /test/input1.zip
[root@slave1 tmp]# hadoop fs -ls -R /
-rw-r--r-- 2 root supergroup 28 2017-05-27 10:15 /input.txt
-rw-r--r-- 1 root supergroup 14 2017-05-27 11:10 /input1
-rwxrwxrwx 2 root root 14 2017-05-26 17:19 /input1.txt
drwxr-xr-x - root supergroup 0 2017-05-27 14:54 /input3
drwxr-xr-x - root supergroup 0 2017-05-27 14:42 /test
-rw-r--r-- 2 root test 0 2017-05-27 10:00 /test/input1.zip
-rw-r--r-- 2 root supergroup 14 2017-05-27 13:51 /test/test.txt
-rw-r--r-- 2 root supergroup 18 2017-05-27 13:54 /test/test2.txt
- setrep: 修改文件权限,权限模式同linux shell命令中的模式
[root@slave1 tmp]# hadoop fs -chmod 754 /input1
[root@slave1 tmp]# hadoop fs -ls -R /
-rw-r--r-- 2 root supergroup 28 2017-05-27 10:15 /input.txt
-rwxr-xr-- 1 root supergroup 14 2017-05-27 11:10 /input1
-rwxrwxrwx 2 root root 14 2017-05-26 17:19 /input1.txt
drwxr-xr-x - root supergroup 0 2017-05-27 14:54 /input3
drwxr-xr-x - root supergroup 0 2017-05-27 14:42 /test
-rw-r--r-- 2 root test 0 2017-05-27 10:00 /test/input1.zip
-rw-r--r-- 2 root supergroup 14 2017-05-27 13:51 /test/test.txt
-rw-r--r-- 2 root supergroup 18 2017-05-27 13:54 /test/test2.txt
- chown: 修改文件的用户或组,选项-R:递归修改,如果是目录,则递归的修改其下的文件及目录
[root@slave1 tmp]# hadoop fs -ls -R /
-rw-r--r-- 2 root supergroup 28 2017-05-27 10:15 /input.txt
-rwxr-xr-- 1 root supergroup 14 2017-05-27 11:10 /input1
-rwxrwxrwx 2 root root 14 2017-05-26 17:19 /input1.txt
drwxr-xr-x - root supergroup 0 2017-05-27 14:54 /input3
drwxr-xr-x - root supergroup 0 2017-05-27 14:42 /test
-rw-r--r-- 2 root test 0 2017-05-27 10:00 /test/input1.zip -- 原用户和组
-rw-r--r-- 2 root supergroup 14 2017-05-27 13:51 /test/test.txt
-rw-r--r-- 2 root supergroup 18 2017-05-27 13:54 /test/test2.txt
[root@slave1 tmp]# hadoop fs -chown test /test/input1.zip
[root@slave1 tmp]# hadoop fs -ls -R /
-rw-r--r-- 2 root supergroup 28 2017-05-27 10:15 /input.txt
-rwxr-xr-- 1 root supergroup 14 2017-05-27 11:10 /input1
-rwxrwxrwx 2 root root 14 2017-05-26 17:19 /input1.txt
drwxr-xr-x - root supergroup 0 2017-05-27 14:54 /input3
drwxr-xr-x - root supergroup 0 2017-05-27 14:42 /test
-rw-r--r-- 2 test test 0 2017-05-27 10:00 /test/input1.zip --向用户和组
-rw-r--r-- 2 root supergroup 14 2017-05-27 13:51 /test/test.txt
-rw-r--r-- 2 root supergroup 18 2017-05-27 13:54 /test/test2.txt
- getfacl:访问控制列表,选项-R:递归显示
[root@slave1 tmp]# hadoop fs -getfacl /test/input1.zip
# file: /test/input1.zip
# owner: test
# group: test
user::rw-
group::r--
other::r--
[root@slave1 tmp]# hadoop fs -getfacl -R /test
# file: /test
# owner: root
# group: supergroup
user::rwx
group::r-x
other::r-x
# file: /test/input1.zip
# owner: test
# group: test
user::rw-
group::r--
other::r--
# file: /test/test.txt
# owner: root
# group: supergroup
user::rw-
group::r--
other::r--
# file: /test/test2.txt
# owner: root
# group: supergroup
user::rw-
group::r--
other::r--
- setfacl:设置访问控制列表,acls默认未开启,直接使用该命令会报错
[root@slave1 tmp]# hadoop fs -setfacl -b /test/test.txt
setfacl: The ACL operation has been rejected. Support for ACLs has been disabled by setting dfs.namenode.acls.enabled to false.
开启acls,配置hdfs-site.xml
[hadoop@localhost hadoop-2.5.2]$ vi etc/hadoop/hdfs-site.xml
<property>
<name>dfs.namenode.acls.enabled</name>
<value>true</value>
</property>
选项-m:修改acls
[root@slave1 tmp]# hadoop fs -getfacl /test/test.txt
# file: /test/test.txt
# owner: root
# group: supergroup
user::rw-
group::r--
other::r--
[root@slave1 tmp]# hadoop fs -setfacl -m user::rw-,user:hadoop:rw-,group::r--,other::r-- /test/test.txt
[root@slave1 tmp]# hadoop fs -getfacl /test/test.txt
# file: /test/test.txt
# owner: root
# group: supergroup
user::rw-
user:hadoop:rw-
group::r--
mask::rw-
other::r--
选项-x:删除指定规则
[root@slave1 tmp]# hadoop fs -getfacl /test/test.txt
# file: /test/test.txt
# owner: root
# group: supergroup
user::rw-
user:hadoop:rw-
group::r--
mask::rw-
other::r--
[root@slave1 tmp]# hadoop fs -setfacl -x user:hadoop /test/test.txt
[root@slave1 tmp]# hadoop fs -getfacl /test/test.txt
# file: /test/test.txt
# owner: root
# group: supergroup
user::rw-
group::r--
mask::r--
other::r--
- setfattr:设置扩展属性的名称和值,
选项-n:属性名称 选项-v:属性值
[root@slave1 tmp]# hadoop fs -getfattr -d /test/input.zip
getfattr: `/test/input.zip': No such file or directory
[root@slave1 tmp]# hadoop fs -getfattr -d /test/input1.zip
# file: /test/input1.zip
[root@slave1 tmp]# hadoop fs -setfattr -n user.web -v www.baidu.com /test/input1.zip
[root@slave1 tmp]# hadoop fs -getfattr -d /test/input1.zip# file: /test/input1.zip
user.web="www.baidu.com"
选项-x:删除扩展属性
[root@slave1 tmp]# hadoop fs -getfattr -d /test/input1.zip# file: /test/input1.zip
user.web="www.baidu.com"
[root@slave1 tmp]# hadoop fs -setfattr -x user.web /test/input1.zip
[root@slave1 tmp]# hadoop fs -getfattr -d /test/input1.zip
# file: /test/input1.zip
- getfattr:设置扩展属性的名称和值,选项-n:显示指定名称的属性值.
[hadoop@localhost hadoop-2.5.2]$ hadoop fs -getfattr -n user.web /input.zip
# file: /input.zip
user.web="www.baidu.com"
统计相关命令
- count:显示指定文件或目录的:DIR_COUNT、FILE_COUNT、CONTENT_SIZE、 FILE_NAME,分别表示:子目录个数(如果指定路径是目录,则包含该目录本身)、文件个数、使用字节个数,以及文件或目录名。
[root@slave1 tmp]# hadoop fs -count /
3 6 88 /
选项-q:显示配额信息(在多人共用的情况下,可以通过限制用户写入目录,并设置目录的quota ,防止不小心就把所有的空间用完造成别人无法存取的情况)。配额信息包括:QUOTA、REMAINING_QUOTA、SPACE_QUOTA、REMAINING_SPACE_QUOTA,分别表示某个目录下档案及目录的总数、剩余目录或文档数量、目录下空间的大小、目录下剩余空间。
计算公式:
QUOTA – (DIR_COUNT + FILE_COUNT) = REMAINING_QUOTA;
SPACE_QUOTA – CONTENT_SIZE = REMAINING_SPACE_QUOTA。
none和inf表示未配置。
[root@slave1 tmp]# hadoop fs -count -q /
9223372036854775807 9223372036854775798 none inf 3 6 88 /
- du:显示文件大小,如果指定目录,会显示该目录中每个文件的大小。
[root@slave1 tmp]# hadoop fs -ls -R /
-rw-r--r-- 2 root supergroup 28 2017-05-27 10:15 /input.txt
-rwxr-xr-- 1 root supergroup 14 2017-05-27 11:10 /input1
-rwxrwxrwx 2 root root 14 2017-05-26 17:19 /input1.txt
drwxr-xr-x - root supergroup 0 2017-05-27 14:54 /input3
drwxr-xr-x - root supergroup 0 2017-05-27 14:42 /test
-rw-r--r-- 2 test test 0 2017-05-27 10:00 /test/input1.zip
-rw-r--r--+ 2 root supergroup 14 2017-05-27 13:51 /test/test.txt
-rw-r--r-- 2 root supergroup 18 2017-05-27 13:54 /test/test2.txt
[root@slave1 tmp]# hadoop fs -du /
28 /input.txt
14 /input1
14 /input1.txt
0 /input3
32 /test
选项-s:显示总的统计信息,而不是显示每个文件的信息
[root@slave1 tmp]# hadoop fs -du -s /
88 /
- df: 检查文件系统的磁盘空间占用情况
[root@slave1 tmp]# hadoop fs -df /
Filesystem Size Used Available Use%
hdfs://192.168.248.132:9000 18788384768 57344 9179619328 0%
- stat: 显示文件统计信息。
格式: %b - 文件所占的块数; %g - 文件所属的用户组 ;%n - 文件名; %o - 文件块大小;%r - 备份数 ;%u - 文件所属用户;%y - 文件修改时间
[root@slave1 tmp]# hadoop fs -stat %b,%g,%n,%o,%r,%u,%y /test/input1.zip
0,test,input1.zip,134217728,2,test,2017-05-27 02:00:54
快照命令
Hdfs的快照原理
Hdfs的快照(snapshot)是在某一时间点对指定文件系统拷贝,快照采用只读模式,可以对重要数据进行恢复、防止用户错误性的操作。
快照分两种:
# 一种是:建立文件系统的索引,每次更新文件不会真正的改变文件,而是新开辟一个空间用来保存更改的文件,
# 一种是:拷贝所有的文件系统。Hdfs属于前者。 Hdfs的快照的特征如下:
1. 快照的创建是瞬间的,代价为O(1),取决于子节点扫描文件目录的时间。
2. 当且仅当做快照的文件目录下有文件更新时才会占用小部分内存,占用内存的大小为
O(M),其中M为更改文件或者目录的数量;
3. 新建快照的时候,Datanode中的block不会被复制,快照中只是记录了文件块的列表和
大小信息。
4. 快照不会影响正常的hdfs的操作。对做快照之后的数据进行的更改将会按照时间顺序逆
序的记录下来,用户访问的还是当前最新的数据,快照里的内容为快照创建的时间点时文件的内容减去当前文件的内容。
每个快照最高限额为65536个文件或者文件夹,在快照的子文件夹中不允许在创建新的快照。
注意,mv命令和del命令是不允许的,因为快照是只读的
参考:http://blog.csdn.net/linlinv3/article/details/44564313
http://blog.csdn.net/androidlushangderen/article/details/51282612
- createSnapshot: 创建快照
[root@slave1 tmp]# hadoop fs -ls -R /test
-rw-r--r-- 2 test test 0 2017-05-27 10:00 /test/input1.zip
-rw-r--r--+ 2 root supergroup 14 2017-05-27 13:51 /test/test.txt
-rw-r--r-- 2 root supergroup 18 2017-05-27 13:54 /test/test2.txt
[root@slave1 tmp]# hadoop fs -createSnapshot /test t1
createSnapshot: Directory is not a snapshottable directory: /test -- 直接创建的话,会报错
[root@slave1 tmp]# hdfs dfsadmin -allowSnapshot /test -- 需要开启某一目录的快照功能
Allowing snaphot on /test succeeded
[root@slave1 tmp]# hadoop fs -createSnapshot /test t1 -- 创建快照
Created snapshot /test/.snapshot/t1
[root@slave1 tmp]# hadoop fs -ls -R /test
-rw-r--r-- 2 test test 0 2017-05-27 10:00 /test/input1.zip
-rw-r--r--+ 2 root supergroup 14 2017-05-27 13:51 /test/test.txt
-rw-r--r-- 2 root supergroup 18 2017-05-27 13:54 /test/test2.txt
[root@slave1 tmp]# hadoop fs -ls /test/.snapshot/t1 --查看快照
Found 3 items
-rw-r--r-- 2 test test 0 2017-05-27 10:00 /test/.snapshot/t1/input1.zip
-rw-r--r--+ 2 root supergroup 14 2017-05-27 13:51 /test/.snapshot/t1/test.txt
-rw-r--r-- 2 root supergroup 18 2017-05-27 13:54 /test/.snapshot/t1/test2.txt
- renameSnapshot: 重命名快照
[root@slave1 tmp]# hadoop fs -ls /test/.snapshot/t1
Found 3 items
-rw-r--r-- 2 test test 0 2017-05-27 10:00 /test/.snapshot/t1/input1.zip
-rw-r--r--+ 2 root supergroup 14 2017-05-27 13:51 /test/.snapshot/t1/test.txt
-rw-r--r-- 2 root supergroup 18 2017-05-27 13:54 /test/.snapshot/t1/test2.txt
[root@slave1 tmp]# hadoop fs -ls /test/.snapshot/t1
Found 3 items
-rw-r--r-- 2 test test 0 2017-05-27 10:00 /test/.snapshot/t1/input1.zip
-rw-r--r--+ 2 root supergroup 14 2017-05-27 13:51 /test/.snapshot/t1/test.txt
-rw-r--r-- 2 root supergroup 18 2017-05-27 13:54 /test/.snapshot/t1/test2.txt
[root@slave1 tmp]# hadoop fs -renameSnapshot /test/ t1 t2
[root@slave1 tmp]# hadoop fs -ls /test/.snapshot/t2
Found 3 items
-rw-r--r-- 2 test test 0 2017-05-27 10:00 /test/.snapshot/t2/input1.zip
-rw-r--r--+ 2 root supergroup 14 2017-05-27 13:51 /test/.snapshot/t2/test.txt
-rw-r--r-- 2 root supergroup 18 2017-05-27 13:54 /test/.snapshot/t2/test2.txt
- deleteSnapshot: 删除快照
[root@slave1 tmp]# hadoop fs -ls /test/.snapshot/t2
Found 3 items
-rw-r--r-- 2 test test 0 2017-05-27 10:00 /test/.snapshot/t2/input1.zip
-rw-r--r--+ 2 root supergroup 14 2017-05-27 13:51 /test/.snapshot/t2/test.txt
-rw-r--r-- 2 root supergroup 18 2017-05-27 13:54 /test/.snapshot/t2/test2.txt
[root@slave1 tmp]# hadoop fs -deleteSnapshot /test/ t2
[root@slave1 tmp]# hadoop fs -ls /test/.snapshot/t2
ls: `/test/.snapshot/t2': No such file or directory
参考:http://www.cnblogs.com/hzh19870110/p/6802596.html