hadoop任務監控實現

來源:互聯網
上載者:User

hadoop1.0.4,struct2.3。

本工程仿效hadoop 50030監控介面,取得任務資訊,並顯示出來。工程可以在http://download.csdn.net/detail/fansy1990/6737451下載。

一、先看效果吧:

1. 正在啟動並執行任務:

從上面可以看到jobId為 job_201312181939_0002正在運行;

2. 運行失敗:


上面可以看到job_201312181939_0004運行失敗,其中的job_201312181939_0003因為輸出目錄重複,所以應該是沒有提交,則會跳過3,這個錯誤暫時沒有捕捉到;

3. 點擊 “點我吧” 可以回到首頁,如下:


點擊“開始監控吧”,跳轉到監控介面,如下:


這裡可以看到監控介面的列表還是10個,不過第一個任務id已經是最新的了;

二、下面是設計思路:

1. 首先是獲得最新的job資訊:

InetSocketAddress jobTracker=new InetSocketAddress(HOST,JOBTRACKER_PORT);jobClient=new JobClient(jobTracker, getConf());JobStatus[] jobStatusAll=jobClient.getAllJobs();
上面是獲得所有的job資訊(已經運行完成的),然後使用:

jobStatus=jobStatusAll[jobStatusAll.length-1];
就可以獲得最近完成的一個jobStatus了,但是這裡有個問題,就是如果叢集是第一次啟動,那麼jobStatusAll就是一個null,這樣的話就需要拼湊一個jobid了,但是lz還沒有找到方法;

2. 根據最新的jobid,來拼湊接下來的10個jobid,如下:

int id=jobStatus.getJobID().getId();log.info("initial monitorJobs with the start jobID :"+id);String jobIden=jobStatus.getJobID().getJtIdentifier();monitorJobs=new LinkedHashMap<String,JobInfo>();for(int i=0;i<jobNums;i++){String jobId= new JobID(jobIden,id+1+i).toString();monitorJobs.put(jobId, new JobInfo(jobId));}log.info("initial monitor jobs map done !!!");
3. 第一次點擊 “開始監控吧”,會提交到TransFormAction中;首先判斷monitorJObs是否有值:

if(MonitorUtil.monitorJobs!=null&&MonitorUtil.monitorJobs.size()>0){
沒有的話,就調用2.的初始化,然後顯示monitoring.jsp頁面:

<table class="table">    <caption>任務監控</caption>        <thead>            <tr>                <td id="tb-username">任務ID</td>                <td>任務名</td>                <td>map進度</td>                <td>reduce進度</td>                <td>任務狀態</td>            </tr>        </thead>        <tbody>        <s:iterator id="list" value="jobInfosList">            <tr>                <td>                <s:property value="#list.jobId" />                </td>                <td><s:property value="#list.jobName" /></td>                <td><s:property value="#list.mapProgress" /></td>                <td><s:property value="#list.redProgress" /></td>                <td><s:property value="#list.runState" /></td>            </tr>        </s:iterator>        </tbody>    </table>
這個頁面就是struct2的標籤 顯示資料,在這個頁面的最後,每隔3秒會向TransformAction提交一個請求:

<script type="text/javascript">delayURL("Monitor",3000);function delayURL(url, time) {     setTimeout("location.href='" + url + "'", time);}   </script>
4. TransFormAction再次接收到請求後,monitorJobs就會有值了,這是就會即時的取得最新的jobStatus:

JobStatus jobStatus= MonitorUtil.getNewJobStatus();
/** * 擷取最新的jobStatus * @return * @throws IOException */public static JobStatus getNewJobStatus() throws IOException{log.info("Getting the newest jobStatus ... ,job interval:"+JOB_INTERVAL);JobStatus[] jobStatusAll=new JobClient(new InetSocketAddress(HOST,JOBTRACKER_PORT), getConf()).getAllJobs();JobStatus jobStatus=jobStatusAll[jobStatusAll.length-1];return jobStatus;}
然後根據jobStatus的runState進行monitorJObs的更新:

/** * 判斷任務狀態 */if(MonitorUtil.monitorJobs.containsKey(jobStatus.getJobID().toString())){String jobName=MonitorUtil.jobClient.getJob(jobStatus.getJobID()).getJobName();if(jobStatus.getRunState()==JobStatus.RUNNING){log.info("jobid:"+jobStatus.getJobID().toString()+",status:"+JobStatus.RUNNING);MonitorUtil.monitorJobs.put(jobStatus.getJobID().toString(), new JobInfo(jobStatus.getJobID().toString(),jobName,jobStatus.mapProgress(),jobStatus.reduceProgress(),"running"));}else if(jobStatus.getRunState()==JobStatus.FAILED){log.info("jobid:"+jobStatus.getJobID().toString()+",status:"+JobStatus.FAILED);MonitorUtil.monitorJobs.put(jobStatus.getJobID().toString(), new JobInfo(jobStatus.getJobID().toString(),jobName,jobStatus.mapProgress(),jobStatus.reduceProgress(),"failed"));}else if(jobStatus.getRunState()==JobStatus.PREP){log.info("jobid:"+jobStatus.getJobID().toString()+",status:"+JobStatus.PREP);}else if(jobStatus.getRunState()==JobStatus.SUCCEEDED){log.info("jobid:"+jobStatus.getJobID().toString()+",status:"+JobStatus.SUCCEEDED);MonitorUtil.monitorJobs.put(jobStatus.getJobID().toString(), new JobInfo(jobStatus.getJobID().toString(),jobName,jobStatus.mapProgress(),jobStatus.reduceProgress(),"successed"));}else{log.info("unknown jobStatus:"+jobStatus.getRunState()+" ----------------------");}
最後返回給前台,同樣的顯示;

5. 當這裡10個任務都有資訊填充後,可以點擊“點我吧”,回到index.jsp頁面,這個頁面有一個初始化monitorJobs的方法:

<body>    <br>    <form action ="Monitor"><h3 align="center"><input type="submit" value="開始監控吧" /></h3>    </form>            <%    /**    * 初始化 monitorJobs    */    MonitorUtil.initialMonitorJobs();     %>  </body>
可以對monitorJobs進行重新初始化,這樣再次點擊“開始監控吧”就會獲得最新的資訊了。

三、運行方法:

1. 修改 MonitorUtil中的HOSTNAME變數為自己使用的雲平台的主機名稱(要在hosts檔案中配置哦);

2. 如果想修改顯示的列表行數,可以修改MonitorUtil的jobNums變數;

3. 瀏覽器中輸入http://localhost:8080/monitor 即可看到工程首頁,其中monitor是lz工程web根路徑;


如果您覺得lz的blog或者資源還ok的話,可以選擇給lz投一票,多謝。(投票地址:http://vote.blog.csdn.net/blogstaritem/blogstar2013/fansy1990 )



分享,成長,快樂

轉載請註明blog地址:http://blog.csdn.net/fansy1990

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.