HDFS编程实践Shell
# 先启动Hadoop
$ cd ~
$ start-all.sh
# 初始化用户文件夹
$ hdfs dfs –mkdir –p /user/qinphy
# 在本地创建测试文件
$ touch myFile.txt
$ vi myFile.txt
This is myFile
Hello HDFS!
$ touch localFile.txt
$ vi localFile.txt
This is localFile
Hello Hadoop!
-
向HDFS中上传任意文本文件,如果指定的文件在HDFS中已存在,由用户指定追加至文件末尾还是覆盖。
# 创建sh文件 touch test1.sh vi test1.sh # 编辑文件内容
if $(hadoop fs -test -e myFile.txt);then echo "文件存在:请选择 1.追加 2.覆盖" read num x=1 if [ $num -eq $x ];then $(hadoop fs -appendToFile myFile.txt /user/qinphy/myFile.txt); echo "文件已追加!" else $(hadoop fs -copyFromLocal -f myFile.txt /user/qinphy/myFile.txt); echo "文件已覆盖!" fi else $(hadoop fs -put myFile.txt /user/qinphy/myFile.txt); echo "文件已上传!" fi
# 执行命令 $ sh test1.sh 文件已经上传 $ hadoop fs -cat /user/qinphy/myFile.txt This is myFile Hello HDFS! $ sh test1.sh 文件存在:请选择 1.追加 2.覆盖 1 文件已追加 $ hadoop fs -cat /user/qinphy/myFile.txt This is myFile Hello HDFS! This is myFile Hello HDFS! $ sh test1.sh 文件存在:请选择 1.追加 2.覆盖 2 文件已覆盖 $ hadoop fs -cat /user/qinphy/myFile.txt This is myFile Hello HDFS!
-
从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名。
# 先移除原来的文件 $ rm -rf myFile.txt # 创建sh文件 $ touch test2.sh $ vi test2.sh
if [ ! -f "./myFile.txt" ]; then $(hadoop fs -copyToLocal /user/qinphy/myFile.txt ./myFile.txt); echo "下载了myFile.txt" else $(hadoop fs -copyToLocal /user/qinphy/myFile.txt ./myFile_1.txt); echo "已重命名myFile.txt" fi
$ sh test2.sh 下载了myFile.txt $ ls $ sh test2.sh 已重命名myFile.txt $ ls
-
将HDFS中指定文件的内容输出到终端中。
$ hadoop fs -cat /user/qinphy/myFile.txt This is myFile Hello HDFS!
-
显示HDFS中指定的文件的读写权限、大小、创建时间、路径等信息。
$ hadoop fs -ls -h /user/qinphy/myFile.txt -rw-r--r-- 2 qinphy supergroup 12 2020-04-20 11:56 /user/qinphy/myFile.txt
-
给定HDFS中某一个目录,输出该目录下的所有文件的读写权限、大小、创建时间、路径等信息,如果该文件是目录,则递归输出该目录下所有文件相关信息。
$ hadoop fs -ls -R -h /user
-
提供一个HDFS内的文件的路径,对该文件进行创建和删除操作。如果文件所在目录不存在,则自动创建目录。
$ touch test3.sh $ vi test3.sh
if $(hadoop fs -test -d /user/qinphy/input); then if $(hadoop fs -test -e input/myFile.txt); then $(hadoop fs -rm input/myFile.txt); echo "检测到myFile.txt,已删除!" else $(hadoop fs -touchz /user/qinphy/input/myFile.txt); echo "已创建myFile.txt" fi else $(hadoop fs -mkdir -p /user/qinphy/input && hdfs dfs -touchz /user/qinphy/input/myFile.txt); echo "不存在文件及路径,已创建" fi
$ sh test3.sh 不存在文件及路径,已创建 $ hadoop fs -ls -R -h /user/qinphy drwxr-xr-x - qinphy supergroup 0 2020-04-21 15:12 /user/qinphy/input -rw-r--r-- 2 qinphy supergroup 0 2020-04-21 15:12 /user/qinphy/input/myFile.txt -rw-r--r-- 2 qinphy supergroup 12 2020-04-21 14:55 /user/qinphy/myFile.txt $ sh test3.sh 检测到myFile.txt,已删除! drwxr-xr-x - qinphy supergroup 0 2020-04-21 15:13 /user/qinphy/input -rw-r--r-- 2 qinphy supergroup 12 2020-04-21 14:55 /user/qinphy/myFile.txt $ sh test3.sh 已创建myFile.txt drwxr-xr-x - qinphy supergroup 0 2020-04-21 15:14 /user/qinphy/input -rw-r--r-- 2 qinphy supergroup 0 2020-04-21 15:14 /user/qinphy/input/myFile.txt -rw-r--r-- 2 qinphy supergroup 12 2020-04-21 14:55 /user/qinphy/myFile.txt
-
提供一个HDFS的目录路径,对该目录进行创建和删除操作。创建目录时,如果目录文件所在目录不存在则自动创建相应目录;删除目录时,由用户指定不为空的目录是否删除。
$ touch test4.sh $ vi test4.sh
if $(hadoop fs -test -d /user/qinphy/input); then if $(hadoop fs -test -e /user/qinphy/input/myFile.txt); then echo "文件夹内存在文件,要删除吗?Y/N: " read an yes="Y" if [ $an == $yes ]; then $(hadoop fs -rm -R /user/qinphy/input); echo "已删除!" else echo "已取消操作。" fi else $(hadoop fs -rm -r /user/qinphy/input); echo "文件夹为空,已删除!" fi else $(hadoop fs -mkdir -p /user/qinphy/input); echo "文件夹不存在,已创建!" fi
$ sh test4.sh 文件夹内存在文件,要删除吗?Y/N: Y 已删除! $ hadoop fs -ls -R -h /user/qinphy -rw-r--r-- 2 qinphy supergroup 12 2020-04-21 14:55 /user/qinphy/myFile.txt $ sh test4.sh 文件夹不存在,已创建! $ hadoop fs -ls -R -h /user/qinphy drwxr-xr-x - qinphy supergroup 0 2020-04-21 16:03 /user/qinphy/input -rw-r--r-- 2 qinphy supergroup 12 2020-04-21 14:55 /user/qinphy/myFile.txt $ sh test4.sh 文件夹为空,已删除! $ hadoop fs -ls -R -h /user/qinphy -rw-r--r-- 2 qinphy supergroup 12 2020-04-21 14:55 /user/qinphy/myFile.txt
-
向HDFS中指定的文件追加内容,由用户指定内容追加到原有文件的开头或结尾。
# 追加在文件末尾 $ hadoop fs -appendToFile localFile.txt /user/qinphy/myFile.txt $ hadoop fs -cat /user/qinphy/myFile.txt This is myFile Hello HDFS! This is localFile Hello Hadoop! # 把localFile.txt内容追加到myFile.txt前面; $ rm -rf myFile.txt $ ls $ hadoop fs -get /user/qinphy/myFile.txt $ ls $ cat myFile.txt >> localFile.txt $ hadoop fs -copyFromLocal -f localFile.txt /user/qinphy/myFile.txt $ hadoop fs -cat /user/qinphy/myFile.txt This is localFile Hello Hadoop! This is myFile Hello HDFS!
-
删除HDFS中指定的文件。
$ hadoop fs -rm /user/qinphy/myFile.txt
-
在HDFS中将文件从源路径移动到目的路径。
$ hadoop fs -put myFile.txt
# 如果删除了input文件夹请添加:$ hadoop fs -mkdir input
$ hdfs dfs -mv /user/qinphy/myFile.txt /user/qinphy/input/myFile.txt
$ hadoop fs -cat /user/qinphy/input/myFile.txt