摘要:本篇教程探讨了大数据技术 HDFS常用命令,希望阅读本篇文章以后大家有所收获,帮助大家对大数据技术的理解更加深入。
本篇教程探讨了大数据技术 HDFS常用命令,希望阅读本篇文章以后大家有所收获,帮助大家对大数据技术的理解更加深入。
<
注意:
以下指令均是在,Linux 命令行窗口界面操作。
本教程基于Hadoop伪分布式安装下编写测试。
[ ]表示可选参数,<>表示必须参数。
开始使用命令前,必须启动Hadoop
(1)-appendToFile
用法: hadoop fs -appendToFile
作用:是将一个或者多个文件添加到HDFS系统中。
示例:
?1234hadoop fs -appendToFile localfile /user/hadoop/hadoopfilehadoop fs -appendToFile localfile1 localfile2 /user/hadoop/hadoopfilehadoop fs -appendToFile localfile hdfs://nn.example.com/hadoop/hadoopfilehadoop fs -appendToFile - hdfs://nn.example.com/hadoop/hadoopfile Reads the input from stdin.
(2)-cat
用法:hadoop fs -cat URI [URI ...]
作用:查看文件内容(可以查看本地和HDFS上的内容)。
示例:
?12hadoop fs -cat hdfs://nn1.example.com/file1 hdfs://nn2.example.com/file2hadoop fs -cat file:///file3 /user/hadoop/file4
(3)-checksum
用法: hadoop fs -checksum URI
作用:查看校验码信息。(例子显示了MD5)
示例:
?12hadoop fs -checksum hdfs://nn1.example.com/file1hadoop fs -checksum file:///etc/hosts
(4)-chgrp
用法: hadoop fs -chgrp [-R] GROUP URI [URI ...]
作用:改变文件所属的组。(Change group association of files.)
使用-R 将使改变在目录结构下递归进行。
(5)-chmod
作用:改变文件访问权限。
用法:hadoop fs -chmod [-R]
这里可以参考 Linux下文件系统的chmod的用法,基本类似。
(6)-chown
作用:hadoop fs -chown [-R] [OWNER][:[GROUP]] URI [URI ]
用法:改变文件的所有者。使用-R 将使改变在目录结构下递归进行。命令的使用者必须是超级用户。
(7)-copyFromLocal
用法:hadoop fs -copyFromLocal
作用:类似于put命令,和put不同的是,拷贝的源地址必须是本地文件地址。
-f 参数 当拷贝的目标文件存在时,进行覆盖。
示例:
?12[root@two1 fanrui]# hadoop fs -copyFromLocal testFlatMap.txt /1.txt copyFromLocal: `/1.txt': File exists
这个时候加上-f参数。即可覆盖。
?1[root@two1 fanrui]# hadoop fs -copyFromLocal -f testFlatMap.txt /1.txt
(8)-copyToLocal
用法: hadoop fs -copyToLocal [-ignorecrc] [-crc] URI
作用:类似于get指令。和get不同的是,拷贝的目的地址必须是本地文件地址。(9)-count
作用:计算paths下的目录数,文件数和字节数。
用法: hadoop fs -count [-q] [-h] [-v]
?1234hadoop fs -count hdfs://nn1.example.com/file1 hdfs://nn2.example.com/file2hadoop fs -count -q hdfs://nn1.example.com/file1hadoop fs -count -q -h hdfs://nn1.example.com/file1hdfs dfs -count -q -h -v hdfs://nn1.example.com/file1
(10)-cp用法:hadoop fs -cp [-f] [-p | -p[topax]] URI [URI ...]
The -p option will preserve file attributes [topx] (timestamps, ownership, permission, ACL, XAttr). If -p is specified with no arg, then preserves timestamps, ownership, permission. If -pa is specified, then preserves permission also because ACL is a super-set of permission. Determination of whether raw namespace extended attributes are preserved is independent of the -p flag.
示例:
?1[root@two1 fanrui]# hadoop fs -cp -p /tmp/fan /tmp/fan1
(11)-df用法:hadoop fs -df [-h] URI [URI ...]作用:显示剩余空间。示例:
?123[root@two1 fanrui]# hadoop fs -df /Filesystem Size Used Available Use%hdfs://localhost:9000 37626667008 311296 24792702976 0%
(12)-dus作用:显示文件长度概要。该方法已经被舍去,等价于 -du -s 方法。见(11)
(13)-expunge作用:从垃圾桶目录永久删除超过保留阈值的检查点中的文件,并创建新检查点。
用法:hadoop fs -expunge
(14)-find作用:查找满足表达式的文件和文件夹。没有配置path的话,默认的就是全部目录/;如果表达式没有配置,则默认为-print。用法: hadoop fs -find
示例:
?1hadoop fs -find / -name test -print
(15)-get作用:从HDFS上拷贝文件到本地。用法:hadoop fs -get [-ignorecrc] [-crc]
?12hadoop fs -get /user/hadoop/file localfilehadoop fs -get hdfs://nn.example.com/user/hadoop/file localfile
(16)getfacl作用:显示文件和文件夹的ACLs(Access Control Lists)。如果目录有默认的ACL,则显示之。-R参数:递归显示。用法:
hadoop fs -getfacl [-R]
Options:
-R: List the ACLs of all files and directories recursively.
path: File or directory to list.
示例:
?12hadoop fs -getfacl /filehadoop fs -getfacl -R /dir
Exit Code:
Returns 0 on success and non-zero on error.
(17)getfattr
作用:显示文件或目录的扩展属性名和值(如果有的话)
用法:hadoop fs -getfattr [-R] -n name | -d [-e en]
Options:
-R:递归显示文件夹和文件。-n name:转储命名的扩展属性值。-d:转储与路径名相关联的所有扩展属性值。-e en: 检索后的值进行编码。 有效的编码是 “text”, “hex”, and “base64”. 值编码作为文本字符串是用双引号括起来的(“),值编码作为16进制和64进制,前缀分别为 0x 和 0s。path:文件或文件夹路径。
示例:
?12hadoop fs -getfattr -d /filehadoop fs -getfattr -R -n user.myAttr /dir
(18)-getmerge作用:是将HDFS上一个目录中所有的文件合并到一起输出到一个本地文件上。用法:hadoop fs -getmerge [-nl]
示例:
?12hadoop fs -getmerge -nl /src /opt/output.txthadoop fs -getmerge -nl /src/file1.txt /src/file2.txt /output.txt
(19)-help作用:帮助文档
用法:hadoop fs -help
(20)-ls作用:查看文件,与linux下ls命令基本类似。用法:hadoop fs -ls [-d] [-h] [-R]
-R:递归展示,显示所有的文件夹及文件
示例:
?123hadoop fs -ls -d /hadoop fs -ls -h /hadoop fs -ls -R /
-lsr作用:已经被舍去,效果等同于-ls -R
(21)-mkdir作用:创建文件夹。
用法:hadoop fs -mkdir [-p]
?12hadoop fs -mkdir /user/hadoop/dir1 /user/hadoop/dir2hadoop fs -mkdir hdfs://nn1.example.com/user/hadoop/dir hdfs://nn2.example.com/user/hadoop/dir
(22)-moveFromLocal用法:hadoop fs -moveFromLocal
(23)-moveToLocal用法:hadoop fs -moveToLocal [-crc]
(24)-mv 用法:移动文件。作用: hadoop fs -mv URI [URI ...]
?12hadoop fs -mv /user/hadoop/file1 /user/hadoop/file2hadoop fs -mv hdfs://nn.example.com/file1 hdfs://nn.example.com/file2 hdfs://nn.example.com/file3 hdfs://nn.example.com/dir1
(25)-put用法: hadoop fs -put
示例:
?1234hadoop fs -put localfile /user/hadoop/hadoopfilehadoop fs -put localfile1 localfile2 /user/hadoop/hadoopdirhadoop fs -put localfile hdfs://nn.example.com/hadoop/hadoopfilehadoop fs -put - hdfs://nn.example.com/hadoop/hadoopfile Reads the input from stdin.
(26)-rm用法:hadoop fs -rm [-f] [-r |-R] [-skipTrash] URI [URI ...]作用:删除文件。选项:
The -f option will not display a diagnostic message or modify the exit status to reflect an error if the file does not exist.The -R option deletes the directory and any content under it recursively.The -r option is equivalent to -R.The -skipTrash option will bypass trash, if enabled, and delete the specified file(s) immediately. This can be useful when it is necessary to delete files from an over-quota directory.
示例:
?1hadoop fs -rm hdfs://nn.example.com/file /user/hadoop/emptydir
(27)-rmdir用法:hadoop fs -rmdir [--ignore-fail-on-non-empty] URI [URI ...]作用:删除空目录。选项: —ignore-fail-on-non-empty:使用它的时候,忽略因文件夹非空删除失败的信息。
(28)-rmr作用:该方法已经被舍去。和-rm -r效果一样。递归删除。
(29)-setfacl用法:hadoop fs -setfacl [-R] [-b |-k -m |-x
选项:
-b:移除所有除了基本的ACL条目。用户、组和其他的条目被保留为与权限位的兼容性。-k:删除默认的ACL。-R: 递归应用于所有文件和目录的操作。-m:修改ACL。新的项目添加到ACL,并保留现有的条目。-x:删除指定的ACL条目。其他保留ACL条目。–set:完全替换ACL,丢弃所有现有的条目。acl_spec必须包括用户,组,和其他有权限位的兼容性。acl_spec:逗号分隔的ACL条目列表。path:修改文件或目录。
示例:
?1234567hadoop fs -setfacl -m user:hadoop:rw- /filehadoop fs -setfacl -x user:hadoop /filehadoop fs -setfacl -b /filehadoop fs -setfacl -k /dirhadoop fs -setfacl --set user::rw-,user:hadoop:rw-,group::r--,other::r-- /filehadoop fs -setfacl -R -m user:hadoop:r-x /dirhadoop fs -setfacl -m default:user:hadoop:r-x /dir
(30)-setrep用法:hadoop fs -setrep [-R] [-w]
示例:
?1hadoop fs -setrep -w 3 /user/hadoop/dir1
(31)-stat用法: hadoop fs -stat [format]
(32)-tail用法:hadoop fs -tail [-f] URI作用:输出文件最后1kb的内容。选项: -f:和unix中tail -f命令类似,当文件内容更新时,输出将会改变,具有实时性。
示例:用一个场景测试下。首先HDFS的/目录下有文件mpwtest1.txt命令:hadoop fs -tail -f /mpwtest1.txt开启另外一个终端。输入命令: hadoop fs -appendToFile mpwtest2.txt /mpwtest1.txt可以发现 窗口1 有变化。
(33)-test
作用:判断文件信息
用法:hadoop fs -test -[defsz] URI
选项:
-d:如果路径是一个目录,返回0-e:如果路径已经存在,返回0-f: 如果路径是一个文件,返回0-s:如果路径不是空,返回0-z:如果文件长度为0,返回0URI:资源地址,可以是文件也可以是目录。
示例:
?1hadoop fs -test -e filename
(34)-text用法:hadoop fs -text
(35)-touchz用法: hadoop fs -touchz URI [URI ...]作用:创建一个空文件。示例:hadoop fs -touchz /hello.jar
(35)-truncate用法: hadoop fs -truncate [-w]
作用:截断指定长度匹配的所有文件内容。
选项:
-w:需要等待命令完成块恢复。如果没有-w选项,在恢复的过程中可能是未闭合的。
length:截断处的值,如果是100,则表示在100B处截断。
paths:文件地址。
示例:
?12hadoop fs -truncate 55 /user/hadoop/file1 /user/hadoop/file2hadoop fs -truncate -w 127 hdfs://nn1.example.com/user/hadoop/file1
(36)-usage用法: hadoop fs -usage command作用:返回命令的help信息。
本文由职坐标整理发布,学习更多的大数据技术相关知识,请关注职坐标大技术云计算大技术技术频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号