自己编写接口用于获取Hadoop Job conf 信息

55 篇文章 3 订阅


Hadoop Job完成后可以设置回调接口,一个自定义的URL,比如我的:

 

http://x.x.x.x/log/notify/stat_job/{jobId}/{jobStatus}

 

之后我在Servlet中可以拿到jobId,通过jobId,就可以拿到Job对象(RunningJob),代码如下:

 

Java代码   收藏代码
  1. public static RunningJob getJobById(String jobId) {  
  2.     Configuration conf = new Configuration();  
  3.     conf.set("mapred.job.tracker", Constants.MAP_REDUCE_URL);  
  4.     JobClient client;  
  5.     try {  
  6.         client = new JobClient(new JobConf(conf));  
  7.         return client.getJob(jobId);  
  8.     } catch (IOException e) {  
  9.         throw new RuntimeException(e);  
  10.     }  
  11. }  
 

关键是这个RunningJob对象只可以获取jobname信息,但是无法获取我们设置的conf信息。为了解决这个问题,我写了一个jsp,放到namenode上,用于读取本地log文件,并将结果反馈给调用者。代码如下,希望对大家有帮助:

 

Java代码   收藏代码
  1. <%@ page  
  2.   contentType="text/xml; charset=UTF-8"  
  3.   import="javax.servlet.*"  
  4.   import="javax.servlet.http.*"  
  5.   import="java.io.*"  
  6.   import="java.net.URL"  
  7.   import="org.apache.hadoop.util.*"  
  8.   import="javax.servlet.jsp.JspWriter"  
  9. %><%!   
  10.     private static final long serialVersionUID = 1L;  
  11.   
  12.         public File getHistoryFile(final String jobId) {  
  13.                 File dir = new File("/opt/hadoop/logs/history/done/");  
  14.   
  15.                 File[] files = dir.listFiles(new FilenameFilter() {  
  16.                         public boolean accept(File dir, String name) {  
  17.                 if (name.indexOf(jobId) >= 0 && name.endsWith(".xml")) {  
  18.                     return true;  
  19.                 }  
  20.                                 return false;  
  21.                         }  
  22.                 });  
  23.   
  24.                 if (files != null && files.length > 0) {  
  25.                         return files[0];  
  26.                 }  
  27.                 return null;  
  28.         }  
  29.   
  30.     public void printXML(JspWriter out, String jobId) throws IOException {  
  31.         FileInputStream jobFile = null;  
  32.         String[] outputKeys = { "db_id""channel_id""channel_name""user_id""user_name""job_day""pub_format_type""mapred.output.dir" };  
  33.         String line = "";  
  34.         try {  
  35.             jobFile = new FileInputStream(getLogFilePath(jobId));  
  36.             BufferedReader reader = new BufferedReader(new InputStreamReader(jobFile));  
  37.   
  38.             while ((line = reader.readLine()) != null) {  
  39.                 for (String key : outputKeys) {  
  40.                     if (!line.startsWith("<property>") || line.indexOf("<name>" + key + "</name>") >= 0) {  
  41.                         out.println(line);  
  42.                         break;  
  43.                     }  
  44.                 }  
  45.             }  
  46.         } catch (Exception e) {  
  47.             out.println("Failed to retreive job configuration for job '" + jobId + "!");  
  48.             out.println(e);  
  49.         } finally {  
  50.             if (jobFile != null) {  
  51.                 try {  
  52.                     jobFile.close();  
  53.                 } catch (IOException e) {  
  54.                 }  
  55.             }  
  56.         }  
  57.     }  
  58.   
  59.     private File getLogFilePath(String jobId) {  
  60.         String logDir = System.getProperty("hadoop.log.dir");  
  61.         if (logDir == null || logDir.length() == 0) {  
  62.             logDir = "/opt/hadoop/logs/";  
  63.         }  
  64.         File logFile = new File(logDir + File.separator + jobId + "_conf.xml");  
  65.         return logFile.exists() ? logFile : getHistoryFile(jobId);  
  66.     }  
  67.   
  68. %><%  
  69.   response.setContentType("text/xml");  
  70.   final String jobId = request.getParameter("jobid");  
  71.   if (jobId == null) {  
  72.     out.println("<h2>Missing 'jobid' for fetching job configuration!</h2>");  
  73.     return;  
  74.   }  
  75.   
  76.   printXML(out, jobId);  
  77.   
  78. %>  
 

    这里有个要点,运行中和刚完成的job xml文件放到了"/opt/hadoop/logs"下,归档的job xml放到了“/opt/hadoop/logs/history/done/”,所以要判断第一个地方找不到文件,去第二个地方找。

 

    功能很简单,但是很有用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值