shell呼叫mapreduce無法執行後續指令碼問題
阿新 • • 發佈:2019-02-15
把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;