input=$1 #获得第一个参数,也就是需要上传的文件
filename=$(basename $input) #获得该文件的文件名
dst="/root/update" #上传的文件目录
if `/root/rDesk/hadoop-3.3.2/bin/hadoop fs -test -e $dst"/"$filename` ;then
echo "该文件已存在,是否追加到该文件尾部“
echo "追加到尾部:y 覆盖源文件:n”
read opt
case $opt in
[yY]*)
`/root/rDesk/hadoop-3.3.2/bin/hadoop fs -appendToFile $input $dst"/"$filename`
;;
[nN]*)
`/root/rDesk/hadoop-3.3.2/bin/hadoop fs -rm $dst"/"$filename`
`/root/rDesk/hadoop-3.3.2/bin/hadoop fs -put $input $dst`
;;
*)
echo "input err, shell out"
;;
esac
else
`/root/rDesk/hadoop-3.3.2/bin/hadoop fs -put $intput $dst`
fi
echo "the shell is completed"
downloadPath=$1 #HDFS中下载的文件的路径
savePath=$2 #保存在本地的路径
downloadFilename=$(basename $downloadPath) #下载的文件的文件名
nowtime=$(date "+%Y%m%d%H%M%S") #获取当前时间
if [ -e $savePath"/"$downloadFilename ]; then
#如果存在相同文件名文件,则以当前时间当作文件名
`/root/rDesk/hadoop-3.3.2/bin/hadoop fs -get $downloadPath $savePath"/"$nowtime`
echo "the original filename is existed, now the filename is $savePath"/"$nowtime"
else
`/root/rDesk/hadoop-3.3.2/bin/hadoop fs -get $downloadPath $savePath`
fi
echo "copyOver"
将hdfs系统下/root/update/tag.txt文件复制到当前目录(./)
filePath=$1 #获得需要指定的HDFS中的文件路径
#echo $filePath
echo `/root/rDesk/hadoop-3.3.2/bin/hadoop fs -cat $filePath`
bin/hadoop fs -ls /root
or
#codecodecoderr
echo `/root/rDesk/hadoop-3.3.2/bin/hadoop fs -ls $1`
/root/rDesk/hadoop-3.3.2/bin/hadoop fs -ls -R /root
#codecodecoderr
path=$1 #获取指定的文件路径
if `/root/rDesk/hadoop-3.3.2/bin/hadoop fs -test -e $path`;then
echo "the file is exist"
else
echo "the file is not exist"
`/root/rDesk/hadoop-3.3.2/bin/hadoop fs -mkdir $path`
echo "the dir is created"
fi
bin/hadoop fs -mkdir -p /root/update/a/b/c
bin/hadoop fs -rmdir /root/update/a
当该文件夹不为空任然删除
bin/hadoop fs -rm -r /root/update/a
hdfsPath=$2 #HDFS中指定文件的路径
localPath=./tmp__ #临时本地文件存储路径
type=$1 #操作类型 -h表示追加到开头,-t表示追加到末尾
#判断是否该目录下是否存在tmp__文件,存在删除
if [ -e $localPath ];then
`rm -rf $localPath`
fi
case $type in
-h)
#追加到开头,操作流程:先将HDFS指定文件复制到本地,使用sed命令在开头写入内容,然后删除HDFS的文件,再将编辑好的文件上传到HDFS
`/root/rDesk/hadoop-3.3.2/bin/hadoop fs -copyToLocal $hdfsPath $localPath`
read -p "输入需要插入的内容:" text
`sed -i "1 i $text" $localPath`
`/root/rDesk/hadoop-3.3.2/bin/hadoop fs -rm $hdfsPath`
`/root/rDesk/hadoop-3.3.2/bin/hadoop fs -put $localPath $hdfsPath`
`rm -rf $localPath`
;;
-t)
#策略:以输入的文件内容在本地临时创建一个文件,然后使用appendToFile命令将该文件内容追加到HDFS指定文件末尾
read -p "输入需要追加到末尾的内容:" text
`echo $text > $localPath`
`/root/rDesk/hadoop-3.3.2/bin/hadoop fs -appendToFile $localPath $hdfsPath`
`rm -rf $localPath`
;;
*)
echo "llxxx操作数错误xxxll"
;;
esac
echo "Over"
删除指定文件
bin/hadoop fs -rm /root/update/tag.txt
删除指定空目录
bin/hadoop fs -rmdir /root/update/new
bin/hadoop fs -cp /root/update/tag.txt /root
https://www.jianshu.com/p/52e16e9ce24e
https://blog.csdn.net/weixin_43215948/article/details/107290768
http://c.biancheng.net/view/2767.html
https://blog.csdn.net/Olivia_Vang/article/details/104081096
https://blog.csdn.net/weixin_39962758/article/details/116929907