畢業(yè)設(shè)計(jì) 大數(shù)據(jù)服務(wù)器數(shù)據(jù)分析與可視化
0 前言
?? 這兩年開始畢業(yè)設(shè)計(jì)和畢業(yè)答辯的要求和難度不斷提升,傳統(tǒng)的畢設(shè)題目缺少創(chuàng)新和亮點(diǎn),往往達(dá)不到畢業(yè)答辯的要求,這兩年不斷有學(xué)弟學(xué)妹告訴學(xué)長自己做的項(xiàng)目系統(tǒng)達(dá)不到老師的要求。
為了大家能夠順利以及最少的精力通過畢設(shè),學(xué)長分享優(yōu)質(zhì)畢業(yè)設(shè)計(jì)項(xiàng)目,今天要分享的是
?? ?基于大數(shù)據(jù)的服務(wù)器數(shù)據(jù)分析與可視化系統(tǒng)
??學(xué)長這里給一個(gè)題目綜合評(píng)分(每項(xiàng)滿分5分)
難度系數(shù):3分
創(chuàng)新點(diǎn):3分
界面美化:5分
畢設(shè)幫助,選題指導(dǎo),技術(shù)解答,歡迎打擾,見B站個(gè)人主頁
https://space.bilibili.com/33886978
1 課題背景
基于python的nginx大數(shù)據(jù)日志分析可視化,通過流、批兩種方式,分析 nginx 日志,將分析結(jié)果通過 flask + echarts 進(jìn)行可視化展示
2 實(shí)現(xiàn)效果
24 小時(shí)訪問趨勢

每日訪問情況

客戶端設(shè)備占比

用戶分布
爬蟲詞云

3 數(shù)據(jù)收集分析過程
總體框架圖

kafka 創(chuàng)建日志主題
# 創(chuàng)建主題
kafka-topics --bootstrap-server gfdatanode01:9092 --create --replication-factor 3 --partitions 1 --topic nginxlog
flume 收集日志寫到 kafka
創(chuàng)建 flume 到 kafka 的配置文件 flume_kafka.conf,配置如下
a1.sources = s1
a1.channels = c1
a1.sinks = k1 ? ?
a1.sources.s1.type=exec
a1.sources.s1.command=tail -f /var/log/nginx/access.log
a1.sources.s1.channels=c1
#設(shè)置Kafka接收器
a1.sinks.k1.type= org.apache.flume.sink.kafka.KafkaSink
#設(shè)置Kafka地址
a1.sinks.k1.brokerList=172.16.122.23:9092
#設(shè)置發(fā)送到Kafka上的主題
a1.sinks.k1.topic=nginxlog
#設(shè)置序列化方式
a1.sinks.k1.serializer.class=kafka.serializer.StringEncoder
a1.sinks.k1.channel=c1 ? ?
a1.channels.c1.type=memory
a1.channels.c1.capacity=10000
a1.channels.c1.transactionCapacity=100 ?
啟動(dòng) flume
flume-ng agent -n a1 -f flume_kafka.conf
python 讀取 kafka 實(shí)時(shí)處理
通過 python 實(shí)時(shí)處理 nginx 的每一條日志數(shù)據(jù),然后寫到 mysql 。
from kafka import KafkaConsumer
servers = ['172.16.122.23:9092', ]
consumer = KafkaConsumer(
? ?bootstrap_servers=servers,
? ?auto_offset_reset='latest', ?# 重置偏移量 earliest移到最早的可用消息,latest最新的消息,默認(rèn)為latest
)
consumer.subscribe(topics=['nginxlog'])
for msg in consumer:
? ?info = re.findall('(.*?) - (.*?) \[(.*?)\] "(.*?)" (\\d+) (\\d+) "(.*?)" "(.*?)" .*', msg.value.decode())
? ?log = NginxLog(*info[0])
? ?log.save()
數(shù)據(jù)分析可視化
-- 用戶分布
select province, count(distinct remote_addr) from fact_nginx_log where device <> 'Spider' group by province;
-- 不同時(shí)段訪問情況
select case when device='Spider' then 'Spider' else 'Normal' end, hour(time_local), count(1)
from fact_nginx_log
group by case when device='Spider' then 'Spider' else 'Normal' end, hour(time_local);
-- 最近7天訪問情況
select case when device='Spider' then 'Spider' else 'Normal' end, DATE_FORMAT(time_local, '%Y%m%d'), count(1)
from fact_nginx_log
where time_local > date_add(CURRENT_DATE, interval - 7 day)
group by case when device='Spider' then 'Spider' else 'Normal' end, DATE_FORMAT(time_local, '%Y%m%d');
-- 用戶端前10的設(shè)備
select device, count(1)
from fact_nginx_log
where device not in ('Other', 'Spider') -- 過濾掉干擾數(shù)據(jù)
group by device
order by 2 desc
limit 10
-- 搜索引擎爬蟲情況
select browser, count(1) from fact_nginx_log where device = 'Spider' group by browser;
最后,通過 pandas 讀取 mysql,經(jīng) ironman 進(jìn)行可視化展示。
4 Flask框架
簡介
Flask是一個(gè)基于Werkzeug和Jinja2的輕量級(jí)Web應(yīng)用程序框架。與其他同類型框架相比,F(xiàn)lask的靈活性、輕便性和安全性更高,而且容易上手,它可以與MVC模式很好地結(jié)合進(jìn)行開發(fā)。Flask也有強(qiáng)大的定制性,開發(fā)者可以依據(jù)實(shí)際需要增加相應(yīng)的功能,在實(shí)現(xiàn)豐富的功能和擴(kuò)展的同時(shí)能夠保證核心功能的簡單。Flask豐富的插件庫能夠讓用戶實(shí)現(xiàn)網(wǎng)站定制的個(gè)性化,從而開發(fā)出功能強(qiáng)大的網(wǎng)站。
本項(xiàng)目在Flask開發(fā)后端時(shí),前端請求會(huì)遇到跨域的問題,解決該問題有修改數(shù)據(jù)類型為jsonp,采用GET方法,或者在Flask端加上響應(yīng)頭等方式,在此使用安裝Flask-CORS庫的方式解決跨域問題。此外需要安裝請求庫axios。
Flask框架圖

相關(guān)代碼
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../')
from flask import Flask, render_template
from ironman.data import SourceData
from ironman.data_db import SourceData
app = Flask(__name__)
source = SourceData()
@app.route('/')
def index():
? ?return render_template('index.html')
@app.route('/line')
def line():
? ?data = source.line
? ?xAxis = data.pop('legend')
? ?return render_template('line.html', title='24小時(shí)訪問趨勢', data=data, legend=list(data.keys()), xAxis=xAxis)
@app.route('/bar')
def bar():
? ?data = source.bar
? ?xAxis = data.pop('legend')
? ?return render_template('bar.html', title='每日訪問情況', data=data, legend=list(data.keys()), xAxis=xAxis)
@app.route('/pie')
def pie():
? ?data = source.pie
? ?return render_template('pie.html', title='客戶端設(shè)備占比', data=data, legend=[i.get('name') for i in data])
@app.route('/china')
def china():
? ?data = source.china
? ?return render_template('china.html', title='用戶分布', data=data)
@app.route('/wordcloud')
def wordcloud():
? ?data = source.wordcloud
? ?return render_template('wordcloud.html', title='爬蟲詞云', data=data)
if __name__ == "__main__":
? ?app.run(host='127.0.0.1', debug=True)
5 最后
畢設(shè)幫助,選題指導(dǎo),技術(shù)解答,歡迎打擾,見B站個(gè)人主頁
https://space.bilibili.com/33886978