需求
需求:递归遍历hdfs的warehouse下所有.db目录下的所有表,判断每张表有没有数据,并将结果输出到文件中
数据有无的判断:
du -s -h 表的结果为:0 0 则hdfs上无数据
其他为hdfs上有数据
思路
遍历warehouse得到所有库 hadoop fs -ls /user/hive/warehouse
再次遍历所有以.db结尾的库的所有表 hadoop fs -ls /user/hive/warehouse/库
对所有目录(表)进行du操作并根据结果判断有无数据,打印到文件中
shell脚本
#! /bin/bash
function read_dir(){
for file1 in `hadoop fs -ls $1 | awk -F ' ' '{print $8}'` #注意此处这是两个反引号,表示运行系统命令
do
#echo $file1
if echo "$file1" | grep -q -E '\.db$' #如果是以.db为结尾的文件,那么进入循环
then
#echo '是以.db结尾的文件'
for file2 in `hadoop fs -ls $file1 | awk -F ' ' '{print $8}'` #遍历.db文件夹
do
#echo $file2
#res3=`hadoop fs -du -s -h $file2` #对目录调用hadoop语句
file3=`hadoop fs -du -s -h $file2 | awk -F ' ' '{print $NF}'`
siz=`hadoop fs -du -s -h $file2 | awk -F ' ' '{print $1}'`
if [[ 0 -eq $siz ]]
then
echo "$file3 hdfs上无数据" >> /root/temp/result_yhw.txt
else
echo "$file3 hdfs上有数据" >> /root/temp/result_yhw.txt
fi
done
else #如果不是,那么continue
#echo '不是以.db结尾的文件'
continue
fi
done
}
#读取第一个参数
read_dir $1
一些要点
- 截取字符串并获取文件全名
awk -F ’ ’ ‘{print $8}’ - 内容通过echo追加到文件中
echo “内容” >> 文件名 - shell中的linux命令要使用``引起来
- 如何判断字符串是否以.db为结尾?
if echo “$file1” | grep -q -E ‘.db$’ - 如何截取ls结果中的某列值?
hadoop fs -ls $1 | awk -F ’ ’ ‘{print $8}’ - 给一个变量赋值的时候,格式为:变量名=
linux语句
,其中,注意等号两边不能有空格 - 如何判断结果中的某列值是否为0?
[[ 0 -eq $siz ]]