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

本文涉及的产品
云原生大数据计算服务MaxCompute,500CU*H 100GB 3个月
云原生大数据计算服务 MaxCompute,5000CU*H 100GB 3个月
简介: 大数据笔记 | 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 的基本操作命令也会非常容易的上手。

相关实践学习
基于MaxCompute的热门话题分析
本实验围绕社交用户发布的文章做了详尽的分析,通过分析能得到用户群体年龄分布,性别分布,地理位置分布,以及热门话题的热度。
SaaS 模式云数据仓库必修课
本课程由阿里云开发者社区和阿里云大数据团队共同出品,是SaaS模式云原生数据仓库领导者MaxCompute核心课程。本课程由阿里云资深产品和技术专家们从概念到方法,从场景到实践,体系化的将阿里巴巴飞天大数据平台10多年的经过验证的方法与实践深入浅出的讲给开发者们。帮助大数据开发者快速了解并掌握SaaS模式的云原生的数据仓库,助力开发者学习了解先进的技术栈,并能在实际业务中敏捷的进行大数据分析,赋能企业业务。 通过本课程可以了解SaaS模式云原生数据仓库领导者MaxCompute核心功能及典型适用场景,可应用MaxCompute实现数仓搭建,快速进行大数据分析。适合大数据工程师、大数据分析师 大量数据需要处理、存储和管理,需要搭建数据仓库?学它! 没有足够人员和经验来运维大数据平台,不想自建IDC买机器,需要免运维的大数据平台?会SQL就等于会大数据?学它! 想知道大数据用得对不对,想用更少的钱得到持续演进的数仓能力?获得极致弹性的计算资源和更好的性能,以及持续保护数据安全的生产环境?学它! 想要获得灵活的分析能力,快速洞察数据规律特征?想要兼得数据湖的灵活性与数据仓库的成长性?学它! 出品人:阿里云大数据产品及研发团队专家 产品 MaxCompute 官网 https://www.aliyun.com/product/odps&nbsp;
相关文章
|
2月前
|
安全 Linux Shell
HDFS常用命令
HDFS常用命令
29 1
|
2月前
|
分布式计算 资源调度 Hadoop
Hadoop入门基础(五):Hadoop 常用 Shell 命令一网打尽,提升你的大数据技能!
Hadoop入门基础(五):Hadoop 常用 Shell 命令一网打尽,提升你的大数据技能!
|
3月前
|
分布式计算 Hadoop
hadoop格式化HDFS的命令
【7月更文挑战第21天】
244 5
|
4月前
|
机器学习/深度学习 分布式计算 DataWorks
MaxCompute产品使用问题之有什么命令可以看到当前账号拥有哪些项目的什么权限
MaxCompute作为一款全面的大数据处理平台,广泛应用于各类大数据分析、数据挖掘、BI及机器学习场景。掌握其核心功能、熟练操作流程、遵循最佳实践,可以帮助用户高效、安全地管理和利用海量数据。以下是一个关于MaxCompute产品使用的合集,涵盖了其核心功能、应用场景、操作流程以及最佳实践等内容。
|
4月前
|
Java 大数据 API
【大数据】HDFS、HBase操作教程(含指令和JAVA API)
【大数据】HDFS、HBase操作教程(含指令和JAVA API)
121 0
【大数据】HDFS、HBase操作教程(含指令和JAVA API)
|
4月前
|
存储 分布式计算 大数据
【大数据】分布式文件系统HDFS
【大数据】分布式文件系统HDFS
68 0
【大数据】分布式文件系统HDFS
|
5月前
|
存储 分布式计算 安全
大数据存储技术(2)—— HDFS分布式文件系统
大数据存储技术(2)—— HDFS分布式文件系统
133 0
|
4月前
|
分布式计算 大数据 数据处理
MaxCompute操作报错合集之在本地用tunnel命令上传excel表格到mc遇到报错: tunnel upload C:\Users***\Desktop\a.xlsx mc里的非分区表名 -s false;该怎么办
MaxCompute是阿里云提供的大规模离线数据处理服务,用于大数据分析、挖掘和报表生成等场景。在使用MaxCompute进行数据处理时,可能会遇到各种操作报错。以下是一些常见的MaxCompute操作报错及其可能的原因与解决措施的合集。
|
5月前
|
分布式计算 数据可视化 Hadoop
【分布式计算框架】HDFS常用操作及编程实践
【分布式计算框架】HDFS常用操作及编程实践
85 1

热门文章

最新文章

下一篇
无影云桌面