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

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. springboot集成拦截器

    一.首先对HandlerInterceptor进行封装,封装为MappingInterceptor.封装的方法里添加拦截器起作用的路径addPathPatterns(),及需要排除路径的方法exclu ...

  2. urllib 库的代替品 requests 的用法

    Requuests 官方的介绍时多么的霸气,之所以那么霸气,是因为 Requestts 相比于 urllib 在使用方面上会让开发者感到更加的人性化.更加简洁.更加舒适,并且国外的一些公司也在使用re ...

  3. eclipse 快速隐藏所有方法的代码块

    java开发中往往一个class文件中会有很多方法,一个方法的实现代码有时候特别长 我个人就喜欢把无关的方法代码全部隐藏起来,只打开当前需要编辑的代码就行了(不喜欢看导航视图,容易头晕) Ctrl+s ...

  4. ApacheDbUtilsTest

    ApacheDbUtilsTest package p1; import com.DataSourceUtil; import entity.Student; import org.apache.co ...

  5. 嵌入式编程中使用 do{...} while(0) 的解释

    最近在看esp32的idf,有一些宏定义使用了do while(0)这种看起来好像没啥用的代码.然后我查了一下资料,发现在linux内核代码中经常用到这个东西! 现在就将这个东西整理一下. 为什么在内 ...

  6. MyEclipse和Eclipse中常用的快捷键

    ##########################快捷键分类速查##########################     *******常用类********[Ctrl+O]   显示类中方法和 ...

  7. Day11-G - Calendar Game HDU - 1079

    Adam and Eve enter this year’s ACM International Collegiate Programming Contest. Last night, they pl ...

  8. 设计模式课程 设计模式精讲 13-2 享元模式coding

    1 代码演练 1.1 代码演练1 1 代码演练 1.1 代码演练1 需求: 每周由随机部门经历做报告: 重点关注: a 该案例是单例模式和享元模式共同使用 b 外部传入的department是外部状态 ...

  9. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:制作一个超小按钮

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. Vue 中引入echarts

    安装依赖 npm install echarts -S 或者使用淘宝的镜像 npm install -g cnpm --registry=https://registry.npm.taobao.org ...