平常脚本运行需要检查对应hdfs路径相关信息,总结一下:
Tip: 假设要检查的路径为check_path
1.获取路径文件大小并转化为规定单位 Byte,K,M,G
bytes获取文件大小,单位为字节;base为转换单位的基准,我这里取1G,所以是1x1024x1024x1024bytes,其他单位转换同理;第三步real只需要做除法即可,bc是shell自带运算处理,这里做2位小数保留,其余同理
#获取大小
bytes=`hadoop fs -du -s $check_path|awk -F " " '{print $1}'`
base=1073741824
real=`echo "scale=2; $bytes/$base" | bc`
2.检查文件是否存在
#检查路径是否存在
function check_exist(){
hadoop fs -test -e $check_path
if [ $? -eq 0 ] ;then
echo 'path exist'
else
echo 'Error! path is not exist'
fi
}
check_exist
3.检查是否为文件夹
#检测路径是否为文件夹
function check_isDir(){
hadoop fs -test -d $check_path
if [ $? -eq 0 ] ;then
echo 'Is a directory'
else
echo 'Is not a directory'
fi
}
4.检查是否为文件
#检测路径是否为📃
function check_isFile(){
hadoop fs -test -f $check_path
if [ $? -eq 0 ] ;then
echo 'Is a file'
else
echo 'Is not a file'
fi
}
5.检查文件是否超过指定大小
#检测📃大小是否超过超过阈值 默认byte
function check_Capacity(){
capacity=$1
hadoop fs -test -s $check_path
if [ $? -eq $capacity ] ;then
echo "超过"${capacity} " bytes/100 M"
else
echo "未超过"${capacity} " bytes/100 M"
fi
}
6.检查文件是否为空
#检测📃大小是否为空
function check_Empty(){
hadoop fs -test -z $check_path
if [ $? -eq 0 ] ;then
echo 'Is zero bytes in size.'
else
echo 'Is not zero bytes in size. '
fi
}
以上直接使用即可