【Hadoop|HDFS篇】HDFS的Shell操作

1. 基本语法

hadoop fs 具体命令或者hadoop dfs 具体命令。

两个是完全相同的。

2. 命令大全

hadoop fs:

bash 复制代码
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] [-t <thread count>] <localsrc> ... <dst>]
	[-copyToLocal [-f] [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
	[-count [-q] [-h] [-v] [-t [<storage type>]] [-u] [-x] [-e] <path> ...]
	[-cp [-f] [-p | -p[topax]] [-d] <src> ... <dst>]
	[-createSnapshot <snapshotDir> [<snapshotName>]]
	[-deleteSnapshot <snapshotDir> <snapshotName>]
	[-df [-h] [<path> ...]]
	[-du [-s] [-h] [-v] [-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>]
	[-head <file>]
	[-help [cmd ...]]
	[-ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [-e] [<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] [-s <sleep interval>] <file>]
	[-test -[defsz] <path>]
	[-text [-ignoreCrc] <src> ...]
	[-touch [-a] [-m] [-t TIMESTAMP ] [-c] <path> ...]
	[-touchz <path> ...]
	[-truncate [-w] <length> <path> ...]
	[-usage [cmd ...]]

Generic options supported are:
-conf <configuration file>        specify an application configuration file
-D <property=value>               define a value for a 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 <file1,...>                specify a comma-separated list of files to be copied to the map reduce cluster
-libjars <jar1,...>               specify a comma-separated list of jar files to be included in the classpath
-archives <archive1,...>          specify a comma-separated list of archives to be unarchived on the compute machines

The general command line syntax is:
command [genericOptions] [commandOptions]

2.1 help命令

hadoop fs -help 命令

比如:

bash 复制代码
[hexuan@hadoop102 ~]$ hadoop fs -help ls
-ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [-e] [<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.
    -e  Display the erasure coding policy of files and directories.

2.2 上传

(1)-moveFromLocal :从本地剪切黏贴到HDFS

bash 复制代码
[hexuan@hadoop102 ~]$ vim wuguo.txt
[hexuan@hadoop102 ~]$ hadoop fs -moveFromLocal wuguo.txt /sanguo/
2024-09-04 10:39:14,395 INFO sasl.SaslDataTransferClient: SASL encryption trust check: localHostTrusted = false, remoteHostTrusted = false

(2)-copyFromLocal:从本地文件系统拷贝文件到HDFS中。

bash 复制代码
hadoop fs -copyFromLocal wuguo.txt /sanguo/

(3)-put:等同于copyFromLocal,生产环境更习惯于用put。

bash 复制代码
hadoop fs -put wuguo.txt /sanguo/

(4)-appendToFile:追加一个文件到已经存在的文件末尾。

2.3 下载

(1)-copyToLocal:从HDFS拷贝到本地

(2)-get:等同于copyToLocal,生产环境中更常用get。

bash 复制代码
[hexuan@hadoop102 ~]$ hadoop fs -get /sanguo/wuguo.txt ./wuguo_copy.txt
2024-09-04 10:59:00,847 INFO sasl.SaslDataTransferClient: SASL encryption trust check: localHostTrusted = false, remoteHostTrusted = false

2.4 其他命令

(1)-ls:显示目录信息。

(2)-cat:显示文件内容。

(3)-chgrp,-chmod,-chown:与Linux系统中的用法一致,修改文件的权限。

(4)-mkdir:创建文件目录。

(5)-cp:从HDFS的一个路径复制到另一个路径。

(6)-mv:在HDFS中移动文件。

(7)-tail:显示一个文件的末尾1kb的数据。

(8)-rm:删除文件或文件夹。

(9)-rm -r:递归删除目录和目录里的内容。

(10)-du:统计文件夹的大小信息。

bash 复制代码
[hexuan@hadoop102 ~]$ hadoop fs -du /sanguo
40  120  /sanguo/jiangwei.txt._COPYING_
0   0    /sanguo/simayi.txt
12  36   /sanguo/wuguo.txt
[hexuan@hadoop102 ~]$ hadoop fs -du -s /sanguo
52  156  /sanguo

说明:

40, 12, 52表示的都是文件大小,120表示的是40*3个副本,/sanguo表示查看的目录。

添加选项-s显示文件夹整体的信息。

(11)-setrep:设置HDFS中文件的副本数量

bash 复制代码
[hexuan@hadoop102 ~]$ hadoop fs -setrep 10 /sanguo/wuguo.txt
Replication 10 set: /sanguo/wuguo.txt

这里设置的副本数只是记录在NameNode的元数据,是否真的会有这么多副本,还得看DataNode的数量。目前只有三台设备,所以最多也就三个副本,只有当节点数达到10台,副本数才能到10个。

相关推荐
在下不上天5 分钟前
Flume日志采集系统的部署,实现flume负载均衡,flume故障恢复
大数据·开发语言·python
智慧化智能化数字化方案35 分钟前
华为IPD流程管理体系L1至L5最佳实践-解读
大数据·华为
PersistJiao2 小时前
在 Spark RDD 中,sortBy 和 top 算子的各自适用场景
大数据·spark·top·sortby
2301_811274312 小时前
大数据基于Spring Boot的化妆品推荐系统的设计与实现
大数据·spring boot·后端
Yz98762 小时前
hive的存储格式
大数据·数据库·数据仓库·hive·hadoop·数据库开发
青云交2 小时前
大数据新视界 -- 大数据大厂之 Hive 数据导入:多源数据集成的策略与实战(上)(3/ 30)
大数据·数据清洗·电商数据·数据整合·hive 数据导入·多源数据·影视娱乐数据
武子康2 小时前
大数据-230 离线数仓 - ODS层的构建 Hive处理 UDF 与 SerDe 处理 与 当前总结
java·大数据·数据仓库·hive·hadoop·sql·hdfs
武子康2 小时前
大数据-231 离线数仓 - DWS 层、ADS 层的创建 Hive 执行脚本
java·大数据·数据仓库·hive·hadoop·mysql
时差9532 小时前
Flink Standalone集群模式安装部署
大数据·分布式·flink·部署
锵锵锵锵~蒋2 小时前
实时数据开发 | 怎么通俗理解Flink容错机制,提到的checkpoint、barrier、Savepoint、sink都是什么
大数据·数据仓库·flink·实时数据开发