大数据 | HDFS 常用操作命令

码农UP2U

共 7021字,需浏览 15分钟

 · 2021-11-08


早期文章



        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] [ ...] :  List the contents that match the specified file pattern. If path is not  specified, the contents of /user/ 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] [ ...] :  List the contents that match the specified file pattern. If path is not  specified, the contents of /user/ 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 117 13:42 abc.txt

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

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


六、rm 命令

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

$ hadoop fs -rm /test.txtDeleted /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/abcmkdir: `/abc/abc': No such file or directory$ hadoop fs -mkdir -p /abc/abc


八、cp 命令

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

$ hadoop fs -ls /Found 3 itemsdrwxr-xr-x   - hadoop supergroup          0 2021-11-07 13:47 /abc-rw-r--r--   2 hadoop supergroup          5 2021-11-07 13:27 /abc.txtdrwxr-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 /abcdrwxr-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.txtdrwxr-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 /abcdrwxr-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.txtdrwxr-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.txt1234$ hadoop fs -cat /abc.txt1234

十一、appendToFile 命令

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

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

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


十二、总结

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



公众号内回复 【mongo】 下载 SpringBoot 整合操作 MongoDB 的文档。


        之前整理的关于 Redis 的文章:

Redis | Redis 的安装

Redis | Redis 的帮助命令

Redis | Redis 命令分类

Redis | Redis 通用命令

Redis | Redis 字符串相关命令

Redis | Redis 列表相关命令

Redis | Redis 集合相关命令

Redis | Redis 有序集合相关命令

Redis | Redis 哈希相关命令

Redis | 源码阅读 —— 字符串

Redis | 源码阅读 —— 链表

Redis | Redis Pub/Sub相关命令

Redis | 管道 —— PipeLine

Redis | SpringBoot整合Redis

Redis | Redis 的事务一

Redis | Redis 的事务二

Redis | 基础数据类型应用场景

Redis | 事务源码阅读

Redis | 事物源码阅读 —— watch

Redis | 慢查询

Redis | 给接口添加缓存

Redis | Redis 也会算距离


浏览 23
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报