【十八掌●内功篇】第五掌:HDFS之Shell

2023-11-22 06:50

本文主要是介绍【十八掌●内功篇】第五掌:HDFS之Shell,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这一篇博文是【大数据技术●降龙十八掌】系列文章的其中一篇,点击查看目录:这里写图片描述大数据技术●降龙十八掌


系列文章:
【十八掌●内功篇】第五掌:HDFS之基础知识
【十八掌●内功篇】第五掌:HDFS之Shell

可以通过hadoop fs –help命令来查看HDFS Shell命令的说明。大部分的HDFS Shell和Linux的shell相似。

一般的shell命令格式为:

bin/hadoop command [genericOptions] [commandOptions]

command是命令;genericOptions是一般的参数;commandOptions是命令参数。

举例
在hadoop fs -ls /input这条命令中,command是fs,genericOptions对应-ls等参数,commandOptions对应于/input这个路径参数。

下面对各个shell命令进行详细地分析

1、 appendToFile
-appendToFile <localsrc> ... <dst>: Appends the contents of all the given local files to thegiven dst file. The dst file will be created if it doesnot exist. If <localSrc> is -, then the input is readfrom stdin.

appendToFile是将本地文件追加到HDFS的文件中,前面的参数是本地文件的路径,后面的参数是HDFS上的文件路径,指定的HDFS文件如果不存在就会先创建,本地文件的路径参数可以有多个,可以将多个本地文件同时追加到一个HDFS文件中。

示例:

hadoop fs -appendToFile ~/myfiles/file1 ~/myfiles/file2 /myhdfsfiles/hdfs1

2、cat
-cat [-ignoreCrc] <src> ...:    Fetch all files that match the file pattern <src>and display their content on stdout.

显示指定路径的HDFS文件,可以一次显示多个文件。

指定-ignoreCrc参数后,可以使用正则表达式来筛选要显示的文件。

示例:

hadoop fs -cat hdfs://ClusterTest/myhdfsfiles/hdfs1
hdfs://ClusterTest/myhdfsfiles/wc.txt

hadoop fs -cat -ignoreCrc hdfs://ClusterTest/myhdfsfiles/*.txt

3、checksum
-checksum <src> ...:    Dump checksum information for files that match the filepattern <src> to stdout. Note that this requires a round-tripto a datanode storing each block of the file, and thus is notefficient to run on a large number of files. The checksum of afile depends on its content, block size and the checksumalgorithm and parameters used for creating the file.

返回文件的校验信息。

举例:

hadoop fs -checksum hdfs://ClusterTest/myhdfsfiles/wc.txt

4、chgrp
chgrp [-R] GROUP PATH...:   This is equivalent to -chown ... :GROUP ...

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

举例:

hadoop fs -chgrp -R centosg
hdfs://ClusterTest/myhdfsfiles/wc.txt

5、chmod
-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...:   Changes permissions of a file.This works similar to shell's chmod with a few exceptions.-R  modifies the files recursively. This is the only optioncurrently supported.MODE    Mode is same as mode used for chmod shell command.Only letters recognized are 'rwxXt'. E.g. +t,a+r,g-w,+rwx,o=rOCTALMODE Mode specifed in 3 or 4 digits. If 4 digits, the first maybe 1 or 0 to turn the sticky bit on or off, respectively.  Unlike shell command, it is not possible to specify only part of the modeE.g. 754 is same as u=rwx,g=rx,o=rIf none of 'augo' is specified, 'a' is assumed and unlikeshell command, no umask is applied.

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

举例:

hadoop fs -chmod -R 777 hdfs://ClusterTest/myhdfsfiles

6、chown
-chown [-R] [OWNER][:[GROUP]] PATH...:  Changes owner and group of a file.This is similar to shell's chown with a few exceptions.-R  modifies the files recursively. This is the only optioncurrently supported.If only owner or group is specified then only owner orgroup is modified.The owner and group names may only consist of digits, alphabet,and any of [-_./@a-zA-Z0-9]. The names are case sensitive.WARNING: Avoid using '.' to separate user name and group thoughLinux allows it. If user names have dots in them and you areusing local file system, you might see surprising results sinceshell command 'chown' is used for local files.

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

举例:

hadoop fs -chown -R centos:supergroup hdfs://ClusterTest/myhdfsfiles

7、copyFromLocal
-copyFromLocal [-f] [-p] <localsrc> ... <dst>:  Identical to the -put command.

将本地文件拷贝到HDFS上。

举例:

hadoop fs -copyFromLocal
~/myfiles/* hdfs://ClusterTest/myhdfsfiles

8、copyToLocal
-copyToLocal [-p] [-ignoreCrc] [-crc] <src> ... <localdst>: Identical to the -get command.

将HDFS上的文件拷贝到本地

举例:

hadoop fs -copyToLocal
hdfs://ClusterTest/myhdfsfiles/wc.txt ~/wx.txt.b

9、count
-count [-q] <path> ...: Count the number of directories, files and bytes under the pathsthat match the specified file pattern.  The output columns are:DIR_COUNT FILE_COUNT CONTENT_SIZE FILE_NAME orQUOTA REMAINING_QUATA SPACE_QUOTA REMAINING_SPACE_QUOTA DIR_COUNT FILE_COUNT CONTENT_SIZE FILE_NAME

统计目录个数,文件和目录下文件的大小。

输出列:DIR_COUNT,
FILE_COUNT, CONTENT_SIZE, PATHNAME

【目录个数,文件个数,总大小,路径名称】

输出列带有 -count
-q 是: QUOTA, REMAINING_QUATA, SPACE_QUOTA, REMAINING_SPACE_QUOTA,
DIR_COUNT, FILE_COUNT, CONTENT_SIZE, PATHNAME
【配置,其余指标,空间配额,剩余空间定额,目录个数,文件个数,总大小,路径名称】
举例:

hadoop fs -count hdfs://ClusterTest/

hadoop fs -count -q hdfs://ClusterTest/

10、cp
-cp [-f] [-p] <src> ... <dst>:  Copy files that match the file pattern <src> to adestination.  When copying multiple files, the destinationmust be a directory. Passing -p preserves access andmodification times, ownership and the mode. Passing -foverwrites the destination if it already exists.

这个命令允许复制多个文件到一个目录。
-f 选项如果文件已经存在将会被重写.
-p 选项保存文件属性 [topx] (timestamps, ownership, permission, ACL, XAttr). 如果指定 -p没有参数, 保存timestamps, ownership, permission. 如果指定 -pa, 保留权限因为ACL是一个权限的超级组。确定是否保存raw命名空间属性取决于是否使用-p决定
举例:

hadoop fs -cp -f hdfs://ClusterTest/myhdfsfiles/* hdfs://ClusterTest/bk

11、 createSnapshot
-createSnapshot <snapshotDir> [<snapshotName>]: Create a snapshot on a directory

创建一个目录的快照

12、deleteSnapshot
-deleteSnapshot <snapshotDir><snapshotName>:    Delete a snapshot from a directory

删除一个目录的快照

13、df
-df [-h] [<path> ...]:  Shows the capacity, free and used space of the filesystem.If the filesystem has multiple partitions, and no path to aparticular partition is specified, then the status of the rootpartitions will be shown.-h   Formats the sizes of files in a human-readable fashionrather than a number of bytes.

显示剩余空间,-h 选项会让人更加易读

举例:

hadoop fs -df -h
hdfs://ClusterTest/myhdfsfiles

14、du
-du [-s] [-h] <path> ...:   Show the amount of space, in bytes, used by the files thatmatch the specified file pattern. The following flags are optional:-s   Rather than showing the size of each individual file thatmatches the pattern, shows the total (summary) size.-h   Formats the sizes of files in a human-readable fashionrather than a number of bytes.Note that, even without the -s option, this only shows size summariesone level deep into a directory.The output is in the form size    name(full path)

显示给定目录的文件大小及包含的目录,如果只有文件只显示文件的大小
-s 选项汇总文件的长度,而不是现实单个文件.
-h 选项显示格式更加易读
举例:

hadoop fs -du -h hdfs://ClusterTest

hadoop fs -du -h -s
hdfs://ClusterTest/

15、expunge
-expunge:   Empty the Trash

清空垃圾回收站

举例:

hadoop fs -expunge

16、get
-get [-p] [-ignoreCrc] [-crc] <src> ... <localdst>: Copy files that match the file pattern <src>to the local name.  <src> is kept.  When copying multiple,files, the destination must be a directory. Passing-p preserves access and modification times,ownership and the mode.

复制文件到本地文件。

-ignorecrc选项复制CRC校验失败的文件

-crc选项复制文件以及CRC信息。

-p是保留文件属性。

举例:

hadoop fs -get -ignoreCrc -crc hdfs://ClusterTest/myhdfsfiles/file1
~

17、getfacl
-getfacl [-R] <path>:   Displays the Access Control Lists (ACLs) of files and directories. If a directory has a default ACL, then getfacl also displays the default ACL.-R: List the ACLs of all files and directories recursively.<path>: File or directory to list.

显示访问控制列表(ACL)的文件和目录. 如果一个目录有默认的ACL, getfacl
也显示默认的ACL.

-R: 递归目录和列出所有文件的ACLs.

举例:

hadoop fs -getfacl -R hdfs://ClusterTest/myhdfsfiles/

18、getmerge
.-getmerge [-nl] <src><localdst>:   Get all the files in the directories thatmatch the source file pattern and merge and sort them to onlyone file on local fs. <src> is kept.-nl   Add a newline character at the end of each file.

符合正则表达式的源目录文件合并到目标文件中。

-nl选项可以设置在每个文件末尾添加一个换行符。

举例:

hadoop fs -getmerge hdfs://ClusterTest/myhdfsfiles/file* ~/files

19、help
-help [cmd ...]:    Displays help for given command or all commands if none is specified.

显示给定的命令说明或者是显示所有的命令说明。
举例:
hadoop fs -help
hadoop fs -help text

20、ls
-ls [-d] [-h] [-R] [<path> ...]:    List the contents that match the specified file pattern. Ifpath is not specified, the contents of /user/<currentUser>will be listed. Directory entries are of the form permissions - userid groupid size_of_directory(in bytes) modification_date(yyyy-MM-dd HH:mm) directoryName and file entries are of the form permissions number_of_replicas userid groupid size_of_file(in bytes) modification_date(yyyy-MM-dd HH:mm) fileName -d  Directories are listed as plain files.-h  Formats the sizes of files in a human-readable fashionrather than a number of bytes.-R  Recursively list the contents of directories.

-d: 目录被列为纯文件。
-h: 文件格式变为易读 (例如 67108864显示 64.0m).
-R: 递归子目录列表中。
如果是文件,则按照如下格式返回文件信息:文件名<副本数>文件大小修改日期修改时间权限用户ID 组ID
如果是目录,返回列表的信息如下:目录名

修改日期修改时间权限用户ID 组ID

举例:

hadoop fs -ls -h -R hdfs://ClusterTest/

21、mkdir
-mkdir [-p] <path> ...: Create a directory in specified location.-p  Do not fail if the directory already exists

创建目录

-p参数是指当目录存在时也不报错

举例:

hadoop fs -mkdir -p hdfs://ClusterTest/myhdfsfiles

22、moveFromLocal
-moveFromLocal <localsrc> ... <dst>:    Same as -put, except that the source isdeleted after it's copied.

将文件从本地移动到HDFS上,实际上是在复制完成后将本地的文件删除。

举例:

hadoop fs -moveFromLocal ~/file1 hdfs://ClusterTest/myhdfsfiles

23、mv
-mv <src> ... <dst>:    Move files that match the specified file pattern <src>to a destination <dst>.  When moving multiple files, thedestination must be a directory.

移动文件,可以将符合正则表达式的文件都移动到一个目录下。如果移动多个文件,目标路径必须是要换个目录。

举例:

hadoop fs -mv hdfs://ClusterTest/myhdfsfiles/* hdfs://ClusterTest/bk2

24、put
-put [-f] [-p] <localsrc> ... <dst>:    Copy files from the local file systeminto fs. Copying fails if the file alreadyexists, unless the -f flag is given. Passing-p preserves access and modification times,ownership and the mode. Passing -f overwritesthe destination if it already exists.

复制本地文件到HDFS。

-f参数指定后如果目标文件已经存在就覆盖

-p参数指定后,将保留原文件的属性

举例:

hadoop fs -put -f -p ~/myfiles/file1 hdfs://ClusterTest/myhdfsfiles

25、renameSnapshot
-renameSnapshot <snapshotDir><oldName><newName>:    Rename a snapshot from oldName to newName

修改快照的的名称

26、rm
-rm [-f] [-r|-R] [-skipTrash] <src> ...:    Delete all files that match the specified file pattern.Equivalent to the Unix command "rm <src>"-skipTrash option bypasses trash, if enabled, and immediatelydeletes <src>-f     If the file does not exist, do not display a diagnosticmessage or modify the exit status to reflect an error.-[rR]  Recursively deletes directories

删除符合给定正则表达式的文件

-f 如果文件不存在也不报错

-rf递归删除

-skipTrash不进回收站,直接删除

27、rmdir
-rmdir [--ignore-fail-on-non-empty] <dir> ...:  Removes the directory entry specified by each directory argument,provided it is empty.

删除目录,只能删除空目录

–ignore-fail-on-non-empty当使用通配符删除多个目录时,如果某一个目录下还有文件,不会提示失败。

举例:

hadoop fs -rmdir –ignore-fail-on-non-empty
hdfs://ClusterTest/myhdfsfiles

注意这里是两个–

28、setfacl
-setfacl [-R] [{-b|-k} {-m|-x <acl_spec>} <path>]|[--set <acl_spec><path>]: Sets Access Control Lists (ACLs) of files and directories.Options:-b :Remove all but the base ACL entries. The entries for user, group and others are retained for compatibility with permission bits.-k :Remove the default ACL.-R :Apply operations to all files and directories recursively.-m :Modify ACL. New entries are added to the ACL, and existing entries are retained.-x :Remove specified ACL entries. Other ACL entries are retained.--set :Fully replace the ACL, discarding all existing entries. The <acl_spec> must include entries for user, group, and others for compatibility with permission bits.<acl_spec>: Comma separated list of ACL entries.<path>: File or directory to modify.

设置访问控制列表(ACL)的文件和目录。

-b:移除所有除了基本的ACL条目。用户、组和其他的条目被保留为与权限位的兼容性。

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

hadoop fs -setfacl -m user:hadoop:rw- /file
hadoop fs -setfacl -x user:hadoop /file
hadoop fs -setfacl -b /file
hadoop fs -setfacl -k /dir
hadoop fs -setfacl –set user::rw-,user:hadoop:rw-,group::r–,other::r–
/file
hadoop fs -setfacl -R -m user:hadoop:r-x /dir
hadoop fs -setfacl -m default:user:hadoop:r-x /dir

29、setrep
-setrep [-R] [-w] <rep><path> ...:  Set the replication level of a file. If <path> is a directorythen the command recursively changes the replication factor ofall files under the directory tree rooted at <path>.The -w flag requests that the command wait for the replicationto complete. This can potentially take a very long time.The -R flag is accepted for backwards compatibility. It has no effect.

更改文件的备份. 如果是一个目录,会递归改变目录下文件的备份。
-w标识,要求备份完成,这可能需要很长时间。
-R标识,是为了兼容,没有实际效果

30、stat
-stat [format] <path> ...:  Print statistics about the file/directory at <path>in the specified format. Format accepts filesize in blocks (%b), group name of owner(%g),filename (%n), block size (%o), replication (%r), user name of owner(%u), modification date (%y, %Y)

按指定格式打印文件/目录的打印统计。格式接受文件块(%b), 类型(%F), groutp拥有者(%g), 名字(%n), block size (%o), replication (%r), 用户拥有者(%u), 修改日期 (%y,
%Y). %y 显示UTC 日期如“yyyy-MM-dd HH:mm:ss” 和 %Y 1970年1月1日以来显示毫秒UTC. 如果没有指定, 默认使用%y.

31、tail
-stat [format] <path> ...:  Print statistics about the file/directory at <path>in the specified format. Format accepts filesize in blocks (%b), group name of owner(%g),filename (%n), block size (%o), replication (%r), user name of owner(%u), modification date (%y, %Y)

显示文件内容,最后千字节的文件发送到stdout

- f选项将输出附加数据随着文件的增长,如同Unix
32、test
-test -[defsz] <path>:  Answer various questions about <path>, with result via exit status.-d  return 0 if <path> is a directory.-e  return 0 if <path> exists.-f  return 0 if <path> is a file.-s  return 0 if file <path> is greater than zero bytes in size.-z  return 0 if file <path> is zero bytes in size.else, return 1.

-d:如果路径是一个目录,返回0
-e:如果路径已经存在,返回0
-f: 如果路径是一个文件,返回0
-s:如果路径不是空,返回0
-z:如果文件长度为0,返回0

33、text
-text [-ignoreCrc] <src> ...:   Takes a source file and outputs the file in text format.The allowed formats are zip and TextRecordInputStream and Avro.

将一个源文件,以文本格式输出文件。允许的格式是zip和textrecordinputstream

34、touchz
-touchz <path> ...: Creates a file of zero lengthat <path> with current time as the timestamp of that <path>.An error is returned if the file exists with non-zero length

创建一个零长度的文件

35、usage
-touchz <path> ...: Creates a file of zero lengthat <path> with current time as the timestamp of that <path>.An error is returned if the file exists with non-zero length

返回单个命令的帮助

这一篇博文是【大数据技术●降龙十八掌】系列文章的其中一篇,点击查看目录:这里写图片描述大数据技术●降龙十八掌

这篇关于【十八掌●内功篇】第五掌:HDFS之Shell的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/408302

相关文章

HDFS—存储优化(纠删码)

纠删码原理 HDFS 默认情况下,一个文件有3个副本,这样提高了数据的可靠性,但也带来了2倍的冗余开销。 Hadoop3.x 引入了纠删码,采用计算的方式,可以节省约50%左右的存储空间。 此种方式节约了空间,但是会增加 cpu 的计算。 纠删码策略是给具体一个路径设置。所有往此路径下存储的文件,都会执行此策略。 默认只开启对 RS-6-3-1024k

HDFS—集群扩容及缩容

白名单:表示在白名单的主机IP地址可以,用来存储数据。 配置白名单步骤如下: 1)在NameNode节点的/opt/module/hadoop-3.1.4/etc/hadoop目录下分别创建whitelist 和blacklist文件 (1)创建白名单 [lytfly@hadoop102 hadoop]$ vim whitelist 在whitelist中添加如下主机名称,假如集群正常工作的节

jenkins 插件执行shell命令时,提示“Command not found”处理方法

首先提示找不到“Command not found,可能我们第一反应是查看目标机器是否已支持该命令,不过如果相信能找到这里来的朋友估计遇到的跟我一样,其实目标机器是没有问题的通过一些远程工具执行shell命令是可以执行。奇怪的就是通过jenkinsSSH插件无法执行,经一番折腾各种搜索发现是jenkins没有加载/etc/profile导致。 【解决办法】: 需要在jenkins调用shell脚

argodb自定义函数读取hdfs文件的注意点,避免FileSystem已关闭异常

一、问题描述 一位同学反馈,他写的argo存过中调用了一个自定义函数,函数会加载hdfs上的一个文件,但有些节点会报FileSystem closed异常,同时有时任务会成功,有时会失败。 二、问题分析 argodb的计算引擎是基于spark的定制化引擎,对于自定义函数的调用跟hive on spark的是一致的。udf要通过反射生成实例,然后迭代调用evaluate。通过代码分析,udf在

站长常用Shell脚本整理分享(全)

站长常用Shell脚本整理分享 站长常用Shell脚本整理分享1-10 站长常用Shell脚本整理分享11-20 站长常用Shell脚本整理分享21-30 站长常用Shell脚本整理分享31-40 站长常用Shell脚本整理分享41-50 站长常用Shell脚本整理分享51-59 长期更新

Shell脚本实现自动登录服务器

1.登录脚本 login_server.sh #!/bin/bash# ReferenceLink:https://yq.aliyun.com/articles/516347#show all host infos of serverList.txtif [[ -f ./serverList.txt ]]thenhostNum=`cat ./serverList.txt | wc -l`e

[轻笔记]ubuntu shell脚本切换conda环境

source /home/yourhostname/anaconda3/etc/profile.d/conda.sh # 关键!!!conda activate env_name

[轻笔记] ubuntu Shell脚本实现监视指定进程的运行状态,并能在程序崩溃后重启动该程序

根据网上博客实现,发现只能监测进程离线,然后对其进行重启;然而,脚本无法打印程序正常状态的信息。自己通过不断修改测试,发现问题主要在重启程序的命令上(需要让重启的程序在后台运行,不然会影响监视脚本进程,使其无法正常工作)。具体程序如下: #!/bin/bashwhile [ 1 ] ; dosleep 3if [ $(ps -ef|grep exe_name|grep -v grep|

adb shell 执行后台程序后断开adb后台进程被结束的解决办法

环境:Android 版本 Android8 通常让程序后台执行就是在命令 最后加上 &即可,但是在Android 8上实验发现,程序的确后台了,但是拔掉USB线再连接上发现进程已结束。不确定Android早期版本是否存在此问题。 参考网上一些Linux方法,如加nohup 仍然无效,还是会结束。看来Android adb shell 与 Linux shell 还是有一定区别。 后来在网上

shell脚本中变量中字符串替换的测试 /和//的区别

test_char=abbbcbbbf echo "bf:test_char = " $test_char test_char=${test_char/bbb/ddd} echo "af:test_char = " $test_char 输出: bf:test_char =  abbbcbbbf af:test_char =  adddcbbbf 只匹配第一个