前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >大数据 | HDFS 常用操作命令

大数据 | HDFS 常用操作命令

作者头像
码农UP2U
发布2021-11-17 14:22:20
5610
发布2021-11-17 14:22:20
举报
文章被收录于专栏:码农UP2U码农UP2U码农UP2U

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-11-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码农UP2U 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
大数据
全栈大数据产品,面向海量数据场景,帮助您 “智理无数,心中有数”!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档