当前位置: 首页 > news >正文

HDFS的操作

HDFS作为分布式存储的文件系统有其对数据的路径的表达方式HDFS同Linux系统一样以/作为根目录在HDFS中区分本地操作和HDFS操作主要通过路径前缀Scheme来识别HDFS操作使用hdfs://前缀hdfs://节点名称:端口号/具体的路径老版本hadoop fs [选项] 命令新版本hdfs dfs [选项] 命令在HDFS中创建文件夹hadoop fs -mkdir [-p] path ... # directory 名录电话簿计算机中的目录 或新版本的命令 hdfs dfs -mkdir [-p] path ...[rootmaster hadoop]# hadoop fs -mkdir -p file:///itheima/bigdata # file://协议头表示在Linux本地中操作 [rootmaster hadoop]# ls / bin boot data demo01 dev etc home itheima lib lib64 media mnt opt proc Projects root run sbin srv sys Tests tmp usr var [rootmaster hadoop]# ls /itheima/ bigdata [rootmaster hadoop]# hadoop fs -mkdir -p hdfs://master:8020/itheima/bigdata # hdfs://协议头表示在HDFS文件系统中操作 [rootmaster hadoop]# hadoop fs -ls / Found 6 items drwxr-xr-x - root supergroup 0 2026-04-27 00:30 /Tests drwxr-xr-x - root supergroup 0 2026-05-14 09:12 /data drwxr-xr-x - root supergroup 0 2026-05-17 23:06 /itheima -rw-r--r-- 2 root supergroup 11 2026-04-23 09:38 /test.txt drwxrwx--- - root supergroup 0 2026-04-18 02:27 /tmp drwxr-xr-x - root supergroup 0 2026-05-14 08:58 /user [rootmaster hadoop]# hadoop fs -ls /itheima/ Found 1 items drwxr-xr-x - root supergroup 0 2026-05-17 23:06 /itheima/bigdata[rootmaster hadoop]# hdfs dfs -mkdir -p /home/hadoop/itcast # 不带协议头默认在HDFS文件系统中操作 [rootmaster hadoop]# hdfs dfs -ls / Found 7 items drwxr-xr-x - root supergroup 0 2026-04-27 00:30 /Tests drwxr-xr-x - root supergroup 0 2026-05-14 09:12 /data drwxr-xr-x - root supergroup 0 2026-05-17 23:10 /home drwxr-xr-x - root supergroup 0 2026-05-17 23:06 /itheima -rw-r--r-- 2 root supergroup 11 2026-04-23 09:38 /test.txt drwxrwx--- - root supergroup 0 2026-04-18 02:27 /tmp drwxr-xr-x - root supergroup 0 2026-05-14 08:58 /user [rootmaster hadoop]# ls /home # 发现Linux本地中没有itcast这个目录证实不带协议头则默认在HDFS中进行操作 user2HDFS中的ls-R选项递归查看每个目录中存在的子目录和文件下面是部分输出[rootmaster hadoop]# hdfs dfs -ls -R / drwxr-xr-x - root supergroup 0 2026-04-27 00:30 /Tests -rw-r--r-- 2 root supergroup 178 2026-04-27 00:01 /Tests/data1.txt -rw-r--r-- 2 root supergroup 293 2026-04-27 00:30 /Tests/data2.txt -rw-r--r-- 2 root supergroup 0 2026-04-26 23:51 /Tests/input.txt drwxr-xr-x - root supergroup 0 2026-05-14 09:12 /data drwxr-xr-x - root supergroup 0 2026-05-14 09:12 /data/tmp drwxrwxrwt - root root 0 2026-05-14 09:12 /data/tmp/logs drwxrwx--- - root root 0 2026-05-14 09:12 /data/tmp/logs/root drwxrwx--- - root root 0 2026-05-15 00:02 /data/tmp/logs/root/logs-tfile上传文件到HDFShadoop fs -put [-f] [-p] localsrc dst 解释 -f表示强制覆盖-p表示保留文件信息修改时间、权限等 src是source来源、根源计算中表示源文件的缩写。dst是destination目的地、目标的缩写 前一个路径是Linux本地的文件的路径后一个路径是要上传到HDFS中的路径 hdfs dfs -put [-f] [-p] localsrc dst[rootmaster ~]# vim testfile1.txt [rootmaster ~]# cat testfile1.txt itheima itcast hahaha heiheihei bigdata 666 [rootmaster ~]# hadoop fs -put file:///root/testfile1.txt hdfs://master:8020/ [rootmaster ~]# hdfs dfs -ls / Found 8 items drwxr-xr-x - root supergroup 0 2026-04-27 00:30 /Tests drwxr-xr-x - root supergroup 0 2026-05-14 09:12 /data drwxr-xr-x - root supergroup 0 2026-05-17 23:10 /home drwxr-xr-x - root supergroup 0 2026-05-17 23:06 /itheima -rw-r--r-- 2 root supergroup 11 2026-04-23 09:38 /test.txt -rw-r--r-- 2 root supergroup 44 2026-05-17 23:31 /testfile1.txt drwxrwx--- - root supergroup 0 2026-04-18 02:27 /tmp drwxr-xr-x - root supergroup 0 2026-05-14 08:58 /user [rootmaster ~]# hdfs dfs -cat /testfile1.txt # 不带协议头默认操作HDFS文件系统 itheima itcast hahaha heiheihei bigdata 666 [rootmaster ~]#注若带协议头~不会被bash展开于是HDFS命令把~当成了普通字符路径就会出错或者显示没有此文件[rootmaster ~]# vim testfile2.txt [rootmaster ~]# cat testfile2.txt happy delight glad joyful cheerful thrill [rootmaster ~]# hdfs dfs -put ./testfile2.txt / # 将当前目录下的testfile2.txt 上传到HDFS中根目录/下 [rootmaster ~]# hdfs dfs -ls / Found 9 items drwxr-xr-x - root supergroup 0 2026-04-27 00:30 /Tests drwxr-xr-x - root supergroup 0 2026-05-14 09:12 /data drwxr-xr-x - root supergroup 0 2026-05-17 23:10 /home drwxr-xr-x - root supergroup 0 2026-05-17 23:06 /itheima -rw-r--r-- 2 root supergroup 11 2026-04-23 09:38 /test.txt -rw-r--r-- 2 root supergroup 44 2026-05-17 23:31 /testfile1.txt -rw-r--r-- 2 root supergroup 42 2026-05-17 23:38 /testfile2.txt drwxrwx--- - root supergroup 0 2026-04-18 02:27 /tmp drwxr-xr-x - root supergroup 0 2026-05-14 08:58 /user [rootmaster ~]# hdfs dfs -cat /testfile2.txt happy delight glad joyful cheerful thrill[rootmaster ~]# cat testfile2.txt testfile1.txt # 将testfile2.txt的内容追加的testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# ls -l testfile1.txt # 查看Linux本地刚追加完的testfile1.txt的大小 -rw-r--r--. 1 root root 2144 May 17 23:41 testfile1.txt # 2144字节 [rootmaster ~]# ls -lh testfile1.txt #-h选项更加人性化的展示文件大小2.1KB -rw-r--r--. 1 root root 2.1K May 17 23:41 testfile1.txt [rootmaster ~]# cat testfile1.txt # 查看文件内容 itheima itcast hahaha heiheihei bigdata 666 happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill [rootmaster ~]# hdfs dfs -ls /testfile1.txt # 普通查看HDFS中testfile1.txt的大小44字节 -rw-r--r-- 2 root supergroup 44 2026-05-17 23:31 /testfile1.txt [rootmaster ~]# hdfs dfs -ls -h /testfile1.txt # 人性化查看还是44字节 -rw-r--r-- 2 root supergroup 44 2026-05-17 23:31 /testfile1.txt [rootmaster ~]# hadoop fs -put testfile1.txt / # 上传已经追加完的文件 put: /testfile1.txt: File exists # HDFS中已经存在该文件上传失败 [rootmaster ~]# hadoop fs -put -f testfile1.txt / # 使用-f选项强制覆盖式上传 [rootmaster ~]# hadoop fs -ls /testfile1.txt # 发现文件变大了文件修改的时间也更新了 -rw-r--r-- 2 root supergroup 2144 2026-05-17 23:44 /testfile1.txt [rootmaster ~]# hdfs dfs -ls -h /testfile1.txt # 人性化2.1KB -rw-r--r-- 2 root supergroup 2.1 K 2026-05-17 23:44 /testfile1.txt查看HDFS中文件的内容hadoop fs -cat src hdfs dfs -cat src读取大文件时可以使用管道符配合more命令(more命令用于对内容进行翻页)hadoop fs -cat src | more hdfs dfs -cat src | more[rootmaster ~]# hdfs dfs -cat /testfile1.txt itheima itcast hahaha heiheihei bigdata 666 happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill [rootmaster ~]# hadoop fs -cat /testfile1.txt | more itheima itcast hahaha heiheihei bigdata 666 happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy --More--出现“--More--”时按空格键就能翻页HDFS中下载文件到Linux本地hadoop fs -get [-f] [-p] src localdst hdfs dfs -get [-f] [-p] src localdst 解释 -f表示Linux本地路径下已经存在了这个文件但是强制覆盖它 -p表示保留访问和修改等信息[rootmaster ~]# ls anaconda-ks.cfg getJPSInfo.sh hadoop-3.1.4.tar.gz manage_firewall.sh startHadoop.sh stopHadoop.sh testfile1.txt testfile2.txt [rootmaster ~]# rm testfile1.txt testfile2.txt rm: remove regular file ‘testfile1.txt’? y rm: remove regular file ‘testfile2.txt’? y [rootmaster ~]# ls anaconda-ks.cfg getJPSInfo.sh hadoop-3.1.4.tar.gz manage_firewall.sh startHadoop.sh stopHadoop.sh [rootmaster ~]# hadoop fs -get /testfile2.txt . #.表示当前目录 [rootmaster ~]# ls anaconda-ks.cfg getJPSInfo.sh hadoop-3.1.4.tar.gz manage_firewall.sh startHadoop.sh stopHadoop.sh testfile2.txt [rootmaster ~]# cat testfile2.txt happy delight glad joyful cheerful thrillHDFS中拷贝文件HDFS删除文件或目录[rootmaster ~]# hdfs dfs -rm -r /Tests/* rm: /Tests/practice.txt: No such file or directory rm: /Tests/test1.sh: No such file or directory rm: /Tests/test2.sh: No such file or directory rm: /Tests/test3.sh: No such file or directory rm: /Tests/test.sh: No such file or directory [rootmaster ~]# hdfs dfs -ls /Tests Found 3 items -rw-r--r-- 2 root supergroup 178 2026-04-27 00:01 /Tests/data1.txt -rw-r--r-- 2 root supergroup 293 2026-04-27 00:30 /Tests/data2.txt -rw-r--r-- 2 root supergroup 0 2026-04-26 23:51 /Tests/input.txt [rootmaster ~]# hdfs dfs -rm /Tests/* Deleted /Tests/data1.txt Deleted /Tests/data2.txt Deleted /Tests/input.txt [rootmaster ~]# hdfs dfs -ls /Tests [rootmaster ~]#第一次删除失败的原因*通配符没有正确传递给hdfs dfs命令而是在当前的Linux Shellbash中被展开了也就是说在执行命令时Shell先尝试把/Tests/*匹配成本地Linux系统中的文件但是在本地Linux中的/Tests目录下没有子目录了你使用参数-r是递归删除目录的。但这还不是主要原因主要原因是Shell匹配到了本地的/Tests下的文件后把匹配到的内容作为参数传递给hdfs这个程序hdfs接收到这些参数后但是在HDFS系统中不存在Linux本地的/Tests目录下的文件如/Tests/data1.txt等所以Shell在HDFS系统中找不到目标文件就会报错[rootmaster ~]# ls /Tests practice.txt test1.sh test2.sh test3.sh test.sh [rootmaster ~]# ls -l /Tests total 20 -rw-r--r--. 1 root root 279 May 7 16:20 practice.txt -rw-r--r--. 1 root root 37 May 8 17:09 test1.sh -rw-r--r--. 1 root root 36 May 8 17:18 test2.sh -rwxrw---x. 1 root root 36 May 8 17:22 test3.sh -rw-r--r--. 1 root root 88 May 8 16:52 test.sh给路径加上引号就不会被展示成本地Linux的路径了
http://www.rkmt.cn/news/1389346.html

相关文章:

  • 8大网盘直链下载助手:免费获取真实下载链接的完整解决方案
  • av1编码--超级块、编码块概念
  • 如何轻松突破30+文档平台限制:免费下载工具kill-doc完整指南
  • 如何快速配置本地语音识别:3步实现离线实时转写
  • 从豆种到玻璃种,你的翡翠耳饰值多少钱?
  • 2026年武汉微电影制作公司TOP5权威排行榜,哪家才是你的心头好? - 企业推荐官
  • 电子电路基础与原理图解析
  • 角谷猜想熵增定律的进一步细思极好--首尾概率等效原理
  • 统信UOS服务器SSH深度加固:从漏洞原理到生产热修复
  • Claude Code 安装教程(免费使用deepseek-V4-pro模型)
  • Spark 内核运行机制与原理深度解析
  • 激光二极管(LD)驱动器的嵌入式控制系统
  • Excel批量查询终极指南:100+文件秒级检索,工作效率提升10倍
  • 城市供热信创升级|亚控KingSCADA实现国产化+智慧化双突破
  • AUTOSAR CP COM 模块深度实践:信号编解码、收发调度与超时监控落地
  • 构建生产级本地 AI 搜索引擎:Python + Ollama + 混合检索 + RAG 的架构深潜与工程落地
  • 3分钟上手!XXMI启动器:免费开源的多游戏模组管理终极方案
  • Deepin Boot Maker:跨平台启动盘制作工具的技术架构与实践指南
  • 5个步骤快速掌握AMD锐龙SMU调试工具:免费硬件优化终极指南
  • ComfyUI ReActor Node:如何在ComfyUI中实现高效面部交换的完整指南
  • 快手Android端__nstokensig与sig签名算法逆向实战解析
  • 百考通AI智能梳理研究演进,精准定位文献综述
  • DeepL Chrome翻译插件:5个步骤实现浏览器内专业级翻译体验
  • 别再搞混了!GNURadio里Interpolation和Decimation滤波器的正确使用顺序(附实战避坑)
  • 终极Switch游戏安装指南:Awoo Installer完整使用教程
  • 破解90%完成悖论:从认知偏差到系统实践的项目交付指南
  • SMU调试工具:如何解决AMD Ryzen系统稳定性问题 - 5个实用技巧
  • 如何3步实现专业级PNG到SVG矢量转换:vectorizer工具深度解析
  • 怎么导出豆包聊天记录
  • Thorium浏览器终极指南:为什么这个基于Chromium的性能怪兽值得立即尝试?