Spark Streaming 專案實戰(12)—— Web層開發
阿新 • • 發佈:2018-12-30
1 Web 層開發
1.1 POM 新增依賴
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
1.2 StatApp.java
package com.streaming.project.spark;
import com.streaming.project.dao.CourseClickCountDAO;
import com.streaming.project.domain.CourseClickCount;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @ClassName StatApp
* @Description Web層開發
* @Author tzb
* @Date 2018/12/19 18:25
* @Version 1.0
**/
@RestController
public class StatApp {
private static Map<String, String> courses = new HashMap<>();
static {
courses.put("112", "Hadoop實戰");
courses.put("128", "Spark快速入門");
courses.put("145", "Scala 入門");
courses.put("146", "Python 爬蟲");
courses.put("130", "資料結構與演算法分析");
courses.put("131", "Spark SQL專案實戰");
}
@Autowired
CourseClickCountDAO courseClickCountDAO;
// @RequestMapping(value = "/course_click_dynamic", method = RequestMethod.GET)
// public ModelAndView courseClickCount() throws IOException {
// ModelAndView view = new ModelAndView("index");
//
// List<CourseClickCount> list = courseClickCountDAO.query("20181219");
//
// for (CourseClickCount model : list) {
// model.setName(courses.get(model.getName().substring(9)));
// }
//
// JSONArray json = JSONArray.fromObject(list);
//
// view.addObject("data_json",json);
//
// return view;
// }
@RequestMapping(value = "/course_click_dynamic", method = RequestMethod.POST)
@ResponseBody
public List<CourseClickCount> courseClickCount() throws IOException {
List<CourseClickCount> list = courseClickCountDAO.query("20181219");
for (CourseClickCount model : list) {
model.setName(courses.get(model.getName().substring(9)));
}
return list;
}
@RequestMapping(value = "/echarts", method = RequestMethod.GET)
public ModelAndView echarts() {
return new ModelAndView("echarts");
}
}
1.3 echarts.html
- 新增
jquery.js
檔案
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>stat</title>
<!-- 引入 ECharts 檔案 -->
<script src="js/echarts.min.js"></script>
<!--引入 jQuery 檔案-->
<script src="js/jQuery.js"></script>
</head>
<body>
通過 /echarts 請求跳轉過來的頁面
<!-- 為 ECharts 準備一個具備大小(寬高)的 DOM -->
<div id="main"
style="width: 600px;height:400px;position: absolute;top:50%;left: 50%;margin-top:-200px;margin-left:-300px"></div>
<script type="text/javascript">
// 基於準備好的dom,初始化echarts例項
var myChart = echarts.init(document.getElementById('main'));
// 指定圖表的配置項和資料
var option = {
title: {
text: '實時流處理統計結果之訪問量統計',
subtext: '實戰課程訪問次數',
x: 'center'
},
tooltip: {
trigger: 'item',
formatter: "{a} <br/>{b} : {c} ({d}%)"
},
legend: {
orient: 'vertical',
left: 'left'
},
series: [
{
name: '訪問次數',
type: 'pie',
radius: '55%',
center: ['50%', '60%'],
data: (function () { //<![CDATA[
var datas = [];
$.ajax({
type: "POST",
url: "/spark-streaming/course_clickcount_dynamic",
dataType: 'json',
async: false,
success: function (result) {
for (var i = 0; i < result.lenght; i++) {
datas.put({"value":result[i].value,"name":result[i].name})
}
}
})
return datas;
//]]>
}),
itemStyle: {
emphasis: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
};
// 使用剛指定的配置項和資料顯示圖表。
myChart.setOption(option);
</script>
</body>
</html>
訪問 http://localhost:8081/spark-streaming/echarts
2 功能擴充套件
Spring Boot 整合 Echarts 動態獲取HBase資料
- 動態傳遞當天的時間:在頁面放一個時間外掛(jQuery外掛),預設只取當天資料;
- 自動重新整理展示圖:每隔一定時間請求重新整理當前的資料