大数据Web可视化分析系统开发

网友投稿 238 2022-09-25

大数据Web可视化分析系统开发

下载地址

​​com.spark.service;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;public class WeblogService { public static void queryWeblogs() throws Exception { Connection conn=null; PreparedStatement pst=null; String url ="jdbc:mysql://bigdata-pro01.kfk.com:3306/test"; String username="root"; String password="root"; try{ Class.forName("com.mysql.jdbc.Driver"); conn= DriverManager.getConnection(url,username,password); String query_sql="select titleName,count from webCount where 1=1 order by count desc limit 20"; pst=conn.prepareStatement(query_sql); ResultSet rs=pst.executeQuery(); int i=0; while (rs.next()){ String titleName= rs.getString("titleName"); String titleCount= rs.getString("count"); WeblogSocket.titleName[i]=titleName; WeblogSocket.titleCount[i]=titleCount; ++i; } }catch (Exception e){ e.printStackTrace(); } }}

再新建一个类

我们把tomcat包加载进来

把这些包拷贝到工程目录下

还需要把这些包引进来

写入代码

package com.spark.service;import java.io.IOException;import java.util.HashMap;import java.util.Map;import com.alibaba.fastjson.JSON;import javax.websocket.OnClose;import javax.websocket.OnMessage;import javax.websocket.OnOpen;import javax.websocket.Session;import javax.websocket.server.ServerEndpoint;@ServerEndpoint("/websocket")public class WeblogSocket { public static String[] titleName = new String[20]; public static String[] titleCount = new String[20]; //public static String[] titleSum = new String[1]; @OnMessage public void onMessage(String message,Session session) throws Exception { while (true){ WeblogService.queryWeblogs(); Map map =new HashMap(); map.put("titleName", titleName); map.put("titleCount",titleCount); // map.put("titleSum", titleSum); session.getBasicRemote(). sendText(JSON.toJSONString(map)); Thread.sleep(5000); map.clear(); } } @OnOpen public void onOpen () { System.out.println("Client connected"); } @OnClose public void onClose () { System.out.println("Connection closed"); }}

下载地址

​​html> Title

把这两个类的代码修改一下

package com.spark.service;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.HashMap;import java.util.Map;public class WeblogService { static String url ="jdbc:mysql://bigdata-pro01.kfk.com:3306/test"; static String username="root"; static String password="root"; public Map queryWeblogs() { Connection conn=null; PreparedStatement pst=null; String[] titleNames = new String[20]; String[] titleCounts = new String[20]; Map retMap= new HashMap(); try{ Class.forName("com.mysql.jdbc.Driver"); conn= DriverManager.getConnection(url,username,password); String query_sql="select titleName,count from webCount where 1=1 order by count desc limit 20"; pst=conn.prepareStatement(query_sql); ResultSet rs=pst.executeQuery(); int i=0; while (rs.next()){ String titleName= rs.getString("titleName"); String titleCount= rs.getString("count"); titleNames[i]=titleName; titleCounts[i]=titleCount; ++i; } retMap.put("titleName", titleNames); retMap.put("titleCount",titleCounts); }catch (Exception e){ e.printStackTrace(); } return retMap; } public String[] titleCount() { Connection conn=null; PreparedStatement pst=null; String[] titleSums = new String[1]; Map retMap= new HashMap(); try{ Class.forName("com.mysql.jdbc.Driver"); conn= DriverManager.getConnection(url,username,password); String query_sql="select count(1) titleSum from webCount "; pst=conn.prepareStatement(query_sql); ResultSet rs=pst.executeQuery(); if (rs.next()){ String titleSum= rs.getString("titleSum"); titleSums[0]=titleSum; } }catch (Exception e){ e.printStackTrace(); } return titleSums; }}

package com.spark.service;import java.io.IOException;import java.util.HashMap;import java.util.Map;import com.alibaba.fastjson.JSON;import javax.websocket.OnClose;import javax.websocket.OnMessage;import javax.websocket.OnOpen;import javax.websocket.Session;import javax.websocket.server.ServerEndpoint;@ServerEndpoint("/websocket")public class WeblogSocket { WeblogService weblogService = new WeblogService(); @OnMessage public void onMessage(String message,Session session) throws Exception { while (true){ Map map =new HashMap(); map.put("titleName", weblogService.queryWeblogs().get("titleName")); map.put("titleCount",weblogService.queryWeblogs().get("titleCount")); map.put("titleCount",weblogService.titleCount()); session.getBasicRemote(). sendText(JSON.toJSONString(map)); Thread.sleep(5000); map.clear(); } } @OnOpen public void onOpen () { System.out.println("Client connected"); } @OnClose public void onClose () { System.out.println("Connection closed"); }}

我们之前的结构化流的处理我们还是跟web分开

报了这么一个错误,我们就是把那个文件干掉就行了

我们再来一次

把多余的包剔除掉

报错了

是因为相同的包冲突的原因

我们就导这些包进来就可以了

我们再编译一次

这次编译通过了

把刚刚打的包上传上来

接下来我们重新建一个web工程

从原来的拷贝过去

把js也拷贝过去

把lib包拷贝过去

把index.html文件也拷贝进来到web目录下

我们把web项目的包引进去

下面配置一下我们的tomcat

我们还要把tomcat的包加载进来

这样子我们的web项目就没有问题了

我们启动一下看看服务是否正常的

这明显不是我们想要的结果,这个界面不是我们的index.html,为了和前面的项目区分开来,我们用的端口号最好不要跟前面的一样

我们再跑一次tomcat

没有报任何错误

这才是我们想要的结果

不过这个经过分析是因为我前面的sparkStu工程还在跑着的原因,我忘记停下来了!

我们按下F12

出现错误了

我们的目录有问题

把这个目录激活

改下这里

改过来就不报错了

把这个工程改成maven工程

这样就把一个普通的工程变成了一个maven工程

添加这个依赖包

javax javaee-api 7.0 provided

然后我们再rebuid一下

再运行一下tomcat

是没什么问题,但是这个数据真他妈的恶心

在sparkStu_web工程里面加上这一段

这里把最后修改的代码附上(sparkStu_web工程的)

package com.spark.service;import com.alibaba.fastjson.JSON;import javax.websocket.OnClose;import javax.websocket.OnMessage;import javax.websocket.OnOpen;import javax.websocket.Session;import javax.websocket.server.ServerEndpoint;import java.io.IOException;import java.util.HashMap;import java.util.Map;@ServerEndpoint("/websocket")public class WeblogSocket { WeblogService weblogService = new WeblogService(); @OnMessage public void onMessage(String message, Session session) throws IOException, InterruptedException { while(true){ Map map = new HashMap(); map.put("titleName", weblogService.queryWeblogs().get("titleName")); map.put("titleCount",weblogService.queryWeblogs().get("titleCount")); map.put("titleSum", weblogService.titleCount()); session.getBasicRemote(). sendText(JSON.toJSONString(map)); Thread.sleep(1000); map.clear(); } } @OnOpen public void onOpen () { System.out.println("Client connected"); } @OnClose public void onClose () { System.out.println("Connection closed"); }}

package com.spark.service;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.HashMap;import java.util.Map;/** * Created by Administrator on 2017/10/17. */public class WeblogService { static String url ="jdbc:mysql://bigdata-pro01.kfk.com:3306/test"; static String username="root"; static String password="root"; public Map queryWeblogs() { Connection conn = null; PreparedStatement pst = null; String[] titleNames = new String[30]; String[] titleCounts = new String[30]; Map retMap = new HashMap(); try{ Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url,username,password); String query_sql = "select titleName,count from webCount where 1=1 order by count desc limit 30"; pst = conn.prepareStatement(query_sql); ResultSet rs = pst.executeQuery(); int i = 0; while (rs.next()){ String titleName = rs.getString("titleName"); String titleCount = rs.getString("count"); titleNames[i] = titleName; titleCounts[i] = titleCount; ++i; } retMap.put("titleName", titleNames); retMap.put("titleCount", titleCounts); }catch(Exception e){ e.printStackTrace(); }finally{ try { if (pst != null) { pst.close(); } if (conn != null) { conn.close(); } }catch(Exception e){ e.printStackTrace(); } } return retMap; } public String[] titleCount() { Connection conn = null; PreparedStatement pst = null; String[] titleSums = new String[1]; try{ Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url,username,password); String query_sql = "select count(1) titleSum from webCount"; pst = conn.prepareStatement(query_sql); ResultSet rs = pst.executeQuery(); if(rs.next()){ String titleSum = rs.getString("titleSum"); titleSums[0] = titleSum; } }catch(Exception e){ e.printStackTrace(); }finally{ try{ if (pst != null) { pst.close(); } if (conn != null) { conn.close(); } }catch(Exception e){ e.printStackTrace(); } } return titleSums; }}

启动tomcat

这边的工程我们也启动

让节点2的数据也跑起来这个时候保证kafka,flume等进程的开启,前面已经讲过很多次了

把表中的数据清除

可以看到实时监控是没有数据的

经过漫长的等待

看到实时在监控,数据发生变化

整个项目到现在为止全部结束了,真的好累啊,现在是凌晨3点了,

我真的感到很疲惫了,该回宿舍休息了,实验室的周围是那么安静的!!!!!!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:不止iPhone 12,苹果秋季发布会继续放大招?
下一篇:基于centos7的真实机环境下安装 vmware workstastion
相关文章

 发表评论

暂时没有评论,来抢沙发吧~