为了可以方便地在hadoop的管理界面(namenode和jobtracker)中自定义展示项,使用代理servlet的方式实现了hadoop的管理界面。

首先,

在org.apache.hadoop.http.HttpServer中的构造函数public HttpServer(String name, String bindAddress, int port,boolean findPort, Configuration conf, AccessControlList adminsAcl,Connector connector)中添加如下代码,指定资源包和url前缀:

ServletHolder servletHoder = new ServletHolder();
servletHoder.setInitParameter("com.sun.jersey.config.property.packages","com.test.agent");
servletHoder.setServlet(new com.sun.jersey.spi.container.servlet.ServletContainer());
webAppContext.addServlet(servletHoder, "/agent/*");

之后,在包中定义了一个抽象对象AbstractResource 

public class AbstractResource {
protected JsonMapper jsonMapper = JsonMapper.nonDefaultMapper();
protected @Context
ServletContext context;
public JsonMapper getJsonMapper() {
return jsonMapper;
}
public void setJsonMapper(JsonMapper jsonMapper) {
this.jsonMapper = jsonMapper;
}
public ServletContext getContext() {
return context;
}
public void setContext(ServletContext context) {
this.context = context;
}
}

然后,真实的资源代码实现AbstractResource对象,对资源进行定义

@Path("/job")
public class JobResource extends AbstractResource { private DecimalFormat percentFormat = new DecimalFormat("##0.00");
private SimpleDateFormat dateFormat = new SimpleDateFormat(
"d-MMM-yyyy HH:mm:ss"); @Path("/clustersummary")
@GET
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String getClusterSummaryInfo() {
JobTracker jt = (JobTracker) context.getAttribute("job.tracker");
ClusterMetrics metrics = jt.getClusterMetrics();
String tasksPerNode = metrics.getTaskTrackerCount() > 0 ? percentFormat
.format(((double) (metrics.getMapSlotCapacity() + metrics
.getReduceSlotCapacity()))
/ metrics.getTaskTrackerCount()) : "-";
Map<String, String> summaryInfo = new LinkedHashMap<String, String>();
summaryInfo.put("usedHeapMemoryBytes",
Long.toString(Runtime.getRuntime().totalMemory()));
summaryInfo.put("totalHeapMemoryBytes",
Long.toString(Runtime.getRuntime().maxMemory()));
summaryInfo.put("runningMapTasks",
Integer.toString(metrics.getRunningMaps()));
summaryInfo.put("runningReduceTasks",
Integer.toString(metrics.getRunningReduces()));
summaryInfo.put("totalJobSubmissions",
Integer.toString(metrics.getTotalJobSubmissions()));
summaryInfo.put("numTotalTaskTrackers",
Integer.toString(metrics.getTaskTrackerCount()));
summaryInfo.put("occupiedMapSlots",
Integer.toString(metrics.getOccupiedMapSlots()));
summaryInfo.put("occupiedReduceSlots",
Integer.toString(metrics.getOccupiedReduceSlots()));
summaryInfo.put("reservedMapSlots",
Integer.toString(metrics.getReservedMapSlots()));
summaryInfo.put("reservedReduceSlots",
Integer.toString(metrics.getReservedReduceSlots()));
summaryInfo.put("mapTaskCapacity",
Integer.toString(metrics.getMapSlotCapacity()));
summaryInfo.put("reduceTaskCapacity",
Integer.toString(metrics.getReduceSlotCapacity()));
summaryInfo.put("avgTasksPerTaskTracker", tasksPerNode);
summaryInfo.put("numBlackListedTaskTrackers",
Integer.toString(metrics.getBlackListedTaskTrackerCount()));
summaryInfo.put("numGrayListedTaskTrackers",
Integer.toString(metrics.getGrayListedTaskTrackerCount()));
summaryInfo.put("numDecommissionedTaskTrackers",
Integer.toString(metrics.getDecommissionedTaskTrackerCount()));
return jsonMapper.toJson(summaryInfo); }
}

这样可以通过访问url进行访问,自定义管理界面访问的端口与原有的管理页面端口相同。JobTracker使用50030,NameNode使用50070,依次类推。具体见下单元测试代码:

public class JobResourceIntTest {
HttpClient client; @Before
public void setUp() {
client = new DefaultHttpClient();
} private String urlPrefix = "http://ip:50030/agent/job/"; private String jobId = "job_201405121106_0001";
private String tipId = "task_201405121106_0001_m_000000";
private String attemptId = "attempt_201405121106_0001_m_000000_0"; private String regex = "\"total\":\"(.+?)\",";
private Pattern p = Pattern.compile(regex); @Test
public void testClusterSummary() {
HttpGet clusterSummary = new HttpGet(urlPrefix + "clustersummary");
try {
HttpResponse response = client.execute(clusterSummary);
String clusterSummaryStr = EntityUtils.toString(response
.getEntity());
System.out.println(clusterSummaryStr);
Assert.assertTrue(clusterSummaryStr.contains("usedHeapMemoryBytes"));
Assert.assertTrue(clusterSummaryStr.contains("runningMapTasks"));
} catch (Exception e) {
e.printStackTrace();
}
clusterSummary.releaseConnection();
}
@After
public void tearDown() {
}
}

上述是后端的实现和单元测试,前端可根据需要进行资源获取和界面设计。 

最新文章

  1. Servlet-RequestDispatcher.forward方法
  2. Hibernate 和 快照
  3. CATransition(过渡)
  4. const修饰指针
  5. 解决CI框架的Disallowed Key Characters错误提示
  6. Sherlock and The Beast
  7. Android 中的图像特效(Matrix)
  8. Koajs原理
  9. 测试ODBC与OLE
  10. Java 基础(一)
  11. HTML5-常见的事件- DOMContentLoaded事件
  12. BZOJ 1196 [HNOI2006]公路修建问题(二分答案+并查集)
  13. Ozmosis实现BIOS直接启动Yosemite,基本完美
  14. JavaScript DOM编程艺术-学习笔记(第七章)
  15. centos 6.5系统判断软件是否安装,如果没安装,则直接使用yum安装,并添加启动项
  16. [转帖] SQL参数化的优点 CopyFrom https://www.cnblogs.com/-lzb/articles/4840671.html
  17. 脚本加密http://www.datsi.fi.upm.es/~frosal/sources/
  18. Java学习——方法中传递参数分简单类型与复杂类型(引用类型)编程计算100+98+96+。。。+4+2+1的值,用递归方法实现
  19. swift 判断真机还是模拟器
  20. golang中的接口实现(二)

热门文章

  1. [转] watch 命令使用(linux监控状态)
  2. 编译安装php-5.3.27报错:make: *** [sapi/fpm/php-fpm] 错误 1
  3. Emma姐
  4. 转-安装vncserver
  5. Linux and Shell 实用命令
  6. final学习
  7. 命令行下class redis not found 解决
  8. spark第十篇:Spark与Kafka整合
  9. 关于Sql注入的那些事
  10. Oracle 维护数据的完整性 一 索引