zl程序教程

您现在的位置是:首页 >  工具

当前栏目

hive中操作hdfs命令

命令HDFS 操作 hive
2023-06-13 09:15:00 时间

大家好,又见面了,我是你们的朋友全栈君。

版本:Hadoop 2.7.4

– 查看dfs帮助信息 [root@hadp-master sbin]# dfs Usage: dfs [generic options] [-appendToFile … ] [-cat [-ignoreCrc] …] [-checksum …] [-chgrp [-R] GROUP PATH…] [-chmod [-R] <MODE[,MODE]… | OCTALMODE> PATH…] [-chown [-R] [OWNER][:[GROUP]] PATH…] [-copyFromLocal [-f] [-p] [-l] … ] [-copyToLocal [-p] [-ignoreCrc] [-crc] … ] [-count [-q] [-h] …] [-cp [-f] [-p | -p[topax]] … ] [-createSnapshot []] [-deleteSnapshot ] [-df [-h] [ …]] [-du [-s] [-h] …] [-expunge] [-find … …] [-get [-p] [-ignoreCrc] [-crc] … ] [-getfacl [-R] ] [-getfattr [-R] {-n name | -d} [-e en] ] [-getmerge [-nl] ] [-help [cmd …]] [-ls [-d] [-h] [-R] [ …]] [-mkdir [-p] …] [-moveFromLocal … ] [-moveToLocal ] [-mv … ] [-put [-f] [-p] [-l] … ] [-renameSnapshot ] [-rm [-f] [-r|-R] [-skipTrash] …] [-rmdir [–ignore-fail-on-non-empty]

…]

[-setfacl [-R] [{-b|-k} {-m|-x <acl_spec>} ]|[–set <acl_spec> ]]

[-setfattr {-n name [-v value] | -x name} ]

[-setrep [-R] [-w] …]

[-stat [format] …]

[-tail [-f] ]

[-test -[defsz] ]

[-text [-ignoreCrc] …]

[-touchz …]

[-truncate [-w] …]

[-usage [cmd …]]

注意:

以下指令均是在,Linux 命令行窗口界面操作。 [ ]表示可选参数,<>表示必须参数。 开始使用命令前,必须启动Hadoop (1)-appendToFile

用法: dfs -appendToFile …

作用:是将一个或者多个文件添加到HDFS系统中。

示例:

dfs -appendToFile localfile /user/hadoop/hadoopfile dfs -appendToFile localfile1 localfile2 /user/hadoop/hadoopfile dfs -appendToFile localfile hdfs://nn.example.com/hadoop/hadoopfile dfs -appendToFile – hdfs://nn.example.com/hadoop/hadoopfile Reads the input from stdin. (2)-cat

用法:dfs -cat URI [URI …]

作用:查看文件内容(可以查看本地和HDFS上的内容)。

示例:

dfs -cat hdfs://nn1.example.com/file1 hdfs://nn2.example.com/file2 dfs -cat file:///file3 /user/hadoop/file4

(3)-checksum

用法: dfs -checksum URI

作用:查看校验码信息。(例子显示了MD5)

示例:

dfs -checksum hdfs://nn1.example.com/file1 dfs -checksum file:///etc/hosts (4)-chgrp

用法: dfs -chgrp [-R] GROUP URI [URI …]

作用:改变文件所属的组。(Change group association of files.)

使用-R 将使改变在目录结构下递归进行。

(5)-chmod

作用:改变文件访问权限。

用法:dfs -chmod [-R] <MODE[,MODE]… | OCTALMODE> URI [URI …]

这里可以参考 Linux下文件系统的chmod的用法,基本类似。

(6)-chown

作用:dfs -chown [-R] [OWNER][:[GROUP]] URI [URI ]

用法:改变文件的所有者。使用-R 将使改变在目录结构下递归进行。命令的使用者必须是超级用户。

(7)-copyFromLocal

用法:dfs -copyFromLocal URI

作用:类似于put命令,和put不同的是,拷贝的源地址必须是本地文件地址。

-f 参数 当拷贝的目标文件存在时,进行覆盖。

示例:

[root@two1 fanrui]# dfs -copyFromLocal testFlatMap.txt /1.txt copyFromLocal: `/1.txt’: File exists 这个时候加上-f参数。即可覆盖。

[root@two1 fanrui]# dfs -copyFromLocal -f testFlatMap.txt /1.txt

(8)-copyToLocal

用法: dfs -copyToLocal [-ignorecrc] [-crc] URI

作用:类似于get指令。和get不同的是,拷贝的目的地址必须是本地文件地址。

(9)-count

作用:计算paths下的目录数,文件数和字节数。

用法: dfs -count [-q] [-h] [-v]

dfs -count hdfs://nn1.example.com/file1 hdfs://nn2.example.com/file2 dfs -count -q hdfs://nn1.example.com/file1 dfs -count -q -h hdfs://nn1.example.com/file1 hdfs dfs -count -q -h -v hdfs://nn1.example.com/file1 (10)-cp 用法:dfs -cp [-f] [-p | -p[topax]] URI [URI …] 作用:拷贝,HDFS文件系统中进行的拷贝操作。 -f 参数选项:当文件存在时,进行覆盖。 -p 参数选项:将权限、所属组、时间戳、ACL以及XAttr等也进行拷贝。下面是官网的描述。

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.

示例:

[root@two1 fanrui]# dfs -cp -p /tmp/fan /tmp/fan1 (11)-df 用法:dfs -df [-h] URI [URI …] 作用:显示剩余空间。 示例:

[root@two1 fanrui]# dfs -df / Filesystem Size Used Available Use% hdfs://localhost:9000 37626667008 311296 24792702976 0%

(12)-dus 作用:显示文件长度概要。该方法已经被舍去,等价于 -du -s 方法。见(11)

(13)-expunge 作用:从垃圾桶目录永久删除超过保留阈值的检查点中的文件,并创建新检查点。

用法:dfs -expunge

(14)-find 作用:查找满足表达式的文件和文件夹。没有配置path的话,默认的就是全部目录/;如果表达式没有配置,则默认为-print。 用法: dfs -find … … -name pattern 所要查找文件的文件名。 -iname pattern 所要查找的文件名,不区分大小写。 -print 打印。 -print0 打印在一行,如下图所示。

示例:

dfs -find / -name test -print (15)-get 作用:从HDFS上拷贝文件到本地。 用法:dfs -get [-ignorecrc] [-crc] 示例:

dfs -get /user/hadoop/file localfile dfs -get hdfs://nn.example.com/user/hadoop/file localfile

(16)getfacl

作用:显示文件和文件夹的ACLs(Access Control Lists)。如果目录有默认的ACL,则显示之。 -R参数:递归显示。 用法:

dfs -getfacl [-R]

Options:

-R: List the ACLs of all files and directories recursively. path: File or directory to list. 示例:

dfs -getfacl /file dfs -getfacl -R /dir Exit Code:

Returns 0 on success and non-zero on error.

(17)getfattr

作用:显示文件或目录的扩展属性名和值(如果有的话)

用法:dfs -getfattr [-R] -n name | -d [-e en]

Options:

-R:递归显示文件夹和文件。 -n name:转储命名的扩展属性值。 -d:转储与路径名相关联的所有扩展属性值。 -e en: 检索后的值进行编码。 有效的编码是 “text”, “hex”, and “base64”. 值编码作为文本字符串是用双引号括起来的(“),值编码作为16进制和64进制,前缀分别为 0x 和 0s。 path:文件或文件夹路径。

示例:

dfs -getfattr -d /file dfs -getfattr -R -n user.myAttr /dir (18)-getmerge 作用:是将HDFS上一个目录中所有的文件合并到一起输出到一个本地文件上。 用法:dfs -getmerge [-nl]

示例:

dfs -getmerge -nl /src /opt/output.txt dfs -getmerge -nl /src/file1.txt /src/file2.txt /output.txt (19)-help 作用:帮助文档

用法:dfs -help

(20)-ls 作用:查看文件,与linux下ls命令基本类似。 用法:dfs -ls [-d] [-h] [-R] 选项: -d:只展示查询展示目录;

-h:显示为人眼更易识别的单位(原来是字节)。

-R:递归展示,显示所有的文件夹及文件

示例:

dfs -ls -d / dfs -ls -h / dfs -ls -R / -lsr 作用:已经被舍去,效果等同于-ls -R

(21)-mkdir 作用:创建文件夹。

用法:dfs -mkdir [-p] 选项: -p:创建父目录。类似于Unix的mkdir -p命令。 示例:

dfs -mkdir /user/hadoop/dir1 /user/hadoop/dir2 dfs -mkdir hdfs://nn1.example.com/user/hadoop/dir hdfs://nn2.example.com/user/hadoop/dir (22)-moveFromLocal 用法:dfs -moveFromLocal 作用:类似于put命令,不同put命令的是,该操作是移动(意思就是localsrc将被删除)。localsrc应是本地文件。

(23)-moveToLocal 用法:dfs -moveToLocal [-crc] 作用:该命令尚未实现,显示“Not implemented yet”。

(24)-mv 用法:移动文件。 作用: dfs -mv URI [URI …] 示例:

dfs -mv /user/hadoop/file1 /user/hadoop/file2 dfs -mv hdfs://nn.example.com/file1 hdfs://nn.example.com/file2 hdfs://nn.example.com/file3 hdfs://nn.example.com/dir1

(25)-put 用法: dfs -put … 作用:将本地的文件上传(复制)到HDFS是dst目录下。

示例:

dfs -put localfile /user/hadoop/hadoopfile dfs -put localfile1 localfile2 /user/hadoop/hadoopdir dfs -put localfile hdfs://nn.example.com/hadoop/hadoopfile dfs -put – hdfs://nn.example.com/hadoop/hadoopfile Reads the input from stdin. (26)-rm 用法:dfs -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.

示例:

dfs -rm hdfs://nn.example.com/file /user/hadoop/emptydir

(27)-rmdir 用法:dfs -rmdir [–ignore-fail-on-non-empty] URI [URI …] 作用:删除空目录。 选项: —ignore-fail-on-non-empty:使用它的时候,忽略因文件夹非空删除失败的信息。

(28)-rmr 作用:该方法已经被舍去。和-rm -r效果一样。递归删除。

(29)-setfacl 用法:dfs -setfacl [-R] [-b |-k -m |-x <acl_spec> ] |[–set <acl_spec> ] 作用:设置访问控制列表(ACL)的文件和目录。

选项:

-b:移除所有除了基本的ACL条目。用户、组和其他的条目被保留为与权限位的兼容性。 -k:删除默认的ACL。 -R: 递归应用于所有文件和目录的操作。 -m:修改ACL。新的项目添加到ACL,并保留现有的条目。 -x:删除指定的ACL条目。其他保留ACL条目。 –set:完全替换ACL,丢弃所有现有的条目。acl_spec必须包括用户,组,和其他有权限位的兼容性。 acl_spec:逗号分隔的ACL条目列表。 path:修改文件或目录。

示例:

dfs -setfacl -m user:hadoop:rw- /file dfs -setfacl -x user:hadoop /file dfs -setfacl -b /file dfs -setfacl -k /dir dfs -setfacl –set user::rw-,user:hadoop:rw-,group::r–,other::r– /file dfs -setfacl -R -m user:hadoop:r-x /dir dfs -setfacl -m default:user:hadoop:r-x /dir (30)-setrep 用法:dfs -setrep [-R] [-w] 作用:改变文件的目标副本系数,放入REP中。选项-R将递归的改变PATH指定的目录中所有文件的目标副本系数。副本系数需要一定的时间才能达到目标值。选项-w将等待副本系数以与目标值相匹配。

示例:

dfs -setrep -w 3 /user/hadoop/dir1 (31)-stat 用法: dfs -stat [format] … 作用:根据一定格式打印文件/文件夹的统计信息。 文件大小 (%b), 类型 (%F), 所有者所在组 (%g), 名字 (%n), 块大小 (%o), 副本 (%r), 用户名(%u), 修改时间 (%y, %Y)。默认的是%y。 示例: dfs -stat “%F %u:%g %b %y %n” /file

(32)-tail 用法:dfs -tail [-f] URI 作用:输出文件最后1kb的内容。 选项: -f:和unix中tail -f命令类似,当文件内容更新时,输出将会改变,具有实时性。

示例:用一个场景测试下。首先HDFS的/目录下有文件mpwtest1.txt 命令:dfs -tail -f /mpwtest1.txt 开启另外一个终端。输入命令: dfs -appendToFile mpwtest2.txt /mpwtest1.txt 可以发现 窗口1 有变化。

(33)-test

作用:判断文件信息

用法:dfs -test -[defsz] URI

选项:

-d:如果路径是一个目录,返回0 -e:如果路径已经存在,返回0 -f: 如果路径是一个文件,返回0 -s:如果路径不是空,返回0 -z:如果文件长度为0,返回0 URI:资源地址,可以是文件也可以是目录。

示例:

dfs -test -e filename

(34)-text 用法:dfs -text 作用:将HDFS中文件以文本形式输出(包括zip包,jar包等形式) 示例:dfs -text /wc.jar

(35)-touchz 用法: dfs -touchz URI [URI …] 作用:创建一个空文件。 示例:dfs -touchz /hello.jar

(35)-truncate 用法: dfs -truncate [-w]

作用:截断指定长度匹配的所有文件内容。

选项:

-w:需要等待命令完成块恢复。如果没有-w选项,在恢复的过程中可能是未闭合的。 length:截断处的值,如果是100,则表示在100B处截断。 paths:文件地址。

示例:

dfs -truncate 55 /user/hadoop/file1 /user/hadoop/file2 dfs -truncate -w 127 hdfs://nn1.example.com/user/hadoop/file1

(36)-usage 用法: dfs -usage command 作用:返回命令的help信息。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。