平常腳本運行需要檢查對應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
}
以上直接使用即可