综合实验:网站访问日志采集、处理及分析
实验步骤
注:截图必须使用实验机的带水印截图功能,题目中要求截图但未提供的视为未做
一、使用Flume导入日志数据
数据文件下载地址:
wget "http://10.90.3.2/files/web_log/xaa.log"
wget "http://10.90.3.2/files/web_log/xab.log"
wget "http://10.90.3.2/files/web_log/xac.log"
新建一个待监控的文件夹,放入三个日志文件
1、(代码)创建flume配置,文件内容及部分属性注释:
2、(截图)运行flume配置的命令(运行前需启动hadoop)
3、(截图)日志导入到hdfs后,查看hdfs目录内容:
4、(截图)选取一个文件查看前几条内容:
二、使用MapReduce对日志数据进行预处理
1、(代码)map和reduce文件:
2、(截图)用hadoop-streaming运行MapReduce的命令、及运行时的部分截图
3、(截图)查看几条产出的数据记录截图:
提示:mapreduce需要输出哪些字段?看第三点需要。
三、使用Hive对日志数据进行分析和统计
1、(代码、截图)哪个IP地址(域名) 访问次数最多?
2、(代码、截图)哪个IP地址(域名) 产生的流量最大?
3、(代码、截图)产生流量最大的前10% IP地址(域名) ?
4、(代码、截图)统计每个月的流量
5、(代码、截图)统计每个月的流量 month_total 、 当年的总流量 year_total 及 每月流量占比 ( month_total /year_total )
6、(代码、截图)日志记录的时期,一共有多少错误的请求?(响应码以4或5开头)
7、(代码、截图)找出每个月错误请求占当月总请求数量的占比。
四、对分析的结果进行可视化
1、(代码、截图)将上一步5的查询结果保存到本地文件
2、(代码)pyecharts实现饼图、折线图
3、(截图)图表展示
一、
下载三个日志文件放到目录的0104文件下
wget -P ~/0104 "http://10.90.3.2/files/web_log/xaa.log"
wget -P ~/0104 "http://10.90.3.2/files/web_log/xab.log"
wget -P ~/0104 "http://10.90.3.2/files/web_log/xac.log"
1、(代码)创建flume配置,文件内容及部分属性注释:
建一个flume.conf文件
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# 定义源: spooldir
a1.sources.r1.type = spooldir
# 指定监控目录(本地路径)
a1.sources.r1.spoolDir = /home/ubuntu/0104
a1.sources.r1.decodeErrorPolicy = IGNORE
# Describe the sink(hdfs路径)
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = /flume/2015070104/
a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.useLocalTimeStamp = true
a1.sinks.k1.hdfs.rollSize = 0
a1.sinks.k1.hdfs.rollCount = 0
# Use a channel which buffers events in memory
a1.channels.c1.type = memory
# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
2. (截图)运行flume配置的命令(运行前需启动hadoop):
#启动hadoop并初始化
cd /opt/Hadoop/sbin
hadoop namenode -format
./start-all.sh
#运行
flume-ng agent -f /home/ubuntu/flume.conf -n a1
3. (截图)日志导入到hdfs后,查看hdfs目录内容:
Hdfs dfs -ls /flume/2015070104/
*hdfs dfs -cat /flume/2015070104/* |wc -l * //查看所有文件行数
4. (截图)选取一个文件查看前几条内容:
hdfs dfs -cat /flume/2015070104/FlumeData.1654736506377 |head -n 10
二、1.
map.py:
import sys
import re
for i in sys.stdin:
ss = i.strip().split(' ')
if len(ss)==10:
ex=re.findall('"(.*?)"',i)[0].strip().split(' ')
if ss[-1:] =='-':
continue
if len(ex)==3:
print(ss[0] + ',' + ss[3].strip('[')+','+ ex[0] + ',' + ex[1] + ',' + ex[2] + ',' \
+ str(ss[-2:-1]).strip('[').strip(']').strip("'")+','+str(ss[-1:]).strip('[').strip(']').strip("'"))
/
mkdir ~/test
hdfs dfs -get /flume/2015070104/* /home/ubuntu/test
cat /home/ubuntu/test/* |python map.py |sort -k 1 >aa.txt
2.
hdfs dfs -mkidr -p /0104/mydemo
hadoop_str.sh文件:
$HADOOP_HOME/bin/hadoop jar \
$HADOOP_HOME/share/hadoop/tools/lib/hadoop-streaming-2.7.3.jar \
-files "/home/ubuntu/map.py" \
-input /flume/2015070104/* \
-output /0104/mydemo/out00 \
-mapper "python3 map.py"
运行hadoop_str.sh
sh ./hadoop_srt.sh
3.
Hdfs dfs –cat /0104/mydemo/out00/part-00000 | head –n 10
Hdfs dfs –tail /0104/mydemo/out00/part-00000 | head –n 10
三、使用Hive对日志数据进行分析和统计
hive
create database if not exists qbc_hive location '/0104/db';
show databases;
use qbc_hive;
建表同时导入数据
create external table logdata(ip string,dates string,fun string,html string,xieyi string,node string,flow string,) row format delimited fields terminated by ',' location '/0104/mydemo/out00';
1、(代码、截图)哪个IP地址(域名) 访问次数最多?
select ip,count(ip) as count from logdata group by ip order by count desc limit 20;
2、(代码、截图)哪个IP地址(域名) 产生的流量最大?
select ip,sum(flow) as flow_sum from logdata group by ip order by flow_sum desc limit 1;
3、(代码、截图)产生流量最大的前10% IP地址(域名) ?
select c.ip from(select *,ntile(10) over(order by b.l desc) x from(select distinct a.ip,a.l from(select ip,sum(flow) over(partition by ip)l from logdata)a)b)c ;
4、(代码、截图)统计每个月的流量
select distinct a.my,a.month_total from(select substr(dates,4,8)as my,sum(flow) over(partition by substr(dates,4,8))month_total from logdata)a;
5、(代码、截图)统计每个月的流量 month_total 、 当年的总流量 year_total 及 每月流量占比 ( mont h_total /year_total )
select distinct a.month,a.month_total,a.year,a.year_total,( a.month_total / a.year_total)as nu from(select substr(dates,8,4)as year,sum(flow) over(partition by substr(dates,8,4))year_total,substr(dates,4,8)as month,sum(flow) over(partition by substr(dates,4,8))month_total from logdata)a;
6、(代码、截图)日志记录的时期,一共有多少错误的请求?(响应码以4或5开头)
select count(*) as count from logdata WHERE node LIKE '4%' or node LIKE '5%';
7、(代码、截图)找出每个月错误请求占当月总请求数量的占比。
select a.aa,b_count,a_count,b_count/a_count from
(select substr(dates,4,8)as aa,count(node) as a_count from logdata group by substr(dates,4,8)) a full join (select substr(dates,4,8)as bb,count(node) as b_count from logdata where node like '4%' or node like '5%'group by substr(dates,4,8))b on a.aa=b.bb;
四、对分析的结果进行可视化
1、(代码、截图)将上一步5的查询结果保存到本地文件
hive -e "use qbc_hive; set hive.cli.print.header=true; select distinct a.month,a.month_total,a.year,a.year_total,( a.month_total/a.year_total)as nu from(select substr(dates,8,4)as year,sum(flow) over(partition by substr(dates,8,4))year_total,substr(dates,4,8)as month,sum(flow) over(partition by substr(dates,4,8))month_total from logdata)a;" > qbc.csv
2、(代码)pyecharts实现饼图、折线图
import pandas as pd;
from pyecharts import options as opts
from pyecharts.charts import Pie, Line
df = pd.read_table("~/qbc.csv")
df1_x=df["a.month"].tolist()
df1_y = df["nu"].tolist()
data1=[[a[0],a[1]] for a in zip(df1_x,df1_y)]
c = (
Pie()
.add("",data1)
.set_global_opts(title_opts=opts.TitleOpts(title="每月流量占比"),legend_opts=opts.LegendOpts(pos_left='20%',type_='scroll'))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b} {d}%"))
.render()
)
df2_x=df["a.month"].tolist()
df2_y=df["a.month_total"].tolist()
l = (
Line()
.add_xaxis(df2_x)
.add_yaxis("", df2_y)
.set_global_opts(title_opts=opts.TitleOpts(title="统计每个月的流量"),legend_opts=opts.LegendOpts(pos_left='20%'))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{c}"))
.render("render_line.html")
)