Hadoop之电信日志数据处理(四)-------web界面实现及可视化效果展示

工具类:

public class DBUtils {
	private static ComboPooledDataSource source = new ComboPooledDataSource();
	
	private DBUtils() {
	}

	public static DataSource getSource(){
		return source;
	}
	
	public static Connection getConn(){
		try {
			return source.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
}

 

servlet类实现:

public class APP01Servlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			//查询日期
			String dateStr = request.getParameter("date");
			System.out.println(dateStr);
			//查询类型
			int type = Integer.parseInt(request.getParameter("type"));
			String sql = null;
			String title = null;
			switch (type) {
				case 1://总流量
					title = "应用大类总流量top10";
					sql = "select " +
							"apptype,DATE_FORMAT(hourid,'%Y-%m-%d') dateid,sum(totalTraffic) su " +
							"from D_H_HTTP_APPTYPE " +
							"group by apptype,dateid " +
							"having dateid ='"+dateStr+"' " +
							"order by su desc " +
							"limit 0,10";
					break;
				case 2://上行流量
					title = "应用大类上行总流量top10";
					sql = "select " +
							"apptype,DATE_FORMAT(hourid,'%Y-%m-%d') dateid,sum(trafficUL) su " +
							"from D_H_HTTP_APPTYPE " +
							"group by apptype,dateid " +
							"having dateid ='"+dateStr+"' " +
							"order by su desc " +
							"limit 0,10";
					break;
				case 3://下行流量
					title = "应用大类下行总流量top10";
					sql = "select " +
							"apptype,DATE_FORMAT(hourid,'%Y-%m-%d') dateid,sum(trafficDL) su " +
							"from D_H_HTTP_APPTYPE " +
							"group by apptype,dateid " +
							"having dateid ='"+dateStr+"' " +
							"order by su desc " +
							"limit 0,10";
					break;
				case 4://尝试次数
					title = "应用大类尝试次数top10";
					sql = "select " +
							"apptype,DATE_FORMAT(hourid,'%Y-%m-%d') dateid,sum(attempts) su " +
							"from D_H_HTTP_APPTYPE " +
							"group by apptype,dateid " +
							"having dateid ='"+dateStr+"' " +
							"order by su desc " +
							"limit 0,10";
					break;
				case 5://接受次数
					title = "应用大类接受次数top10";
					sql = "select " +
							"apptype,DATE_FORMAT(hourid,'%Y-%m-%d') dateid,sum(accepts) su " +
							"from D_H_HTTP_APPTYPE " +
							"group by apptype,dateid " +
							"having dateid ='"+dateStr+"' " +
							"order by su desc " +
							"limit 0,10";
					break;
	
				default:
					throw new RuntimeException("未知操作码");
			}
			
			//查询数据库
			QueryRunner runner = new QueryRunner(DBUtils.getSource());
			List> list = runner.query(sql, new MapListHandler());
			
			//处理结果 -- 图例
			StringBuffer legendBuf = new StringBuffer();
			//处理结果 -- 数据
			StringBuffer seriesBuf = new StringBuffer();
			
			int sum = 0;
			for(Map m : list){
				String app01Name = APP01.getName((Integer) m.get("apptype"));
				legendBuf.append("'"+app01Name+"',");
				
				int data = ((BigDecimal) m.get("su")).intValue();
				sum += data;
				seriesBuf.append("{value:"+data+", name:'"+app01Name+"'},");
			}
			String legend = "legend: {orient : 'vertical',x : 'left',data:["+(legendBuf.length() == 0 ? "" : legendBuf.substring(0, legendBuf.length()-1))+"]}";
			String series = "series : [{name:'应用大类',type:'pie',radius : '55%',center: ['50%', '60%'],data:["+(seriesBuf.length() == 0 ? "" : seriesBuf.substring(0,seriesBuf.length()-1))+"]}]";
		  
			
			//整合
			Map resultMap = new HashMap();
			resultMap.put("date", dateStr);
			resultMap.put("title", 	"title : {text: '"+title+"',subtext: '',x:'center'}");
			resultMap.put("legend", legend);
			resultMap.put("series", series);
			resultMap.put("sum", sum*0.75+"");
			//转发
			request.setAttribute("map", resultMap);
			request.getRequestDispatcher("/app01.jsp").forward(request,response);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}

}

 

public class APP02Servlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String app01name = new String(request.getParameter("app01name").getBytes("iso8859-1"),"utf-8");
		int app01id = APP01.getId(app01name);
		String dateStr = request.getParameter("date");
		int type = Integer.parseInt(request.getParameter("type"));
		
		try {

			String sql = null;
			String title = null;
			switch (type) {
				case 1://总流量
					title = app01name+"类总流量top10";
					sql = "select " +
							"apptype,appsubtype,DATE_FORMAT(hourid,'%Y-%m-%d') dateid,sum(totalTraffic) su " +
							"from D_H_HTTP_APPTYPE " +
							"group by apptype,appsubtype,dateid " +
							"having dateid ='"+dateStr+"' and apptype= '"+app01id+"'" +
							"order by su desc " +
							"limit 0,10";
					break;
				case 2://上行流量
					title = app01name+"类上行总流量top10";
					sql = "select " +
							"apptype,appsubtype,DATE_FORMAT(hourid,'%Y-%m-%d') dateid,sum(trafficUL) su " +
							"from D_H_HTTP_APPTYPE " +
							"group by apptype,appsubtype,dateid " +
							"having dateid ='"+dateStr+"' and apptype= '"+app01id+"'" +
							"order by su desc " +
							"limit 0,10";
					break;
				case 3://下行流量
					title = app01name+"类下行总流量top10";
					sql = "select " +
							"apptype,appsubtype,DATE_FORMAT(hourid,'%Y-%m-%d') dateid,sum(trafficDL) su " +
							"from D_H_HTTP_APPTYPE " +
							"group by apptype,appsubtype,dateid " +
							"having dateid ='"+dateStr+"' and apptype= '"+app01id+"'" +
							"order by su desc " +
							"limit 0,10";
					break;
				case 4://尝试次数
					title = app01name+"类尝试次数top10";
					sql = "select " +
							"apptype,appsubtype,DATE_FORMAT(hourid,'%Y-%m-%d') dateid,sum(attempts) su " +
							"from D_H_HTTP_APPTYPE " +
							"group by apptype,appsubtype,dateid " +
							"having dateid ='"+dateStr+"' and apptype= '"+app01id+"'" +
							"order by su desc " +
							"limit 0,10";
					break;
				case 5://接受次数
					title = app01name+"类接受次数top10";
					sql = "select " +
							"apptype,appsubtype,DATE_FORMAT(hourid,'%Y-%m-%d') dateid,sum(accepts) su " +
							"from D_H_HTTP_APPTYPE " +
							"group by apptype,appsubtype,dateid " +
							"having dateid ='"+dateStr+"' and apptype= '"+app01id+"'" +
							"order by su desc " +
							"limit 0,10";
					break;
	
				default:
					throw new RuntimeException("未知操作码");
			}
			
			//查询数据库
			QueryRunner runner = new QueryRunner(DBUtils.getSource());
			List> list = runner.query(sql, new MapListHandler());
			
			//处理结果 -- 图例
			StringBuffer legendBuf = new StringBuffer();
			//处理结果 -- 数据
			StringBuffer seriesBuf = new StringBuffer();
			
			int sum = 0;
			for(Map m : list){
				String app02Name = APP02.getName((Integer) m.get("appsubtype"));
				legendBuf.append("'"+app02Name+"',");
				
				int data = ((BigDecimal) m.get("su")).intValue();
				sum += data;
				seriesBuf.append("{value:"+data+", name:'"+app02Name+"'},");
			}
			String legend = "legend: {orient : 'vertical',x : 'left',data:["+(legendBuf.length() == 0 ? "" : legendBuf.substring(0, legendBuf.length()-1))+"]}";
			String series = "series : [{name:'"+app01name+"类',type:'pie',radius : '55%',center: ['50%', '60%'],data:["+(seriesBuf.length() == 0 ? "" : seriesBuf.substring(0,seriesBuf.length()-1))+"]}]";
		  
			
			//整合
			Map resultMap = new HashMap();
			resultMap.put("date", dateStr);
			resultMap.put("title", 	"title : {text: '"+title+"',subtext: '',x:'center'}");
			resultMap.put("legend", legend);
			resultMap.put("series", series);
			resultMap.put("sum", sum*0.75+"");
			//转发
			request.setAttribute("map", resultMap);
			request.getRequestDispatcher("/app02.jsp").forward(request,response);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

 

public class APP03Servlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			//查询日期
			String dateStr = request.getParameter("date");
			//查询类型
			int type = Integer.parseInt(request.getParameter("type"));

			String sql = null;
			String title = null;
			switch (type) {
				case 1://尝试次数
					title = "网站表现尝试次数top10";
					sql = "select " +
							"host,DATE_FORMAT(hourid,'%Y-%m-%d') dateid,sum(attempts) su " +
							"from D_H_HTTP_HOST " +
							"group by host,dateid " +
							"having dateid ='"+dateStr+"' " +
							"order by su desc " +
							"limit 0,10";	
					break;
				case 2://接受次数
					title = "网站表现接受次数top10";
					sql = "select " +
							"host,DATE_FORMAT(hourid,'%Y-%m-%d') dateid,sum(accepts) su " +
							"from D_H_HTTP_HOST " +
							"group by host,dateid " +
							"having dateid ='"+dateStr+"' " +
							"order by su desc " +
							"limit 0,10";	
					break;
				case 3://总流量
					title = "网站总流量top10";
					sql = "select " +
							"host,DATE_FORMAT(hourid,'%Y-%m-%d') dateid,sum(totaltraffic) su " +
							"from D_H_HTTP_HOST " +
							"group by host,dateid " +
							"having dateid ='"+dateStr+"' " +
							"order by su desc " +
							"limit 0,10";	
					break;
				case 4://上行流量
					title = "网站上行流量top10";
					sql = "select " +
							"host,DATE_FORMAT(hourid,'%Y-%m-%d') dateid,sum(trafficUL) su " +
							"from D_H_HTTP_HOST " +
							"group by host,dateid " +
							"having dateid ='"+dateStr+"' " +
							"order by su desc " +
							"limit 0,10";	
					break;
				case 5://上行流量
					title = "网站上行流量top10";
					sql = "select " +
							"host,DATE_FORMAT(hourid,'%Y-%m-%d') dateid,sum(trafficDL) su " +
							"from D_H_HTTP_HOST " +
							"group by host,dateid " +
							"having dateid ='"+dateStr+"' " +
							"order by su desc " +
							"limit 0,10";	
					break;

				default:
					throw new RuntimeException("未知操作码");
			}
			
			//查询数据库
			QueryRunner runner = new QueryRunner(DBUtils.getSource());
			List> list = runner.query(sql, new MapListHandler());
			
			//处理结果 -- 横坐标
			StringBuffer xAxisBuf = new StringBuffer();
			
			//处理结果 -- 数据
			StringBuffer seriesBuf = new StringBuffer();
			
			for(Map m : list){
				String hostName = (String) m.get("host");
				xAxisBuf.append("'"+hostName+"',");
				
				int data = ((BigDecimal) m.get("su")).intValue();
				seriesBuf.append("{value:"+data+", name:'"+hostName+"'},");
			}
			String xAxis = "xAxis: {data:["+(xAxisBuf.length() == 0 ? "" : xAxisBuf.substring(0, xAxisBuf.length()-1))+"]}";
			String series = "series : [{name:'网站表现',type:'bar',data:["+(seriesBuf.length() == 0 ? "" : seriesBuf.substring(0,seriesBuf.length()-1))+"]}]";
		  
			
			//整合
			Map resultMap = new HashMap();
			resultMap.put("date", dateStr);
			resultMap.put("title", 	"title : {text: '"+title+"',subtext: '',x:'center'}");
			resultMap.put("xAxis", xAxis);
			resultMap.put("series", series);
			//转发
			request.setAttribute("map", resultMap);
			request.getRequestDispatcher("/app03.jsp").forward(request,response);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}

}

 

public class APP04Servlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String hostname = new String(request.getParameter("hostname").getBytes("iso8859-1"),"utf-8");
		String dateStr = request.getParameter("date");
		int type = Integer.parseInt(request.getParameter("type"));
		
		try {

			String sql = null;
			String title = null;
			switch (type) {
				case 1://尝试次数
					title = hostname+"网站尝试次数top10";
					sql = "select " +
							"host,DATE_FORMAT(hourid,'%H') hour,sum(attempts) su " +
							"from D_H_HTTP_HOST " +
							"group by host,hour " +
							"having host ='"+ hostname +"'" +
							"order by su desc ";
					break;
				default:
					throw new RuntimeException("未知操作码");
			}
			
			//查询数据库
			QueryRunner runner = new QueryRunner(DBUtils.getSource());
			List> list = runner.query(sql, new MapListHandler());
			
			//处理结果 -- 横轴
			StringBuffer xAxisBuf = new StringBuffer();
			//处理结果 -- 数据
			StringBuffer seriesBuf = new StringBuffer();
			
			int sum = 0;
			for(Map m : list){
				String hour = (String) m.get("hour");
				xAxisBuf.append("'"+hour+"',");
				
				int data = ((BigDecimal) m.get("su")).intValue();
				seriesBuf.append("{value:"+data+", name:'"+hour+"'},");
			}
			String xAxis = "xAxis: {data:["+(xAxisBuf.length() == 0 ? "" : xAxisBuf.substring(0, xAxisBuf.length()-1))+"]}";
			String series = "series : [{name:'"+hostname+"网站',type:'line',data:["+(seriesBuf.length() == 0 ? "" : seriesBuf.substring(0,seriesBuf.length()-1))+"]}]";
		  
			
			//整合
			Map resultMap = new HashMap();
			resultMap.put("date", dateStr);
			resultMap.put("title", 	"title : {text: '"+title+"',subtext: '',x:'center'}");
			resultMap.put("xAxis", xAxis);
			resultMap.put("series", series);
			//转发
			request.setAttribute("map", resultMap);
			request.getRequestDispatcher("/app04.jsp").forward(request,response);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

 

public class MonitorServlet extends HttpServlet {

	@SuppressWarnings("unused")
	public void doGet(HttpServletRequest request, final HttpServletResponse resp)
			throws ServletException, IOException {
		
		resp.setContentType("text/html;charset=UTF-8");
		
		final CountDownLatch cdl = new CountDownLatch(1);
		ZooKeeper zk =null;
		try {
			if(zk == null){
				zk = new ZooKeeper("192.168.164.129:2181", 2000, new Watcher() {
					public void process(WatchedEvent event) {
						cdl.countDown();
					}
				});
			}
			cdl.await();
			
			List list1 = zk.getChildren("/zebra/engin1", null);
			List list2 = zk.getChildren("/zebra/engin2", null);
			List list = new ArrayList();
			list.addAll(list1);
			list.addAll(list2);
			String s = "";
			for(String str : list){
				s += str;
				s += ",";
			}
			String st = s.substring(0, s.length()-2);
			if(list1!=null && list2 != null){
				resp.getWriter().println(st);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}

}

前端页面实现请到博客资源处下载

相关jar包:

链接:https://pan.baidu.com/s/1zEyTG_VpjUlw5Nj-tL1K0w 
提取码:joi0 

可视化效果展示:

Hadoop之电信日志数据处理(四)-------web界面实现及可视化效果展示_第1张图片

Hadoop之电信日志数据处理(四)-------web界面实现及可视化效果展示_第2张图片

Hadoop之电信日志数据处理(四)-------web界面实现及可视化效果展示_第3张图片

Hadoop之电信日志数据处理(一)------业务简介

Hadoop之电信日志数据处理(二)------mapreducer端处理

Hadoop之电信日志数据处理(三)------创建数据库及数据收集

 

你可能感兴趣的:(Hadoop之电信日志数据处理(四)-------web界面实现及可视化效果展示)