0


【Kafka+Flume+Mysql+Spark】实现新闻话题实时统计分析系统(附源码)

需要源码请点赞关注收藏后评论区留言私信~~~

系统简介

新闻话题实时统计分析系统以搜狗实验室的用户查询日志为基础,模拟生成用户查询日志,通过Flume将日志进行实时采集、汇集,分析并进行存储。利用Spark Streaming实时统计分析前20名流量最高的新闻话题,并在前端页面实时显示结果。

系统总体架构

1:利用搜狗实验室的用户查询日志模拟日志生成程序生成用户查询日志,供Flume采集

2:日志采集端Flume采集数据发送给Flume日志汇聚节点,并进行预处理

3:Flume将预处理的数据进行数据存储,存储到HBase数据库中,并发送消息给Kafka的Topic

4:Spark Streaming接收Kafka的Topic实时消息并计算实时话题的数量,并将计算结构保存到mysql数据库中

5:前端页面通过建立WebSocket通道读取Mysql数据库中的数据,实时展示话题的动态变化

表结构设计

(1)MySQL的表结构设计

webCount(新闻话题数量表)

(2)HBase表结构设计

weblogs(日志表)

系统实现

模拟日志生成程序

(1)在IntelliJ IDEA构建Java项目weblogs。编写数据生成模拟类,主要功能是读取搜狗用户日志文件,并构建新的格式写入一个新文件供Flume采集

(2)生成 JAR包,并将JAR包上传到生成日志服务器

(3)编写weblog.sh,调用模拟日志生成JAR包,并将weblog.sh上传到生成日志服务器

代码如下

  1. #/bin/bashecho "start log......"java -jar /opt/jars/weblog.jar /opt/datas/weblog.log /opt/datas/weblog-flume.log

Flume配置

(1)配置日志采集端的Flume服务

(2)配置日志汇聚端的Flume服务

(3)自定义SinkHBase程序设计与开发

(4)修改SimpleRowKeyGenerator类,根据具体业务自定义Rowkey生成方法

(5)生成JAR包

(6) JAR上传,将打包名字替换为Flume默认包名flume-ng-hbase-sink-1.7.0.jar ,然后上传至日志汇聚服务器上的flume/lib目录下,覆盖原有的JAR包

Spark Streaming开发

(1)新建一个MAVEN工程,添加依赖包

(2)编写Scala类StructuredStreamingKafka ,实现从Kafka中读取数据存储到关系型数据库MySQL

Websocket和前端界面开发

(1)新建pom.xml文件,内容如下

(2)编写Java类WeblogService,实现功能为连接MySQL数据库

(3)编写Java类WeblogSocket,实现功能建立WebSocket通讯,取统计数据供前端调用。

(4)建立大屏显示页面index.html,实时进行大屏显示

效果展示

在日志采集端运行模拟日志生成程序

发布Web应用 访问大屏显示页面如下

代码

部分代码如下

  1. package main.java;
  2. import java.io.*;
  3. public class ReadWrite {
  4. static String readFileName;
  5. static String writeFileName;
  6. public static void main(String args[]){
  7. readFileName = args[0];
  8. writeFileName = args[1];
  9. try {
  10. // readInput();
  11. readFileByLines(readFileName);
  12. }catch(Exception e){
  13. }
  14. }
  15. public static void readFileByLines(String fileName) {
  16. FileInputStream fis = null;
  17. InputStreamReader isr = null;
  18. BufferedReader br = null;
  19. String tempString = null;
  20. try {
  21. System.out.println("以行为单位读取文件内容,一次读一整行:");
  22. fis = new FileInputStream(fileName);// FileInputStream
  23. // 从文件系统中的某个文件中获取字节
  24. isr = new InputStreamReader(fis,"GBK");
  25. br = new BufferedReader(isr);
  26. int count=0;
  27. while ((tempString = br.readLine()) != null) {
  28. count++;
  29. // 显示行号
  30. Thread.sleep(300);
  31. String str = new String(tempString.getBytes("UTF8"),"GBK");
  32. System.out.println("row:"+count+">>>>>>>>"+tempString);
  33. method1(writeFileName,tempString);
  34. //appendMethodA(writeFileName,tempString);
  35. }
  36. isr.close();
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. } catch (InterruptedException e) {
  40. e.printStackTrace();
  41. } finally {
  42. if (isr != null) {
  43. try {
  44. isr.close();
  45. } catch (IOException e1) {
  46. }
  47. }
  48. }
  49. }
  50. // public static void appendMethodA(String fileName, String content) {
  51. // try {
  52. // // 打开一个随机访问文件流,按读写方式
  53. // //logger.info("file line >>>>>>>>>>>>>>>>>>>>>>>>>:"+content);
  54. // RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
  55. //
  56. // // 文件长度,字节数
  57. // long fileLength = randomFile.length();
  58. // //将写文件指针移到文件尾。
  59. // randomFile.seek(fileLength);
  60. // //randomFile.writeUTF(content);
  61. // randomFile.writeUTF(content);
  62. // randomFile.writeUTF("\n");
  63. // // randomFile.wri;
  64. //
  65. // randomFile.close();
  66. // } catch (IOException e) {
  67. // e.printStackTrace();
  68. // }
  69. // }
  70. public static void method1(String file, String conent) {
  71. BufferedWriter out = null;
  72. try {
  73. out = new BufferedWriter(new OutputStreamWriter(
  74. new FileOutputStream(file, true)));
  75. out.write("\n");
  76. out.write(conent);
  77. } catch (Exception e) {
  78. e.printStackTrace();
  79. } finally {
  80. try {
  81. out.close();
  82. } catch (IOException e) {
  83. e.printStackTrace();
  84. }
  85. }
  86. }
  87. }

Hbase数据库类代码

  1. package org.apache.flume.sink.hbase;
  2. import com.google.common.base.Charsets;
  3. import org.apache.flume.Context;
  4. import org.apache.flume.Event;
  5. import org.apache.flume.FlumeException;
  6. import org.apache.flume.conf.ComponentConfiguration;
  7. import org.apache.flume.sink.hbase.SimpleHbaseEventSerializer.KeyType;
  8. import org.hbase.async.AtomicIncrementRequest;
  9. import org.hbase.async.PutRequest;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. public class KfkAsyncHbaseEventSerializer implements AsyncHbaseEventSerializer {
  13. private byte[] table;
  14. private byte[] cf;
  15. private byte[] payload;
  16. private byte[] payloadColumn;
  17. private byte[] incrementColumn;
  18. private String rowPrefix;
  19. private byte[] incrementRow;
  20. private KeyType keyType;
  21. @Override
  22. public void initialize(byte[] table, byte[] cf) {
  23. this.table = table;
  24. this.cf = cf;
  25. }
  26. @Override
  27. public List<PutRequest> getActions() {
  28. List<PutRequest> actions = new ArrayList<PutRequest>();
  29. if (payloadColumn != null) {
  30. byte[] rowKey;
  31. try {
  32. String[] columns = new String(this.payloadColumn).split(",");
  33. String[] values = new String(this.payload).split(",");
  34. for (int i=0; i < columns.length; i++) {
  35. byte[] colColumn = columns[i].getBytes();
  36. byte[] colValue = values[i].getBytes(Charsets.UTF_8);
  37. String datetime = values[0].toString();
  38. String userid = values[1].toString();
  39. rowKey = SimpleRowKeyGenerator.getKfkRowKey(userid, datetime);
  40. PutRequest putRequest = new PutRequest(table, rowKey, cf,
  41. colColumn, colValue);
  42. actions.add(putRequest);
  43. }
  44. } catch (Exception e) {
  45. throw new FlumeException("Could not get row key!", e);
  46. }
  47. }
  48. return actions;
  49. }
  50. public List<AtomicIncrementRequest> getIncrements() {
  51. List<AtomicIncrementRequest> actions = new ArrayList<AtomicIncrementRequest>();
  52. if (incrementColumn != null) {
  53. AtomicIncrementRequest inc = new AtomicIncrementRequest(table,
  54. incrementRow, cf, incrementColumn);
  55. actions.add(inc);
  56. }
  57. return actions;
  58. }
  59. @Override
  60. public void cleanUp() {
  61. // TODO Auto-generated method stub
  62. }
  63. @Override
  64. public void configure(Context context) {
  65. String pCol = context.getString("payloadColumn", "pCol");
  66. String iCol = context.getString("incrementColumn", "iCol");
  67. rowPrefix = context.getString("rowPrefix", "default");
  68. String suffix = context.getString("suffix", "uuid");
  69. if (pCol != null && !pCol.isEmpty()) {
  70. if (suffix.equals("timestamp")) {
  71. keyType = KeyType.TS;
  72. } else if (suffix.equals("random")) {
  73. keyType = KeyType.RANDOM;
  74. } else if (suffix.equals("nano")) {
  75. keyType = KeyType.TSNANO;
  76. } else {
  77. keyType = KeyType.UUID;
  78. }
  79. payloadColumn = pCol.getBytes(Charsets.UTF_8);
  80. }
  81. if (iCol != null && !iCol.isEmpty()) {
  82. incrementColumn = iCol.getBytes(Charsets.UTF_8);
  83. }
  84. incrementRow = context.getString("incrementRow", "incRow").getBytes(Charsets.UTF_8);
  85. }
  86. @Override
  87. public void setEvent(Event event) {
  88. this.payload = event.getBody();
  89. }
  90. @Override
  91. public void configure(ComponentConfiguration conf) {
  92. // TODO Auto-generated method stub
  93. }
  94. }

创作不易 觉得有帮助请点赞关注收藏~~~

标签: spark kafka flume

本文转载自: https://blog.csdn.net/jiebaoshayebuhui/article/details/128503123
版权归原作者 showswoller 所有, 如有侵权,请联系我们删除。

“【Kafka+Flume+Mysql+Spark】实现新闻话题实时统计分析系统(附源码)”的评论:

还没有评论