前情提要
在日常集群的跑数据中,时常出现跑的过程之中出现各种情况,导致一些日期的数据没有跑成功。
而每日跑的表也是非常的多,所以有时候一张一张的去看哪些表缺数据,也会将自己的头脑弄晕。
所以想着,自己写一个脚本,然后批量的检测哪些数据目录为空。
脚本功能说明
输入一个查询的截止日期(例如:20161115),从截止日期当月1号(例子:20161101)开始遍历各表路径,如果路径不存在,就表示当日缺少数据。
输出缺少日期的表的路径,以及缺数据的日期。
需要的信息
整个集群跑出来的数据保存在HDFS上面,所以我们看数据是否跑空或则有错误的时候,就使用hadoop shell的方式去查看该当日路径(每日任务都是分区表,以日期分区)是否存在。
hadoop fs -du TABLE_LOCATION/$RECEIVE_DAY
所以我们需要知道的关键信息就是表的路径
使用:
hadoop fs -test -e TABLE_LOCATION/$RECEIVE_DAY
可以查看该目录是否存在,如果存在会return 0
返回的数值,我们使用linux的$?来读取,结合if判断来确定文件路径是否存在。
$?
脚本部分
#!/bin/bash
#PROGRAM:
# 检查日汇总表格,当月1日至截至日期跑数据情况
#HISTROY:
# 20161115 L.Z created
#####
#**设置好各表的路径
#####
#DIR(0)=table1
loc_table1=table1_location
#DIR(1)=table2
loc_table2=table2_location
#DIR(2)=table3
loc_table3=table3_location
#DIR(3)=table4
loc_table4=table4_location
#DIR(4)=table5
loc_table5=table5_location
#DIR(5)=table6
loc_table6=table6_location
#DIR(6)=table7
loc_table7=table7_location
#DIR(7)=table8
loc_table8=table8_location
#使用数组装整个目录列表
DIR=(${loc_table1} ${loc_table2} ${loc_table3} ${loc_table4} ${loc_table5} ${loc_table6} ${loc_table7} ${loc_table8})
DIR_LEN=${#DIR[*]}
#######
#**获取查询时间
#######
read -p "输入本月查询数据的截至日期:" end_day
start_day=${end_day:0:6}01
echo -e "将从${start_day}日数据开始检查"
######
#**检查是否存在
######
#清空上次保存的数据
true>checkindata.txt
#使用循环依次读取表的路径,再循环检测该日期下的目录是否存在
for((i=0;${i}<${DIR_LEN};i++))
do
dir_path=${DIR[i]}
echo -e "检测路径:${DIR[i]}"
echo -e "检测路径:${DIR[i]}\
">>checkindata.txt
for ((data_day=${start_day};$data_day<=$end_day;data_day=$data_day+1))
do
hadoop fs -test -e "${DIR[i]}/$data_day/"
if [ $? -eq 0 ];
then
echo -e "$data_day日数据存在"
else
echo -e "$data_day日数据不存在"
echo -e "$data_day" >>checkindata.txt
fi
done;
done;
输出结果:
/table1_location
20161113
/table2_location
20161105
20161113
/table3_location
20161105
20161113
/table4_location
20161105
20161113
/table5_location
/table6_location
/table7_location
/table8_location
不足
- 不能检测到数据路径存在,大小却为0的情况
此脚本只能检测到表的路径是否存在,对于表的路径存在,但是数据大小却为0的情况。这种情况也是需要补数据的。这个时候可以使用需要使用:
hadoop fs -test -z LOCATION/$RECEIVE_DAY
替换掉脚本中的
hadoop fs -test -e LOCATION/$RECEIVE_DAY
来实现。
- 不能检测数据是否出错。
虽然大数据的特性之一是容错性高,但是会发生当日整天数据虽然都跑好了,但是却大批量跑错地情况。所以日常人工定期的检查数据是否跑错也是非常重要的。
20161121更新:
hadoop fs -test -z 是测试文件的大小是否为0bytes,不是文件路径~
so 这个方法行不通
20161208更新:
后来为了检测文件路径是否是0的情况,我使用grep然后再cut来获取文件目录的大小。就解决了不能测试路径大小是否为0这种情况了。
test_num=`hadoop fs -du "${DIR[i]}/"|grep "$data_day" |cut -d " " -f1`
echo $test_num
if [ "$test_num" == '0' ];
then
echo -e "$data_day日数据为空"
echo -e "$data_day为空" >>checkindata_e0.txt
fi