网站行为日志信息统计分析

news/2024/5/20 23:01:25/文章来源:https://blog.csdn.net/qq_41919792/article/details/106911078

网站行为日志信息统计分析

  • 一、开发环境
  • 二、项目思路
  • 三、系统实现
    • (一)、原始数据上传hdfs
    • (二)、数据清洗(第一遍)
    • (三)、数据清洗(第二遍)
    • (三)、通过hive对数据分析
  • 四、总结
  • 五、完整代码:
    • (一)、pom.xml文件
    • (二)、初始数据清洗:截取需要字段
    • (三)、数据清洗:清理网络连接中的资源文件和清洗不完整数据

一、开发环境

(一)、开发环境:
Windows + JDK1.8 + Hadop-2.9.2+Eclipse+linux
(二)、需要的只知识:
hdfs、mapreduce、hive、简单正则表达式、用户画像等等
(三)、开发时间:2019年1月

二、项目思路

(一)、对以采集的信息先上传到hdfs上
(二)、通过打标签,对网站进行用指标画像,提取出最能描述网站指标的字段,对网站性能负载进行综合调整、评估、优化!
(三)、根据对网站指标画提取出的特征字段,对数据进行清洗
(四)、分析网站的访问量,跳出率,网络连接状态,单个ip流量的总和等 ,对网站进行研究和分析

三、系统实现

(一)、原始数据上传hdfs

1、原始数据格式
在这里插入图片描述
2、上传到hdfs上:

hadoop dfs -put ./access_2015_03_30.log   /

(二)、数据清洗(第一遍)

1、利用正则表达式对原始数据处理,提取出想要的字段:
我对网址指标画像的字段是:

ip,time,timeArea,request,url,state,dataSize

public String[] parser(String line) {String str="27.19.74.143 - - [30/Mar/2015:17:38:20 +0800] \"GET /static/image/common/faq.gif HTTP/1.1\" 200 1127";//Pattern.compile("(27.19.74.143) - - \\[(30/Mar/2015:17:38:20) ( +0800)\\] \"(GET) (.*) HTTP/1.1\" (200) (1127)");Pattern compile = Pattern.compile("([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}) - - \\[(.*) ([-|+][0-9]{1,4})\\] \"([A-Z]{1,4}) (.*) HTTP/1.1\" ([0-9]*) ([0-9]*)");Matcher matcher = compile.matcher(line);//System.out.println(matcher.find());while(matcher.find()){if (matcher.group() != null) {System.out.println("成功!");String ip = matcher.group(1);String time = matcher.group(2);String timeArea = matcher.group(3);String request = matcher.group(4);String url = matcher.group(5);String state = matcher.group(6);String dataSize = matcher.group(7);SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss",Locale.ENGLISH);SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date;try {date = sdf1.parse(time);time = sdf.format(date);} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}return new String[]{ip,time,timeArea,request,url,state,dataSize};}}return new String[]{};}

(三)、数据清洗(第二遍)

1、通过mapreduce对已经提取出的字段再次清洗:清理网络连接中的资源文件和清理不完整数据

public class clean {static final String INPUT_PATH = "hdfs://192.168.56.30:9000/access_2015_03_30.log";static final String OUT_PATH = "hdfs://192.168.56.30:9000/user/hive/warehouse/t1";public static void main(String[] args) throws Exception {String str="27.19.74.143 - - [30/Mar/2015:17:38:20 +0800] \"GET /static/image/common/faq.gif HTTP/1.1\" 200 1127";System.out.println("==========================数据清洗============================");String[] parser = new LongParser().parser(str);for (int i = 0; i < parser.length; i++) {System.out.println("字段:"+(i+1)+" : "+parser[i]);}System.out.println("==========================数据清洗============================");Configuration conf = new Configuration();Job job =Job.getInstance(conf,clean.class.getSimpleName());job.setJarByClass(clean.class);//打jar包必须在这一行//文件的输入格式FileInputFormat.setInputPaths(job, new Path(INPUT_PATH));job.setInputFormatClass(TextInputFormat.class);//map序列化job.setMapperClass(MyMapper.class);job.setMapOutputKeyClass(LongWritable.class);job.setMapOutputValueClass(Text.class);//reduce序列化job.setReducerClass(MyReducer.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(LongWritable.class);//文件的输出格式String OUT_DIR =OUT_PATH;FileOutputFormat.setOutputPath(job, new Path(OUT_DIR));job.setOutputFormatClass(TextOutputFormat.class);//判断输出文件是否存在,若存在,则删除deleteOutDir(conf, OUT_DIR);job.waitForCompletion(true);}private static void deleteOutDir(Configuration conf, String OUT_DIR) throws IOException, URISyntaxException {FileSystem fileSystem = FileSystem.get(new URI(OUT_DIR), conf);if(fileSystem.exists(new Path(OUT_DIR))){fileSystem.delete(new Path(OUT_DIR), true);}}public static  class MyMapper extends Mapper<LongWritable, Text, LongWritable, Text>{@Overrideprotected void map(LongWritable key, Text value,org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,LongWritable,Text>.Context context)throws IOException ,InterruptedException {String line = value.toString();String[] parser = new LongParser().parser(line);//清理网络连接中的资源文件if (line.contains(".gif")||line.contains("jpg")||line.contains("png")||line.contains(".css")||line.contains(".js")) {return;}//清理不完整数据if(parser.length != 7){return;}Text text = new Text();text.set(parser[0]+"\t"+parser[1]+"\t"+parser[2]+"\t"+parser[3]+"\t"+parser[4]+"\t"+parser[5]+"\t"+parser[6]+"\t");context.write(key, text);}}public static	class  MyReducer extends Reducer<LongWritable,Text, Text, NullWritable>{@Overrideprotected void reduce(LongWritable arg0, Iterable<Text>text,Reducer<LongWritable, Text, Text, NullWritable>.Context context) throws IOException, InterruptedException {// TODO Auto-generated method stubfor (Text value : text) {context.write(value, NullWritable.get());}}}}

(三)、通过hive对数据分析

1、创建表

create table t1(ip String,
time String,
timeArea String,
request String,
url String,
State String,
dataSize int
)row format delimited fields terminated by "\t";

2、pageview:用户的总访问量

select count(1) as PV from t1;

3、uv:独立用户(去重)

select count(distinct ip) as UV from t1;

4、只浏览了一次就离开的用户

select count(1) from t1 group by ip having count(1)=1;

5、只浏览了一次就离开用户的总数

select count(1) from (select count(1) from t1 group by ip having count(1)=1) nums;

6、所有浏览的总数

select ip,count(1) as nums from t1 group by ip;

7、跳出率

select sum(case when a.nums=1 then 1 else 0 end)/sum(1)
from(select count(1) as nums from t1 group by ip) a;

结果:7348/21645=0.33947793947793947

跳出率(取精度):round()

select round(sum(case when a.nums=1 then 1 else 0 end)/sum(1)*100,2)
from(select count(1) as nums from t1 group by ip) a;

结果:33.95

跳出率(字符转换):concat()

select concat(round(sum(case when a.nums=1 then 1 else 0 end)/sum(1)*100,2),"%")
from(select count(1) as nums from t1 group by ip) a;

结果:33.95%

8、ip浏览量的top100

select ip,count(1) as nums from t1 group by ip sort by nums desc limit 100;

9、统计时区

select timeArea,count(1) from t1 group by timeArea;

10、统计页面热点

select url,count(1) as nums from t1 group by url sort by nums desc limit 100;

11、网站用户连接状态

select state,count(1) as nums from t1 group by state;

12、单个ip流量的总和

select ip,sum(dataSize) as totalSize from t1 group by ip sort by totalSize desc limit 100;

四、总结

通过完成此次项目,学到了很多东西!我觉得最难的地方是正则表达式,因为以前用正则表达式较少,所以就没有学习正则表达式,真到用的时候不会,很着急!就只好现学了,通过本次项目,对正则表达式有了新的认识和理解。通过这次项目对用户画像,如何给某一事物打标签有了深刻的了解,同时也对mapreduce这一知识进行了复习掌握,最终要的是对hive的掌握也有了一定程度上的提升!真的是受益颇多!

五、完整代码:

(一)、pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>Clean</groupId><artifactId>clean</artifactId><version>0.0.1-SNAPSHOT</version><dependencies><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>2.2.0</version></dependency><dependency><groupId>jdk.tools</groupId><artifactId>jdk.tools</artifactId><version>1.8</version><scope>system</scope><systemPath>D:/java/jdk1.8/lib/tools.jar</systemPath></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-hdfs</artifactId><version>2.2.0</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-mapreduce-client-core -->
<dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-mapreduce-client-core</artifactId><version>2.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
<dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-client</artifactId><version>1.2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-mapreduce-client-common --><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-mapreduce-client-common</artifactId><version>2.2.0</version></dependency><!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version>
</dependency><!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope>
</dependency></dependencies></project>

初始数据清洗:截取需要字段

(二)、初始数据清洗:截取需要字段

package data;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class LongParser {public String[] parser(String line) {String str="27.19.74.143 - - [30/Mar/2015:17:38:20 +0800] \"GET /static/image/common/faq.gif HTTP/1.1\" 200 1127";//Pattern.compile("(27.19.74.143) - - \\[(30/Mar/2015:17:38:20) ( +0800)\\] \"(GET) (.*) HTTP/1.1\" (200) (1127)");Pattern compile = Pattern.compile("([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}) - - \\[(.*) ([-|+][0-9]{1,4})\\] \"([A-Z]{1,4}) (.*) HTTP/1.1\" ([0-9]*) ([0-9]*)");Matcher matcher = compile.matcher(line);//System.out.println(matcher.find());while(matcher.find()){if (matcher.group() != null) {System.out.println("成功!");String ip = matcher.group(1);String time = matcher.group(2);String timeArea = matcher.group(3);String request = matcher.group(4);String url = matcher.group(5);String state = matcher.group(6);String dataSize = matcher.group(7);SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss",Locale.ENGLISH);SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date;try {date = sdf1.parse(time);time = sdf.format(date);} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}return new String[]{ip,time,timeArea,request,url,state,dataSize};}}return new String[]{};}}

(三)、数据清洗:清理网络连接中的资源文件和清洗不完整数据

package data;import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;public class clean {static final String INPUT_PATH = "hdfs://192.168.56.30:9000/access_2015_03_30.log";static final String OUT_PATH = "hdfs://192.168.56.30:9000/user/hive/warehouse/t1";public static void main(String[] args) throws Exception {String str="27.19.74.143 - - [30/Mar/2015:17:38:20 +0800] \"GET /static/image/common/faq.gif HTTP/1.1\" 200 1127";System.out.println("==========================数据清洗============================");String[] parser = new LongParser().parser(str);for (int i = 0; i < parser.length; i++) {System.out.println("字段:"+(i+1)+" : "+parser[i]);}System.out.println("==========================数据清洗============================");Configuration conf = new Configuration();Job job =Job.getInstance(conf,clean.class.getSimpleName());job.setJarByClass(clean.class);//打jar包必须在这一行//文件的输入格式FileInputFormat.setInputPaths(job, new Path(INPUT_PATH));job.setInputFormatClass(TextInputFormat.class);//map序列化job.setMapperClass(MyMapper.class);job.setMapOutputKeyClass(LongWritable.class);job.setMapOutputValueClass(Text.class);//reduce序列化job.setReducerClass(MyReducer.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(LongWritable.class);//文件的输出格式String OUT_DIR =OUT_PATH;FileOutputFormat.setOutputPath(job, new Path(OUT_DIR));job.setOutputFormatClass(TextOutputFormat.class);//判断输出文件是否存在,若存在,则删除deleteOutDir(conf, OUT_DIR);job.waitForCompletion(true);}private static void deleteOutDir(Configuration conf, String OUT_DIR) throws IOException, URISyntaxException {FileSystem fileSystem = FileSystem.get(new URI(OUT_DIR), conf);if(fileSystem.exists(new Path(OUT_DIR))){fileSystem.delete(new Path(OUT_DIR), true);}}public static  class MyMapper extends Mapper<LongWritable, Text, LongWritable, Text>{@Overrideprotected void map(LongWritable key, Text value,org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,LongWritable,Text>.Context context)throws IOException ,InterruptedException {String line = value.toString();String[] parser = new LongParser().parser(line);//清理网络连接中的资源文件if (line.contains(".gif")||line.contains("jpg")||line.contains("png")||line.contains(".css")||line.contains(".js")) {return;}//清理不完整数据if(parser.length != 7){return;}Text text = new Text();text.set(parser[0]+"\t"+parser[1]+"\t"+parser[2]+"\t"+parser[3]+"\t"+parser[4]+"\t"+parser[5]+"\t"+parser[6]+"\t");context.write(key, text);}}public static	class  MyReducer extends Reducer<LongWritable,Text, Text, NullWritable>{@Overrideprotected void reduce(LongWritable arg0, Iterable<Text>text,Reducer<LongWritable, Text, Text, NullWritable>.Context context) throws IOException, InterruptedException {// TODO Auto-generated method stubfor (Text value : text) {context.write(value, NullWritable.get());}}}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_905302.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Bliface借区块链定义视频网站3.0

超级解霸 能看懂这个词的多数都已当爹了&#xff0c;那是一个没有还没有在线播放的年代&#xff0c;光盘解码器是电脑的标配&#xff0c;后来逐渐有人在BBS上享视频给大家下载&#xff0c;我们暂且定为第一代 土豆 2005年4月15日土豆网正式上线&#xff0c;在线视频网站如雨后…

IE10 如何保护您的电脑免于恶意网站危害

NSS 实验室不久前发表了一份关于浏览器拦截恶意程序的 评估报告 (英文) 。在这个报告中可以看出最安全的浏览器是 Internet Explorer 10 。 IE10 可以拦截 99.1% 的已知恶意程序。日常生活中在网络上进行的社交活动、购物、工作等皆有可能会将您的个人信息透露给其他人。Micro…

2017年9月3日 实现网站的权限管理

现在各个企业管理网站对登录的账号都要进行权限管理&#xff0c;并且相当重要&#xff0c;每个账号登录进去所能看到的东西大不相同&#xff0c;下面是实现该功能的一个的一种方法。 需求&#xff1a; 权限&#xff1a;权限是使用者操作系统中功能模块的能力&#xff0c;如“角…

我的个人网站又恢复了,欢迎登陆

我的个人网站的网址是http://www.myjavaserver.com/~maqujun . 前一段日子停掉了付费的Hosting的个人站点。自己还伤心了好一整子呢。在Javaeye的Blog里还郑重其事的写了一篇哀悼我那个人网站的悼文。可今天又恢复了&#xff01;可见多变不仅仅是女人的特权&#xff0c;呵呵。其…

网站SEO关键词排名优化经验分享-建站后的推广细节

首先搜索引擎后台会有蜘蛛爬虫技术&#xff0c;根据网站的关百键词密度&#xff0c;内容更新时间上&#xff0c;网站的打开速度&#xff0c;页面跳出率度等因素来进行综合排序。 一般网站做好后&#xff0c;想获取好的收录&#xff0c;可以向搜索引擎提交收录申请&#xff0c;这…

对于网站优化该怎么去做外链?

外链是建网站做优化的重要工作之一&#xff0c;做外链质量的高低直接影响着最终的网站关键词排名&#xff0c;也影响流量和权重。 做外链其实说起来也不是很难&#xff0c;做SEO的基本都知道去哪里做这些网站的外链&#xff0c;但是难就在于这些网站的一个规则和你文章的质量上…

企业网站百度排名第一需要多少外链

网站排名百度第一&#xff0c;到底需要多少外链呢&#xff1f;这是做SEO的同学最大的疑问&#xff0c;这个问题比较宽泛&#xff0c;网站的行业不同&#xff0c;外链的需求也不一样。那么我们要如何审视这个问题呢&#xff1f; 外链计划的制定有以下几个步骤&#xff1a; 一、…

天津网站建设-文率科技天津众多网站建设中的楷模

天津网站建设-文率科技是天津企业建站的首选公司&#xff0c;我们能够为您提供全方位一站式的解决方案&#xff0c;利用我们专业的建站技术&#xff0c;优秀的设计水平&#xff0c;采用目前最先进的送搜索引擎&#xff0c;技术娴熟的人工seo优化&#xff0c;让您的网站拥有高的…

网站为什么要做外链?

SEO工作人员都知道&#xff0c;外链是SEO工作中不可缺少的部分。那么外链是如何对网站的排名起到作用的呢&#xff1f;又为什么如此重要呢&#xff1f; 什么是外链&#xff1f; 从字面意思来看&#xff0c;外链可以拆解成“外部链接”&#xff0c;是指从别的网站链接向自己网站…

网站架构之缓存应用(1)概念篇

网站缓存这个话题并不新颖&#xff0c;但是能否将它用好&#xff0c;可是一门学问&#xff0c;同一件工具在不同人的手中会做出不同的事情来。这里我来分享总结下我对于网站架构中缓存应用的一些看法和经验&#xff0c;大家有好的想法可以补充。 第一&#xff1a;缓存的…

Tomcat5.5.x配置整理 - 发布webapp到网站根目录

1。直接复制到ROOT目录下。 2.因为无法创建无名字的xml文件&#xff0c;并且在xml文件里指定path也是无效的(tomcat靠文件名字来判断的)&#xff0c; 因此必须在server.xml里写下面一段&#xff1a; 查看 复制到剪切板 打印 Xml代码 <ContextdocBase"${catalina.home}/…

仿商业网站——商品评分效果的实现

个人觉得效果还是不错的。废话不多说&#xff0c;先看看HTML的布局&#xff1a; rel"stylesheet" type"text/css" href"gradeStyle.css"/><script src"jquery-1.11.1.js"></script><script src"gradeFunction.…

仿商业网站——商品评分效果实现【提高篇】

看过之前的那一篇仿商业网站——商品评分效果的实现,就可能发现之前的那种实现方式还差一点点效果:就是当鼠标移动到每个星星上的时候,星星最好有动态的变化,如下图: 鼠标移动到星星上面后,星星的状态: 这里用到一点点CSS sprite的技术,

Yahoo——网站性能优化35条黄金守则

Yahoo!的 Exceptional Performance团队为改善 Web性能带来最佳实践。他们为此进行了一系列的实验、开发了各种工具、写了大量的文章和博客并在各种会议上参与探讨。最佳实践的核心就是旨在提高网站性能。 Excetional Performance 团队总结出了一系列可以提高网站速度的方法。可…

IIS 7 Web服务器上部署ASP.NET网站

开发环境Visual Studio 2010/2008皆可&#xff0c;数据库SQL Server 2008/2005。接下来就以这样的环境部署我们的网站&#xff0c;按下图来配置&#xff1a; 第一步&#xff0c;别偷懒&#xff0c;先看看角色添加了没&#xff0c;没有就点击右上边“添加角色”连接添加吧。 第二…

Struts 专业应用: 用 Struts 对象关系桥、Lucene和Velocity 构建网站

Struts 专业应用: 用 Struts 对象关系桥、Lucene和Velocity 构建网站要想建设一个可维护、可扩展的网站&#xff0c;必须在编写每一行代码前花大力气进行设计规划。可是&#xff0c;通过运用framework&#xff0c;大多数困难都是可以克服的。这本书展示了如何利用Jakarta Strut…

中大型网站架构的演变之路

一个成熟的网站架构并不是一开始设计就具备高可用、高伸缩、高性能等特性的&#xff0c;它是随着用户量和业务线不断增加&#xff0c;基础架构才逐渐健壮的。在发展初期&#xff0c;一般都是从0到1&#xff0c;不会一上来就整一些大而全的架构&#xff0c;也很少人这么任性。 说…

网站性能优化实战

网站性能优化实战——从12.67s到1.06s的故事 JAVA高级架构 昨天 网站性能监测与优化策略 0.引言 作为互联网项目&#xff0c;最重要的便是用户体验。在举国“互联网”的热潮中&#xff0c;用户至上也已经被大多数企业所接收&#xff0c;特别是在如今移动端快速发展的时代&#…

20个颇具创意的移动网站设计案例

CB2 North Carolina Wine Country Aerie Marlboro Etsy Airbnb J Taylor Design Sun-Maid Wrigley Zappos Stowe Dungs Burton Yasalam Tim Hortons Sony Tablets Tyler Michaud Microsoft Toyota FXNINE

国内旅游社区网站推荐

蚂蜂窝是一个旅游分享社区,在蚂蜂窝你可以交换资讯,分享旅行。帮助这里的友邦,或者是获得帮助。 也可以交流功略、美食、音乐、摄影日记,以及与旅行有关的零零种种。(