前期回顾:
大数据学习笔记1——hdfs架构原理
大数据学习笔记2——hdfs安装及相关配置文件的设置
总述:
文件系统(fs) shell包括各种类shell的命令,这些命令直接与hadoop分布式文件系统(hdfs)以及hadoop支持的其他文件系统交互,如本地fs、webhdfs、s3 fs等。fs shell由以下命令调用:
bin/hadoop fs <args>
或者 bin/hdfs dfs <args>
所有的FS shell命令都以路径uri作为参数。URI格式是scheme://authority/path。
对于HDFS,scheme是hdfs,对于本地文件,scheme是file。方案和权限是可选的。如果未指定,则使用配置中指定的默认方案。HDFS文件或目录(比如/parent/child)可以指定为HDFS://namenodehost/parent/child,或者简单地指定为/parent/child(假设您的配置设置为指向HDFS://namenodehost)。
FS shell中的大多数命令的行为类似于相应的Unix命令。每个命令都描述了不同之处。错误信息被发送到stderr,输出被发送到stdout。如果使用HDFS,则HDFS dfs是同义词。可以使用相对路径。对于HDFS,当前工作目录是HDFS主目录/user/<username>,通常必须手动创建。HDFS主目录也可以隐式访问,例如,当使用HDFS垃圾文件夹时,主目录中的. trash目录。
今天先详细描述截图中的这些命令的用法。
fs shell命令(一)
appendToFile
Usage: hadoop fs -appendToFile <localsrc> ... <dst>
从本地文件系统向目标文件系统追加单个或多个源文件内容。
- hadoop fs -appendToFile localfile /user/hadoop/hadoopfile
- hadoop fs -appendToFile localfile1 localfile2 /user/hadoop/hadoopfile
- hadoop fs -appendToFile localfile hdfs://nn.example.com/hadoop/hadoopfile
还可以从标准输入stdin读取输入并将其追加到目标文件系统。
- hadoop fs -appendToFile - /user/hadoop/hadoopfile
cat
Usage: hadoop fs -cat [-ignoreCrc] URI [URI ...]
将源文件复制到标准输出stdout.
- ignorecrc选项禁用checkshum验证.
Example:
- hadoop fs -cat hdfs://nn1.example.com/file1 hdfs://nn2.example.com/file2
- hadoop fs -cat file:///file3 /user/hadoop/file4
checksum
Usage: hadoop fs -checksum URI
返回文件的校验信息.
- hadoop fs -checksum hdfs://nn1.example.com/file1
- hadoop fs -checksum file:///etc/hosts
chgrp
Usage: hadoop fs -chgrp [-R] GROUP URI [URI ...]
更改文件的组信息。用户必须是文件的所有者,或者是超级用户。
选项-R:递归
chmod
Usage: hadoop fs -chmod [-R] <MODE[,MODE]... | OCTALMODE> URI [URI ...]
更改文件的权限。使用-R,通过目录结构递归地进行更改。用户必须是文件的所有者,或者是超级用户。
chown
Usage: hadoop fs -chown [-R] [OWNER][:[GROUP]] URI [URI ]
更改文件的所有者。用户必须是文件的所有者,或者是超级用户。
copyFromLocal
Usage: hadoop fs -copyFromLocal Options <localsrc> URI
类似于fs -put命令,仅限于本地文件。
Options:
- -p : 保留访问和修改时间,所有权和权限。
- -f : 如果目的地已经存在,则覆盖它。
- -d : 跳过创建后缀为. _copying_的临时文件
copyToLocal
Usage: hadoop fs -copyToLocal [-ignorecrc] [-crc] URI <localdst>
类似于get命令,只是目标限制为本地文件引用。
count
Usage: hadoop fs -count [-q] [-h] [-v] [-x] [-t [<storage type>]] [-u] <paths>
计算与指定文件模式匹配的路径下的目录、文件和字节数。获取使用情况。
-u和-q选项控制输出包含的列。
t选项显示每种存储类型的使用情况。如果没有提供-u或-q选项,则忽略-t选项。可以在-t选项中使用的可能参数列表:“”、“all”、“ram_disk”、“ssd”、“disk”或“archive”。
v选项显示标题行。
x选项将快照排除在结果计算之外。如果没有-x选项(默认),则始终从所有inode计算结果,包括给定路径下的所有快照。如果给定-u或-q选项,则忽略-x选项。
Example:
- hadoop fs -count hdfs://nn1.example.com/file1 hdfs://nn2.example.com/file2
- hadoop fs -count -q hdfs://nn1.example.com/file1
- hadoop fs -count -q -h hdfs://nn1.example.com/file1
- hadoop fs -count -q -h -v hdfs://nn1.example.com/file1
- hadoop fs -count -u hdfs://nn1.example.com/file1
- hadoop fs -count -u -h hdfs://nn1.example.com/file1
- hadoop fs -count -u -h -v hdfs://nn1.example.com/file1
- hadoop fs -count -e hdfs://nn1.example.com/file1
cp
Usage: hadoop fs -cp [-f] [-p] URI [URI ...] <dest>
将文件从源复制到目标。此命令还允许多个源,在这种情况下,目标必须是一个目录。
Options:
- f选项覆盖原文件
- The -p option will preserve file attributes [topx] (timestamps, ownership, permission, ACL, XAttr).
- p选项将保留文件属性
Example:
- hadoop fs -cp /user/hadoop/file1 /user/hadoop/file2
- hadoop fs -cp /user/hadoop/file1 /user/hadoop/file2 /user/hadoop/dir
df
Usage: hadoop fs -df [-h] URI [URI ...]
显示空闲空间。
选项:
h选项将以可读的方式显示文件大小(64.0m,而不是67108864)
Example:
- hadoop dfs -df /user/hadoop/dir1
du
Usage: hadoop fs -du [-s] [-h] [-v] [-x] URI [URI ...]
显示给定目录中包含的文件和目录的大小或文件的长度。
Options:
- -s选项将显示文件长度的汇总。
- h选项将以可读的方式显示文件大小(64.0m,而不是67108864)
- -v选项将显示列的名称作为标题行。
- -x选项将从结果计算中排除快照。
返回三列,格式如下
size disk_space_consumed_with_all_replicas full_path_name
Example:
- hadoop fs -du /user/hadoop/dir1 /user/hadoop/file1 hdfs://nn.example.com/user/hadoop/dir1
dus
Usage: hadoop fs -du -s <args>
显示文件长度
先写这么多吧。下回书分解。
提前预祝2020新年快乐。
下篇:
大数据学习笔记4——hdfs 文件系统shell命令(2)
,