为了可以方便地在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() {
}
}

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

Hadoop自定义JobTracker和NameNode管理页面的更多相关文章

  1. git学习------>在CenterOS系统上安装GitLab并自定义域名访问GitLab管理页面

    目前就职的公司一直使用SVN作为版本管理,现在打算尝试从SVN迁移到Git.安排我来预言并搭建好相关的环境以及自己尝试使用Git.今天我就尝试在Center OS系统上安装GitLab,现在在此记录一 ...

  2. Hadoop JobTracker和NameNode运行时参数查看

      1)JobTracker运行时参数: hadoop@ubuntu:/home/zhangchao3$ ps -ef | grep job hadoop 29563 1 0 11:34 pts/12 ...

  3. Django(四) 后台管理:创建管理员、注册模型类、自定义管理页面显示内容

    后台管理 第1步.本地化:设置语言.时区 修改project1/settings.py #LANGUAGE_CODE = 'en-us' LANGUAGE_CODE = 'zh-hans' #设置语言 ...

  4. C#_MVC 自定义AuthorizeAttribute实现权限管理

    随笔- 28  文章- 31  评论- 16 MVC 自定义AuthorizeAttribute实现权限管理   在上一节中提到可以使用AuthorizeAttribute进行权限管理: [Autho ...

  5. MVC自定义AuthorizeAttribute实现权限管理

    [转]MVC自定义AuthorizeAttribute实现权限管理 原文载自:小飞的DD http://www.cnblogs.com/feiDD/articles/2844447.html 网站的权 ...

  6. hadoop学习笔记肆--元数据管理机制

    1.首先,认识几个名词 (1).NameNode中读.写.以及DataNode映射等信息叫做“元数据” ,NameNode元数据存放位置有.内存.fsimage.edits log三个位置. (2). ...

  7. python 全栈开发,Day132(玩具管理页面,控制玩具通讯录,基于请求的好友关系建立)

    先下载github代码,下面的操作,都是基于这个版本来的! https://github.com/987334176/Intelligent_toy/archive/v1.5.zip 注意:由于涉及到 ...

  8. BBS--后台管理页面,编辑文章,xss攻击

    1 1.对文章进行增删改查 # 后台管理url re_path(r'^cn_backend/$', views.cn_backend, name='cn_backend'), re_path(r'^c ...

  9. [hadoop读书笔记] 第十章 管理Hadoop集群

    P375 Hadoop管理工具 dfsadmin - 查询HDFS状态信息,管理HDFS. bin/hadoop dfsadmin -help 查询HDFS基本信息 fsck - 检查HDFS中文件的 ...

随机推荐

  1. [转] 基于Gitlab CI搭建持续集成环境

    [From] https://blog.csdn.net/wGL3k77y9fR1k61T1aS/article/details/78798577 前言 本文是在12月12号迅雷@赵兵在前端早读课第三 ...

  2. 2.5 Go 算术运算与变量使用技巧

    变量作用域 简单来说,GO的变量以块为范围:一个if-else的块,就可以包含一个变量的生命周期.变量的声明,要在使用之前. package main import ( "fmt" ...

  3. C#几种常用的加密方式

    一.AES加密算法   AES算法基于排列和置换运算.排列是对数据重新进行安排,置换是将一个数据单元替换为另一个.AES 使用几种不同的方法来执行排列和置换运算.    AES是一个迭代的.对称密钥分 ...

  4. (转)MySQL性能调优my.cnf详解

    MySQL性能调优my.cnf详解 https://blog.linuxeye.cn/379.html http://blog.csdn.net/orichisonic/article/details ...

  5. JS框架设计之模块加载系统

    任何语言一到大规模应用阶段,必然要拆封模块,有利于维护和团队协作,与Java走得最近的dojo率先引进了加载器,使用document.write与同步Ajax请求实现,后台dojo以JSONP的方法来 ...

  6. ETL 工具下载全集 包括 Informatica Datastage Cognos( 持续更新)

    Datastage 8.0 BT种子下载:http://files.cnblogs.com/taven/Datastage_8.0.rar Informatica PowerCenter 8.6.0 ...

  7. validate表单校验插件笔记

    1validation知识点 1 validation基础 validation插件下载http://bassistance.de/jquery-plugins/jquery-plugin-valid ...

  8. 利用rem解决移动端响应适配问题

    最近看了<从网易与淘宝的font-size思考前端设计稿与工作流>和github上的<使用Flexible实现手淘H5页面的终端适配> 以前一直用百分比的我,对移动前端的H5页 ...

  9. PHP的file_get_contents()方法,将整个文件读入字符串中

    <?php $post_data = file_get_contents("e:\\1.txt"); echo $post_data; ?> 更多信息看这里:http: ...

  10. input输入提示历史记录

    一般便于用户的输入习惯,我们都会提示历史消息,让用户有更好的使用体验,以前可能比较多朋友会用js来实现,现在HTML5的datalist可以轻松帮我们实现这个功能!只需以下几行代码 <!doct ...