四、 模块开发----统计分析

select * from ods_weblog_detail limit 2;
+--------------------------+--------------------------------+--------------------------------+-------------------------------+---------------------------+----------------------------+--------------------------+------------------------+-------------------------+-----------------------------------------------+---------------------------+------------------------------------+---------------------------------+-----------------------------+-----------------------------+------------------------------+---------------------------------+------------------------------------+----------------------------+--+
| ods_weblog_detail.valid | ods_weblog_detail.remote_addr | ods_weblog_detail.remote_user | ods_weblog_detail.time_local | ods_weblog_detail.daystr | ods_weblog_detail.timestr | ods_weblog_detail.month | ods_weblog_detail.day | ods_weblog_detail.hour | ods_weblog_detail.request | ods_weblog_detail.status | ods_weblog_detail.body_bytes_sent | ods_weblog_detail.http_referer | ods_weblog_detail.ref_host | ods_weblog_detail.ref_path | ods_weblog_detail.ref_query | ods_weblog_detail.ref_query_id | ods_weblog_detail.http_user_agent | ods_weblog_detail.datestr |
+--------------------------+--------------------------------+--------------------------------+-------------------------------+---------------------------+----------------------------+--------------------------+------------------------+-------------------------+-----------------------------------------------+---------------------------+------------------------------------+---------------------------------+-----------------------------+-----------------------------+------------------------------+---------------------------------+------------------------------------+----------------------------+--+
| false | 194.237.142.21 | - | 2013-09-18 06:49:18 | 2013-09-18 | 06:49:18 | 09 | 18 | 06 | /wp-content/uploads/2013/07/rstudio-git3.png | 304 | 0 | "-" | NULL | NULL | NULL | NULL | "Mozilla/4.0(compatible;)" | 20130918 |
| false | 163.177.71.12 | - | 2013-09-18 06:49:33 | 2013-09-18 | 06:49:33 | 09 | 18 | 06 | / | 200 | 20 | "-" | NULL | NULL | NULL | NULL | "DNSPod-Monitor/1.0" | 20130918 |
+--------------------------+--------------------------------+--------------------------------+-------------------------------+---------------------------+----------------------------+--------------------------+------------------------+-------------------------+-----------------------------------------------+---------------------------+------------------------------------+---------------------------------+-----------------------------+-----------------------------+------------------------------+---------------------------------+------------------------------------+----------------------------+--+

1. 流量分析
--------------------------------------------------------------------------------------------
--计算每小时pvs,注意gruop by语句的语法
select count(*) as pvs,month,day,hour from ods_weblog_detail group by month,day,hour;
--------------------------------------------------------------------------------------------
1.1. 多维度统计PV总量
--第一种方式:直接在ods_weblog_detail单表上进行查询
1.1.1 计算该处理批次(一天)中的各小时pvs
drop table if exists dw_pvs_everyhour_oneday;
create table if not exists dw_pvs_everyhour_oneday(month string,day string,hour string,pvs bigint) partitioned by(datestr string);

insert into table dw_pvs_everyhour_oneday partition(datestr='20130918')
select a.month as month,a.day as day,a.hour as hour,count(*) as pvs from ods_weblog_detail a
where a.datestr='20130918' group by a.month,a.day,a.hour;

--计算每天的pvs
drop table if exists dw_pvs_everyday;
create table if not exists dw_pvs_everyday(pvs bigint,month string,day string);

insert into table dw_pvs_everyday
select count(*) as pvs,a.month as month,a.day as day from ods_weblog_detail a
group by a.month,a.day;

+----------------------+------------------------+----------------------+--+
| dw_pvs_everyday.pvs | dw_pvs_everyday.month | dw_pvs_everyday.day |
+----------------------+------------------------+----------------------+--+
| 10777 | 09 | 18 |
| 2993 | 09 | 19 |
+----------------------+------------------------+----------------------+--+

1.1.2 第二种方式:与时间维表关联查询

--维度:日
drop table dw_pvs_everyday;
create table dw_pvs_everyday(pvs bigint,month string,day string);

insert into table dw_pvs_everyday
select count(*) as pvs,a.month as month,a.day as day from (select distinct month, day from t_dim_time) a
join ods_weblog_detail b
on a.month=b.month and a.day=b.day
group by a.month,a.day;

--维度:月
drop table dw_pvs_everymonth;
create table dw_pvs_everymonth (pvs bigint,month string);

insert into table dw_pvs_everymonth
select count(*) as pvs,a.month from (select distinct month from t_dim_time) a
join ods_weblog_detail b on a.month=b.month group by a.month;

--另外,也可以直接利用之前的计算结果。比如从之前算好的小时结果中统计每一天的
Insert into table dw_pvs_everyday
Select sum(pvs) as pvs,month,day from dw_pvs_everyhour_oneday group by month,day having day='18';
+--------+--------+------+--+
| pvs | month | day |
+--------+--------+------+--+
| 10777 | 09 | 18 |
| 2993 | 09 | 19 |
+--------+--------+------+--+

--------------------------------------------------------------------------------------------
1.2 按照来访维度统计pv

--统计每小时各来访url产生的pv量,查询结果存入:( "dw_pvs_referer_everyhour" )

drop table if exists dw_pvs_referer_everyhour;
create table if not exists dw_pvs_referer_everyhour
(referer_url string,referer_host string,month string,day string,
hour string,pv_referer_cnt bigint) partitioned by(datestr string);

insert into table dw_pvs_referer_everyhour partition(datestr='20130918')
select http_referer,ref_host,month,day,hour,count(1) as pv_referer_cnt
from ods_weblog_detail
group by http_referer,ref_host,month,day,hour
having ref_host is not null
order by hour asc,day asc,month asc,pv_referer_cnt desc;

--统计每小时各来访host的产生的pv数并排序

drop table dw_pvs_refererhost_everyhour;
create table dw_pvs_refererhost_everyhour(ref_host string,month string,day string,hour string,ref_host_cnts bigint) partitioned by(datestr string);

insert into table dw_pvs_refererhost_everyhour partition(datestr='20130918')
select ref_host,month,day,hour,count(1) as ref_host_cnts
from ods_weblog_detail
group by ref_host,month,day,hour
having ref_host is not null
order by hour asc,day asc,month asc,ref_host_cnts desc;

---------------------------------------------------------------------------
1.3 统计pv总量最大的来源TOPN
--需求:按照时间维度,统计一天内各小时产生最多pvs的来源topN
分组求topN,先分组,再求每组内的topN

--row_number函数
select ref_host,ref_host_cnts,concat(month,day,hour),
row_number() over (partition by concat(month,day,hour) order by ref_host_cnts desc) as od
from dw_pvs_refererhost_everyhour;

--综上可以得出
drop table dw_pvs_refhost_topn_everyhour;
create table dw_pvs_refhost_topn_everyhour(
hour string,
toporder string,
ref_host string,
ref_host_cnts string
)partitioned by(datestr string);

insert into table dw_pvs_refhost_topn_everyhour partition(datestr='20130918')
select t.hour,t.od,t.ref_host,t.ref_host_cnts from
(select ref_host,ref_host_cnts,concat(month,day,hour) as hour,
row_number() over (partition by concat(month,day,hour) order by ref_host_cnts desc) as od
from dw_pvs_refererhost_everyhour) t where od<=3;

---------------------------------------------------------------------------------------------
1.4 人均浏览页数
--需求描述:统计今日所有来访者平均请求的页面数。
--总页面请求数/去重总人数

select '20130918',count(1) / count(distinct remote_addr) from ods_weblog_detail where datestr='20130918'; --13.4 (20.744 seconds)
select count(request) / count(distinct remote_addr) from ods_weblog_detail where datestr='20130918'; -- 13.4 (20.888 seconds)
select count(request) / (select count(1) from (select remote_addr from ods_weblog_detail group by remote_addr) t) from ods_weblog_detail;报错:
Error: Error while compiling statement: FAILED: ParseException line 1:25 cannot recognize input near 'select' 'count' '(' in expression specification (state=42000,code=40000)

select count(distinct remote_addr) from ods_weblog_detail; --1027(19.466 seconds)
select count(1) from (select remote_addr from ods_weblog_detail group by remote_addr) t; --1027(36.809 seconds)

drop table dw_avgpv_user_everyday;
create table dw_avgpv_user_everyday(
day string,
avgpv string);

insert into table dw_avgpv_user_everyday
select '20130918',count(1) / count(distinct remote_addr) from ods_weblog_detail where datestr='20130918';
--select '20130918',sum(b.pvs)/count(b.remote_addr) --这个方法实际上效率还要低一些
from
(
select remote_addr,count(1) as pvs from ods_weblog_detail where datestr='20130918'
group by remote_addr
) b;

第2节 网站点击流项目(下):3、流量统计分析,分组求topN的更多相关文章

  1. 第2节 网站点击流项目(下):6、访客visit分析

    0: jdbc:hive2://node03:10000> select * from ods_click_stream_visit limit 2;+--------------------- ...

  2. 第2节 网站点击流项目(下):7、hive的级联求和

    一.hive级联求和的简单例子: create table t_salary_detail(username string,month string,salary int)row format del ...

  3. 第1节 网站点击流项目(上):4、网站的数据采集,使用flume的taildir实现多个文件的监控采集

    一. 模块开发----数据采集 1. 需求 在网站web流量日志分析这种场景中,对数据采集部分的可靠性.容错能力要求通常不会非常严苛,因此使用通用的flume日志采集框架完全可以满足需求. 2. Fl ...

  4. 05.网站点击流数据分析项目_模块开发_ETL

    项目的数据分析过程在hadoop集群上实现,主要应用hive数据仓库工具,因此,采集并经过预处理后的数据,需 要加载到hive数据仓库中,以进行后续的挖掘分析. ETL:用来描述将数据从来源端经过抽取 ...

  5. 大数据学习——SparkStreaming整合Kafka完成网站点击流实时统计

    1.安装并配置zk 2.安装并配置Kafka 3.启动zk 4.启动Kafka 5.创建topic [root@mini3 kafka]# bin/kafka-console-producer. -- ...

  6. 精通Web Analytics 2.0 (6) 第四章:点击流分析的奇妙世界:实际的解决方案

    精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第四章:点击流分析的奇妙世界:实际的解决方案 到开始实际工作的时候了.哦耶! 在本章中,您将了解到一些最重要的网络分析报告,我将 ...

  7. 精通Web Analytics 2.0 (5) 第三章:点击流分析的奇妙世界:指标

    精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第三章:点击流分析的奇妙世界:指标 新的Web Analytics 2.0心态:搞定它.新的闪亮系列工具:是的.准备好了吗?当然 ...

  8. Python 利用 BeautifulSoup 爬取网站获取新闻流

    0. 引言 介绍下 Python 用 Beautiful Soup 周期性爬取 xxx 网站获取新闻流: 图 1 项目介绍 1. 开发环境 Python: 3.6.3 BeautifulSoup:   ...

  9. 【Spark】通过Spark实现点击流日志分析

    文章目录 数据大致内容及格式 统计PV(PageViews) 统计UV(Unique Visitor) 求取TopN 数据大致内容及格式 194.237.142.21 - - [18/Sep/2013 ...

随机推荐

  1. python第一章 python基础编程

    第一次学习python 首先python对于我来说是我学习的第三门语言,之前大一学习过了c和c++这两门语言. 接触一个新语言,首先应该的是搭载一下编译的环境.我们是老师给我们上传了的python3安 ...

  2. Centos7618安装后常见操作

    1.查看ip ip address  或者ip add 2.查看发行版本 yum -y install redhat-lsb lsb_release -a 3.查看内核版本 uname -r cat ...

  3. BUUCTF知识记录

    [强网杯 2019]随便注 先尝试普通的注入 发现注入成功了,接下来走流程的时候碰到了问题 发现过滤了select和where这个两个最重要的查询语句,不过其他的过滤很奇怪,为什么要过滤update, ...

  4. 使用git commit命令时会提示"Please tell me who you are"

    在命令行中输入 git config --global user.email "邮箱地址" git config --global user.name "用户名" ...

  5. Java中短路

    当使用逻辑运算符时,我们会遇到一种“短路”的现象.即一旦能够准确无误的确定整个表达式的值,就不再计算表达式余下的部分了.因此整个表达式靠后的部分有可能不被运算 /**短路 * @param args ...

  6. PTA的Python练习题(二)

    继续在PTA上练习Python (从 第2章-5 求奇数分之一序列前N项和  开始) 1. x=int(input()) a=i=1 s=0 while(i<=x): s=s+1/a a=a+2 ...

  7. OA:办公自动化———笔记一

    oa:办公自动化 1.对公司结构的管理 基础数据管理         部门进行管理     角色进行管理     权限进行管理  员工进行管理   2.流程管理          利用工作流技术对比较 ...

  8. Educational Codeforces Round 72 (Rated for Div. 2)D(DFS,思维)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int n,m,k=1;int c[5007] ...

  9. scrapy(创建scrapy工程)报错:“ ImportError:DLL load failed:找不到指定的模块”

    先要确定什么模块找不到 解决方法 windowa环境下加 ( --user) pip install -I cryptography --user

  10. 通过python 构建一个简单的聊天服务器

    构建一个 Python 聊天服务器 一个简单的聊天服务器 现在您已经了解了 Python 中基本的网络 API:接下来可以在一个简单的应用程序中应用这些知识了.在本节中,将构建一个简单的聊天服务器.使 ...