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

图片

目录

一、HDFS 命令前缀

二、ls 命令

三、put 命令

四、moveFromLocal 命令

五、get 命令

六、rm 命令

七、mkdir 命令

八、cp 命令

九、mv 命令

十、cat 命令

十一、appendToFile 命令

十二、总结


        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 的基本操作命令也会非常容易的上手。

图片

图片

  • 10
    点赞
  • 97
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农UP2U

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值