大数据笔记 | HDFS 常用操作命令

简介: 大数据笔记 | HDFS 常用操作命令

12332cd9cb6a51c767df6fd5b0ee8b96.png

       HDFS 是 Hadoop Distributed File System 的简写,即 Hadoop 分布式文件系统。它是 Hadoop 项目的核心子项目,它为大数据分布式计算提供了海量数据的存储与管理。


       既然 HDFS 是文件系统,那么它必然有一套对文件管理的命令,这里介绍一下 HDFS 常用的文件管理命令。


一、HDFS 命令前缀

       所有操作 HDFS 的命令都需要前缀,它的前缀有两种,分别是 hadoop fs 或 hdfs dfs 两种。可以通过 hadoop fs -help 或 hdfs dfs -help 来查看其帮助文件。比如:

$ hadoop fs -help ls
-ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [<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.

       或者使用 hdfs dfs 来查看帮助,命令如下:

$ hdfs dfs -help ls
-ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [<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.

二、ls 命令

       ls 命令用来查看 HDFS 系统中的目录和文件,命令如下:

$ hadoop fs -ls /

       也可以通过给 ls 添加 -R 参数来递归列出要查看目录下的所有目录和文件,命令如下:

$ hadoop fs -ls -R /

        由于目前在 HDFS 中并没有任何文件和目录,因此这里没有显示任何的结果。

三、put 命令

       put 命令用于将本地文件上传到 HDFS 系统中,命令如下:

$ hadoop fs -put test.txt /
$ hadoop fs -ls /
Found 1 items
-rw-r--r--   2 hadoop supergroup          5 2021-11-07 13:22 /test.txt

       通过 -put 命令将本地当前目录下的 test.txt 文件上传到了 HDFS 的 / 目录下,通过 -ls 命令可以看到文件已经上传到 HDFS 系统中了。

四、moveFromLocal 命令

       将本地文件移动到 HDFS 文件系统中,并将本地的文件进行删除,命令如下:

$ ll
总用量 84804
-rw-rw-r--. 1 hadoop hadoop        5 11月  7 13:27 abc.txt
$ hadoop fs -moveFromLocal abc.txt /
$ hadoop fs -ls /
Found 2 items
-rw-r--r--   2 hadoop supergroup          5 2021-11-07 13:27 /abc.txt
-rw-r--r--   2 hadoop supergroup          5 2021-11-07 13:22 /test.txt

       将本地的 abc.txt 文件上传到 HDFS 的 / 目录下,通过 -ls 命令查看 / 目录下已经有了 abc.txt 文件,再来查看本地文件,本地的 abc.txt 文件已经被移除。

五、get 命令

       get 命令用来将 HDFS 文件系统中的文件下载到本地,下载时的文件名不能与本地文件相同,否则会提示文件已存在。命令如下:

$ hadoop fs -get /abc.txt /home/hadoop/
$ ll
总用量 84804
-rw-r--r--. 1 hadoop hadoop        5 11月  7 13:42 abc.txt

       下载文件时确保文件不重名,否则提示文件已存在,命令如下:

$ hadoop fs -get / /home/hadoop/
get: `/home/hadoop/abc.txt': File exists
get: `/home/hadoop/test.txt': File exists

六、rm 命令

       rm 命令用来删除 HDFS 系统中的文件或文件夹,每次可以删除多个文件或目录,命令如下:

$ hadoop fs -rm /test.txt
Deleted /test.txt
$ hadoop fs -ls /
Found 1 items
-rw-r--r--   2 hadoop supergroup          5 2021-11-07 13:27 /abc.txt

七、mkdir 命令

       mkdir 命令用来在 HDFS 系统中创建目录,可以使用 -p 参数创建多级目录,即当父目录不存在时,则自动创建,若不使用 -p 参数,当父目录不存在时则会提示文件或目录不存在。命令如下:

$ hadoop fs -mkdir /test
$ hadoop fs -mkdir /abc/abc
mkdir: `/abc/abc': No such file or directory
$ hadoop fs -mkdir -p /abc/abc

八、cp 命令

       cp 命令在 HDFS 文件系统中用于文件的复制,命令如下:

$ hadoop fs -ls /
Found 3 items
drwxr-xr-x   - hadoop supergroup          0 2021-11-07 13:47 /abc
-rw-r--r--   2 hadoop supergroup          5 2021-11-07 13:27 /abc.txt
drwxr-xr-x   - hadoop supergroup          0 2021-11-07 13:47 /test
$ hadoop fs -cp /abc.txt /abc/
$ hadoop fs -ls -R /
drwxr-xr-x   - hadoop supergroup          0 2021-11-07 13:49 /abc
drwxr-xr-x   - hadoop supergroup          0 2021-11-07 13:47 /abc/abc
-rw-r--r--   2 hadoop supergroup          5 2021-11-07 13:49 /abc/abc.txt
-rw-r--r--   2 hadoop supergroup          5 2021-11-07 13:27 /abc.txt
drwxr-xr-x   - hadoop supergroup          0 2021-11-07 13:47 /test

    通过 cp 命令将 /abc.txt 文件复制到了 /abc/ 目录下,然后使用 ls -R 来递归查看目录。

九、mv 命令

       mv 命令用来在 HDFS 文件系统下完成移动的功能,也可以用来进行重命名。命令如下:

hadoop fs -mv /abc/abc.txt /test/
[hadoop@centos01 ~]$ hadoop fs -ls -R /
drwxr-xr-x   - hadoop supergroup          0 2021-11-07 13:52 /abc
drwxr-xr-x   - hadoop supergroup          0 2021-11-07 13:47 /abc/abc
-rw-r--r--   2 hadoop supergroup          5 2021-11-07 13:27 /abc.txt
drwxr-xr-x   - hadoop supergroup          0 2021-11-07 13:52 /test
-rw-r--r--   2 hadoop supergroup          5 2021-11-07 13:49 /test/abc.txt

       从上面的命令中可以看出,/abc/abc.txt 文件被移动到了 /test/ 目录下。再来看下它的重命名功能:

$ hadoop fs -mv /test/abc.txt /test/abcabc.txt
$ hadoop fs -ls /test/
Found 1 items
-rw-r--r--   2 hadoop supergroup          5 2021-11-07 13:49 /test/abcabc.txt

       通过 ls 命令可以看到,abc.txt 已经被重命名为了 abcabc.txt。

十、cat 命令

       cat 命令用来输出 HDFS 文件系统中某个文件的所有内容,命令如下:

$ hadoop fs -cat /test/abcabc.txt
1234
$ hadoop fs -cat /abc.txt
1234

十一、appendToFile 命令

       将单个或多个文件的内容从本地系统追加到 HDFS 系统的文件中,命令如下:

$ hadoop fs -appendToFile abc.txt /abc.txt
$ hadoop fs -cat /abc.txt
1234
1234

      可以看到,/abc.txt 的内容已经发生了改变。

十二、总结

       HDFS 关于文件的基本操作与 Linux 系统命令的基本是一样的,只是 HDFS 命令增加了 hadoop fs 这样的前缀。如果对 Linux 系统命令有些了解,那么 HDFS 的基本操作命令也会非常容易的上手。

相关实践学习
简单用户画像分析
本场景主要介绍基于海量日志数据进行简单用户画像分析为背景,如何通过使用DataWorks完成数据采集 、加工数据、配置数据质量监控和数据可视化展现等任务。
SaaS 模式云数据仓库必修课
本课程由阿里云开发者社区和阿里云大数据团队共同出品,是SaaS模式云原生数据仓库领导者MaxCompute核心课程。本课程由阿里云资深产品和技术专家们从概念到方法,从场景到实践,体系化的将阿里巴巴飞天大数据平台10多年的经过验证的方法与实践深入浅出的讲给开发者们。帮助大数据开发者快速了解并掌握SaaS模式的云原生的数据仓库,助力开发者学习了解先进的技术栈,并能在实际业务中敏捷的进行大数据分析,赋能企业业务。 通过本课程可以了解SaaS模式云原生数据仓库领导者MaxCompute核心功能及典型适用场景,可应用MaxCompute实现数仓搭建,快速进行大数据分析。适合大数据工程师、大数据分析师 大量数据需要处理、存储和管理,需要搭建数据仓库?学它! 没有足够人员和经验来运维大数据平台,不想自建IDC买机器,需要免运维的大数据平台?会SQL就等于会大数据?学它! 想知道大数据用得对不对,想用更少的钱得到持续演进的数仓能力?获得极致弹性的计算资源和更好的性能,以及持续保护数据安全的生产环境?学它! 想要获得灵活的分析能力,快速洞察数据规律特征?想要兼得数据湖的灵活性与数据仓库的成长性?学它! 出品人:阿里云大数据产品及研发团队专家 产品 MaxCompute 官网 https://www.aliyun.com/product/odps&nbsp;
相关文章
|
4月前
|
SQL 分布式计算 大数据
请问本地安装了大数据计算MaxCompute studio,如何验证联通性及基本DDL操作呢?
请问本地安装了大数据计算MaxCompute studio,如何验证联通性及基本DDL操作呢?
27 0
|
4月前
|
分布式计算 Java 大数据
【大数据技术Hadoop+Spark】HDFS Shell常用命令及HDFS Java API详解及实战(超详细 附源码)
【大数据技术Hadoop+Spark】HDFS Shell常用命令及HDFS Java API详解及实战(超详细 附源码)
225 0
|
4月前
|
安全 大数据 API
elasticsearch|大数据|elasticsearch的api部分实战操作以及用户和密码的管理
elasticsearch|大数据|elasticsearch的api部分实战操作以及用户和密码的管理
64 0
|
4月前
|
存储 分布式计算 Hadoop
【大数据技术Hadoop+Spark】HDFS概念、架构、原理、优缺点讲解(超详细必看)
【大数据技术Hadoop+Spark】HDFS概念、架构、原理、优缺点讲解(超详细必看)
124 0
|
2月前
|
SQL 分布式计算 DataWorks
MaxCompute确实支持对id_card_no进行加密和解密操作
【2月更文挑战第4天】MaxCompute确实支持对id_card_no进行加密和解密操作
27 12
|
4月前
|
人工智能 分布式计算 大数据
Python大数据之PySpark(六)RDD的操作
Python大数据之PySpark(六)RDD的操作
32 0
|
4月前
|
SQL 分布式计算 Hadoop
[AIGC ~大数据] 深入理解Hadoop、HDFS、Hive和Spark:Java大师的大数据研究之旅
[AIGC ~大数据] 深入理解Hadoop、HDFS、Hive和Spark:Java大师的大数据研究之旅
|
4月前
|
SQL 分布式计算 DataWorks
MaxCompute确实支持对id_card_no进行加密和解密操作
MaxCompute确实支持对id_card_no进行加密和解密操作
137 56
|
4月前
|
分布式计算 Hadoop 大数据
大数据成长之路-- hadoop集群的部署(3)HDFS新增节点
大数据成长之路-- hadoop集群的部署(3)HDFS新增节点
69 0
|
4月前
|
存储 分布式计算 Hadoop
Hadoop系列HDFS详解
Hadoop系列HDFS详解
41 0