Hadoop-02基于Hadoop的JavaEE数据可视化简易案例需求
1.统计⾳乐点播次数
2.使⽤echarts柱状图显⽰每⾸⾳乐的点播次数
项⽬结构
创建JavaEE项⽬
统计播放次数Job关键代码
;
import java.io.IOException;
import java.util.HashMap;
import org.f.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
/** 歌曲点播统计 */
public class MusicCount {
//定义保存统计数据结果的map集合
public static HashMap<String, Integer> map=new HashMap<String, Integer>();
public static class MusicMapper extends Mapper<Object, Text, Text, IntWritable> {
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
IntWritable valueOut = new IntWritable(1);
String keyInStr = String();
String[] keyInStrArr = keyInStr.split("\t");// 使⽤\t将输⼊⽂本⾏转换为字符串
String keyOut = keyInStrArr[0];// 获取歌曲名称
context.write(new Text(keyOut), valueOut);
}
}
public static class MusicReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += ();
}
result.set(sum);
context.write(key, result);//统计数据保存到hdfs⽂件
map.String(), sum);//将统计结果保存到map集合
}
}
public static HashMap<String, Integer> main() throws Exception {
Configuration conf = new Configuration();
conf.addResource("l");// 读取项⽬中hdfs配置信息
conf.addResource("l");// 读取项⽬中mapreduce配置信息
/
/ 实例化作业
Job job = Instance(conf, "music_count");
// 指定jar的class
job.setJarByClass(MusicCount.class);
// 指定Mapper
job.setMapperClass(MusicMapper.class);
// 压缩数据
job.setCombinerClass(MusicReducer.class);// 减少datanode,TaskTracker之间数据传输
// 指定reducer
job.setReducerClass(MusicReducer.class);
// 设置输出key数据类型
job.setOutputKeyClass(Text.class);
// 设置输出Value数据类型
job.setOutputValueClass(IntWritable.class);
// 设置输⼊⽂件路径
FileInputFormat.addInputPath(job, new Path("hdfs://192.168.137.131:9000/"));  FileInputFormat.addInputPath(job, new Path("hdfs://192.168.137.131:9000/"));  FileInputFormat.addInputPath(job, new Path("hdfs://192.168.137.131:9000/"));  FileInputFormat.addInputPath(job, new Path("hdfs://192.168.137.131:9000/"));    //设置输出⽂件路径
FileSystem (conf);
Path path=new Path("hdfs://192.168.137.131:9000/musicout");
ists(path)) {
fs.delete(path,true);
}
FileOutputFormat.setOutputPath(job, new Path("hdfs://192.168.137.131:9000/musicout"));
if(job.waitForCompletion(true)) {
return map;
}else {
return null;
}
}
}
Servlet关键代码
action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
MusicCount;
/**向客户端提供json数据*/
@WebServlet("/CountServlet")
public class CountServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//post乱码处理
request.setCharacterEncoding("utf-8");
// 设置响应数据类型
response.setContentType("text/html");
// 设置响应编码格式
response.setCharacterEncoding("utf-8");
/
/ 获取out对象
PrintWriter out = Writer();
//组织json数据
HashMap<String, Integer> map=null;
try {
map=MusicCount.main();
} catch (Exception e) {
System.out.println("获取数据出错");
}
//通过构建map集合转换为嵌套json格式数据
HashMap jsonmap = new HashMap();
jsonmap.put("mytitle","歌词播放统计");
jsonmap.put("mylegend", "点播");
jsonmap.put("prolist", map);
String str =JSONString(jsonmap);
out.print(str);
out.flush();
out.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
doGet(request, response);
}java开发可视化界面
}
视图index.jsp关键代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>⾦融⼤数据解析</title>
<!-- 引⼊ echarts.js -->
<script src="script/echarts.min.js"></script>
<!-- 引⼊ jquery.js -->
<script src="script/jquery-1.8.3.min.js"></script>
</head>
<body>
<!-- 为ECharts准备⼀个具备⼤⼩(宽⾼)的Dom -->
<div id="main" ></div>
<script type="text/javascript">
//显⽰柱状图函数
function showdata(mytitle, mylegend, xdata, ydata) {
/
/ 基于准备好的dom,初始化echarts实例
var myChart = echarts.ElementById('main'));
// 指定图表的配置项和数据
var option = {
title : {
text : mytitle
},
tooltip : {},
legend : {
data : mylegend
},
xAxis : {
data : xdata
},
yAxis : {},
series : [ {
name : '点播',
type : 'bar',
data : ydata
} ]
};
// 使⽤刚指定的配置项和数据显⽰图表。
myChart.setOption(option);
}
$(function() {
var mytitle;
var mylegend;
var xdata=new Array();
var ydata=new Array();
$.getJSON("CountServlet", function(data) {
mytitle = itle;
mylegend = legend;
//获取x轴数据
$.each(data.prolist, function(i, n) {
xdata.push(i);
});
//获取y轴数据
$.each(data.prolist, function(i, n) {
ydata.push(n);
});
//执⾏函数
showdata(mytitle, [ mylegend ], xdata, ydata);  });
});
</script>
</body>
</html>
运⾏结果
项⽬所需jar列表
总结
1.该案例的缺点是什么?每次访问数据需要提交job到hadoop集运⾏,性能低。
2.数据分析结果保存在HDFS和集合中,不适合分析结果为⼤数据集合。
3.如何改进?使⽤HBase存储解析后的数据集,构建离线分析和即时查询⼤数据分析平台。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。