把mapreduce打成jar包然后用shell去调用,但是mapreduce执行结束后,总是卡在那里不会继续往下执行,今天写了个循环来检测,可以达到目的。
#!/bin/sh
set -x
deal_date=${1:-`date --date '1 days ago' +%Y%m%d`}
deal_date=20140616
hadoop fs -rmr /user/hdfs/result/TermActiveJob/${deal_date}/
java -classpath /home/mapreduceProgram/dotStat/dotstat_v2-0.0.1-SNAPSHOT.jar com.winksi.dotstat.TermActiveJob ${deal_date} &
# &符号是让上面的语句在后台执行,然后无间歇的执行下面的语句
while true
do
sleep 1s #1秒钟循环一次
hadoop fs -test -e /user/hdfs/result/TermActiveJob/${deal_date}/part* #该文件时mapreduce执行结束后生成的文件
if [ $? -ne 0 ]; then
echo "Directory not exists!"
else
echo "it is exist!"
ps -ef |grep TermActiveJob |awk '{print $2}' |while read pid #如果生成的文件存在,那么就杀死mapreduce的进程
do
kill -9 $pid
done
#进行后续的收尾操作
mkdir /root/hanfeng/shell/TermActiveJob
cd /root/hanfeng/shell/TermActiveJob
rm -rf *
hadoop fs -get /user/hdfs/result/TermActiveJob/${deal_date}/part*
cat part* > result.txt
echo "DELETE FROM term_active wHERE cur_time=${deal_date} ;" | mysql -h172.16.1.81 -P3308 -uadmin -ptonggangdasha reportdb
echo "LOAD DATA local INFILE 'result.txt' INTO TABLE term_active FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (adccompany,cityId,av,model,num,cur_time) ;" | mysql -h172.16.1.81 -P3308 -uadmin -ptonggangdasha reportdb;
break
fi
done
echo "Finish"
------------------------------------------------------------------------------------------------------------------------
重大发现:在老大的指点下,发现执行mapreduce无返回,是我代码的问题,后来经改正,代码如下:
public class ZdwsCombineJob extends Configured implements Tool{ //我之前就是没有实现Configured 和Tool,而是直接用main函数执行JOB,才不能结束的
public static String date;
public static String month;
private int reducernum;
public ZdwsCombineJob(int i){
reducernum = i;
}
public static void main(String argv[]) throws Exception{
if(argv.length != 0){
date = argv[0];
month = date.toString().substring(0, 6);
}else{
Date yesterdayDate = new Date(new Date().getTime() - 1*24*60*60*1000);
date = DateUtils.getDotActionDate(yesterdayDate); //20140501
month = DateUtils.getCurrentMonth(yesterdayDate);
}
//这里需要用线程来启动任务,执行结束后才能退出
int exit = ToolRunner.run(new ZdwsCombineJob(48), argv);
System.exit(exit);
}
@Override
public int run(String[] args) throws Exception {
Configuration conf = getConf();
conf.set("fs.default.name", "hdfs://172.16.1.50:8020");
String input="/user/hdfs/source/db/cust_phone/cust_phone.txt";
StringBuffer sb = new StringBuffer();
sb.append("/user/hdfs/source/log/qymp/").append(month).append("/qymp_").append(date).append(".dat");
String output="/user/hdfs/combine/ZdwsCombine/"+date+"/";
Job job = new Job(conf, "ZdwsCombine");
job.setJarByClass(ZdwsCombineJob.class);
job.setMapperClass(ZdwsCombineMapper.class);
job.setReducerClass(ZdwsCombineReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(input));
FileInputFormat.addInputPath(job, new Path(sb.toString()));
FileOutputFormat.setOutputPath(job, new Path(output));
job.setNumReduceTasks(reducernum);
job.waitForCompletion(true) ;
return 0;
}
}
这样的话,直接shell调用就可以了:
#!/bin/sh
set -x
deal_date=${1:-`date --date '1 days ago' +%Y%m%d`}
deal_date=20140617
hadoop fs -rmr /user/hdfs/combine/ZdwsCombine/${deal_date}/
java -classpath /home/mapreduceProgram/dotStat/dotstat_v2-0.0.1-SNAPSHOT.jar com.winksi.dotstat.ZdwsCombineJob ${deal_date}
mkdir /root/hanfeng/shell/ZdwsCombineJob
cd /root/hanfeng/shell/ZdwsCombineJob
rm -rf *
hive -S -e "alter table call_show_zdws add partition (dt='${deal_date}') location '/user/hdfs/combine/ZdwsCombine/${deal_date}'"
hive -S -e 'select phone,adccompany,cur_time,ip,model,av,enterpriseId,count(*),count(distinct(imsi)) from call_show_zdws where dt='${deal_date}' group by phone,adccompany,cur_time,ip,model,av,enterpriseId' > result.txt
echo "DELETE FROM call_show_zdws wHERE cur_time=${deal_date} ;" | mysql -h172.16.1.81 -P3308 -uadmin -ptonggangdasha reportdb
echo "LOAD DATA local INFILE 'result.txt' INTO TABLE call_show_zdws FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' (phone,adccompany,cur_time,cityId,model,av,enterpriseId,pv,uv) ;" | mysql -h172.16.1.81 -P3308 -uadmin -ptonggangdasha reportdb;