熟悉常用的HDFS操作

一、Hadoop提供的Shell命令完成相同任务:

1.在本地Linux文件系统的“ / home / hadoop /”目录下创建一个文件txt,里面可以随意输入一些单词.
2.在本地查看文件位置(ls)
3.在本地显示文件内容
1
2
3
cd / home / hadoop /
touch test.txt
cat temp.txt

4.使用命令把本地文件系统中的“txt”上传到HDFS中的当前用户目录的input目录下。

5.查看hdfs中的文件(-ls)

1
2
3
cd / usr / local / hadoop
./ bin / hdfs dfs - put / home / hadoop / test.txt input
./ bin / hdfs dfs - ls input

6.显示hdfs中该的文件内容

7.删除本地的txt文件并查看目录

1
2
3
./ bin / hdfs dfs - cat input / test.txt
rm - rf test.txt
ls

8.从hdfs中将txt下载地本地原来的位置。

9.从hdfs中删除txt并查看目录
1
2
3
./ bin / hdfs dfs - get input / text.txt / home / hadoop
./ bin / hdfs dfs - rm - r input / text.txt
./ bin / hdfs dfs - ls input

二、

向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,由用户指定是追加到原有文件末尾还是覆盖原有的文件;
1
2
3
4
if $(hdfs dfs -test -e test.txt);
then $(hdfs dfs -appendToFile local.txt test.txt);
else $(hdfs dfs -copyFromLocal -f local.txt test.txt);
fi

从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名;
1
2
3
4
if $(hdfs dfs -test -e file:
    then $(hdfs dfs -copyToLocal hello.txt./ test2.txt);
else $(hdfs dfs -copyToLocal hello.txt./ test.txt);
fi
将HDFS中指定文件的内容输出到终端中;
1
hadoop fs - cat / usr / local / hadoop / test.txt

显示HDFS中指定的文件的读写权限、大小、创建时间、路径等信息;
1
hadoop fs - ls - h / usr / local / hadoop / test.txt

给定HDFS中某一个目录,输出该目录下的所有文件的读写权限、大小、创建时间、路径等信息,如果该文件是目录,则递归输出该目录下所有文件相关信息;
1
hadoop fs - ls - R - h / usr / local / hadoop

提供一个HDFS内的文件的路径,对该文件进行创建和删除操作。如果文件所在目录不存在,则自动创建目录;
1
2
3
4
if $(hadoop fs -test -d / usr / local / hadoop / test);
then $(hadoop fs -touchz / usr / local / hadoop / test / test1.txt);
else $(hadoop fs -mkdir -p / usr / local / hadoop / test & & hadoop fs -touchz / usr / local / hadoop / test / test1.txt);
fi

提供一个HDFS的目录的路径,对该目录进行创建和删除操作。创建目录时,如果目录文件所在目录不存在则自动创建相应目录;删除目录时,由用户指定当该目录不为空时是否还删除该目录;
1
hadoop fs -rmr / usr / local / hadoop / test

向HDFS中指定的文件追加内容,由用户指定内容追加到原有文件的开头或结尾;
1
hadoop fs -appendToFile local.txt test.txt

删除HDFS中指定的文件;
1
hadoop fs -rm / usr / local / hadoop / test.txt

删除HDFS中指定的目录,由用户指定目录中如果存在文件时是否删除目录;
1
hadoop fs -rmr / usr / local / hadoop

在HDFS中,将文件从源路径移动到目的路径。

1
hadoop fs -mv / usr / local / hadoop / test.txt / usr / local / hadoop / hadoop_tmp / test.txt
posted @   206梁荣臻  阅读(153)  评论(0编辑  收藏  举报
编辑推荐:
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
阅读排行:
· 本地部署 DeepSeek:小白也能轻松搞定!
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 从 Windows Forms 到微服务的经验教训
· 李飞飞的50美金比肩DeepSeek把CEO忽悠瘸了,倒霉的却是程序员
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee
点击右上角即可分享
微信分享提示