网站日志流量分析系统之离线分析(自动化脚本)

摘要:
altertablelogdemoaddpartition(reportTime=“${tday}”)位置“/logdemo/reportTime=${tday}”;pv_tab.pvfrom(selectcount(*)aspvfromdataclearwherereportTime='${tday}')aspv_tab;

一、概述

  网站日志流量分析系统之数据清洗处理(离线分析)已经将数据进行清洗处理,但是处理过程分区信息(日期reportTime是写死的),而且hql语句也是需要人工手动去执行,在实际开发中肯定不会容忍这样的事情,所以让程序自动程序那些hql语句,作离线分析是当下我们要解决的问题。

二、自动化脚本

(1)编写logdemo.hql脚本

use logdb;

alter table logdemo add partition (reportTime='${tday}') location '/logdemo/reportTime=${tday}';

insert into dataclear partition(reportTime='${tday}') select split(url,'-')[2],urlname,ref,uagent,stat_uv,split(stat_ss,'_')[0],split(stat_ss,'_')[1],split(stat_ss,'_')[2],cip from logdemo where reportTime = '${tday}';

insert into tongji1_temp select '${tday}','pv',pv_tab.pv from (select count(*) as pv from dataclear where reportTime='${tday}') as pv_tab;create table tongji1_temp(reportTime string,k string,v string);

insert into tongji1_temp select '${tday}','uv',uv_tab.uv from (select count(distinct uvid) as uv from dataclear where reportTime='${tday}') as uv_tab;

insert into tongji1_temp select '${tday}','vv',vv_tab.vv from (select count(distinct ssid) as vv from dataclear where reportTime='${tday}') as vv_tab;

insert into tongji1_temp select '${tday}','br',br_tab.br from (select round(br_left_tab.br_count / br_right_tab.ss_count,4) as br from (select count(*) as br_count from (select ssid from dataclear group by ssid having count(*) = 1) as br_intab) br_left_tab,(select count(distinct ssid) as ss_count from dataclear where reportTime='${tday}') as br_right_tab) as br_tab;

insert into tongji1_temp select '${tday}','newip',newip_tab.newip from (select count(distinct out_dc.cip) as newip from dataclear as out_dc where out_dc.reportTime='${tday}' and out_dc.cip not in (select in_dc.cip from dataclear as in_dc where datediff('${tday}',in_dc.reportTime)>0)) as newip_tab;

insert into tongji1_temp select '${tday}','newcust',newcust_tab.newcust from (select count(distinct out_dc.uvid) as newcust from dataclear as out_dc where out_dc.reportTime='${tday}' and out_dc.uvid not in (select in_dc.uvid from dataclear as in_dc where datediff('${tday}',in_dc.reportTime)>0)) as newcust_tab;

insert into tongji1_temp select '${tday}','avgtime',avgtime_tab.avgtime from (select avg(at_tab.usetime) as avgtime from (select max(sstime) - min(sstime) as usetime from dataclear where reportTime='${tday}' group by ssid) as at_tab) as avgtime_tab;

insert into tongji1_temp select '${tday}','avgdeep',avgdeep_tab.avgdeep from (select avg(ad_tab.deep) as avgdeep from (select count(distinct url) as deep from dataclear where reportTime='${tday}' group by ssid) as ad_tab) as avgdeep_tab;

insert into tongji1 select  '${tday}', pv_tab.pv, uv_tab.uv, vv_tab.vv, newip_tab.newip, newcust_tab.newcust, avgtime_tab.avgtime, avgdeep_tab.avgdeep from (select v as pv from tongji1_temp where reportTime='${tday}' and k='pv') as pv_tab, (select v as uv from tongji1_temp where reportTime='${tday}' and k='uv') as uv_tab, (select v as vv from tongji1_temp where reportTime='${tday}' and k='vv') as vv_tab, (select v as newip from tongji1_temp where reportTime='${tday}' and k='newip') as newip_tab, (select v as newcust from tongji1_temp where reportTime='${tday}' and k='newcust') as newcust_tab, (select v as avgtime from tongji1_temp where reportTime='${tday}' and k='avgtime') as avgtime_tab, (select v as avgdeep from tongji1_temp where reportTime='${tday}' and k='avgdeep') as avgdeep_tab; 

  注:不要有中文注释。

 (2)进入hive的bin目录启动该脚本:-d表示变量,-f表示文件(这里你可能注意到我们还是手写的日期,别急。)

[root@hadoopalone bin]# ./hive -d tday=2019-09-07 -f /home/software/logdemo.hql

(3)设定linux的定时任务,要求每天零点过后,调用这个脚本(./hive -d tday=date "+%Y-%m-%d" -f /home/software/logdemo.hql)

  1.可以在Linux配置文件/etc/crontab中配置定时任务,配置方式:

  网站日志流量分析系统之离线分析(自动化脚本)第1张

 其中可以使用通配符:

  ①星号(*):代表每的意思,例如month字段如果是星号,则表示每月都执行该命令操作。

       ②逗号(,):表示分隔时段的意思,例如,“1,3,5,7,9”。

       ③中杠(-):表示一个时间范围,例如“2-6”表示“2,3,4,5,6”。  

       ④正斜线(/):可以用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,例如*/10,如果用在minute字段,表示每十分钟执行一次。

 实例: 

 ##每月每天凌晨3点30分和中午12点20分执行test.sh脚本
30 3,12 * * *  /home/test.sh           
 
##每月每天每隔6小时的每30分钟执行test.sh脚本
30 */6 * * *  /home/test.sh
 
##每月每天早上8点到下午18点每隔2小时的每30分钟执行test.sh脚本
30 8-18/2 * * * /etc/init.d/network restart
 
##每月每天晚上21点30分执行test.sh脚本
30 21 * * *  /etc/init.d/network restart
 
##每月1号、10号、22号凌晨4点45分执行test.sh脚本
45 4 1,10,22 * * /etc/init.d/network restart
 
##8月份周一、周日凌晨1点10分执行test.sh脚本
10 1 * 8 6,0 /etc/init.d/network restart
 
##每月每天每小时整点执行test.sh脚本
00 */1 * * *  /etc/init.d/network restart

  2.管理crontab命令 

service crond start //启动服务
service crond stop //关闭服务
service crond restart //重启服务
service crond reload //重新载入配置

  3.本项目的配置

  网站日志流量分析系统之离线分析(自动化脚本)第2张

  4.启动crontab配置即可完成定时任务:service crond start //启动服务

 三、总结

  至此已经完成了hql脚本的自动化执行,接下来就是对数据清洗处理完成的结果进行可视化展示:网站日志流量分析系统之数据可视化展示

免责声明:文章转载自《网站日志流量分析系统之离线分析(自动化脚本)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇单应矩阵关于Lucene全文检索相关技术下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

『NodeJS』简单的本地 DNS 代理脚本

风飘林's Blog 牛博 『NodeJS』简单的本地 DNS 代理脚本 var dgram = require("dgram"); const DNSADDRESS = "8.8.8.8"; const DNSPORT = "53"; console.log("DNS Server: " + DNSADDRESS + ":" + DNSPORT);...

Laravel 缓存操作

Laravel 为不同的缓存系统提供了统一的 API。缓存配置位于 config/cache.php。 Laravel 目前支持主流的缓存后端如 File、Memcached 和 Redis 等,默认是使用文件缓存。 env文件配置 ,推荐修改这里 config/cache.php 文件,不建议直接修改 默认laravel支持缓存介质:"apc", "...

DELPHI移动端支付宝支付

Delphi XE7 Android 应用接入支付宝SDK的方法     1      应用场景和准备工作: 采用XE系列开发的android apps。 apps中需要集成支付宝的支付能力。 支付到指定的商家(一般就是软件开发商自己啦),商家需要事先在支付宝的开放平台申请开通【支付宝无线快捷支付】,具体请百度。 开通【无线快捷支付】后,支付宝应该返回给...

torch 深度学习(5)

torch 深度学习(5) mnist torch siamese deep-learning 这篇文章主要是想使用torch学习并理解如何构建siamese network。 siamese network的结构如下: 1486455020988.jpg 使用的数据集:mnist 手写数据集 实验目的:通过孪生网络使得同一类的尽可能的靠近...

Vue2.0进阶组件 短信倒计时组件

原本我想隔个几天再发文章,刚好今天项目上线,环境有问题,导致只有干等,刚好要为公司打造一套属于公司自己的一系列功能组件,这个使命就交给我了,大家也一直叫我来点干货,说实话我只是一个湿货,肚子里干一点就给你们出点货,那从今天开始不看岛国片系列教程视频,不但自撸,还教你撸............你懂的!!最强vue组件 写之前我只想说如果看到错别字,就别给我点...

angularJs学习笔记-入门

1.angularJs简介   angularJs是一个MV*的javascript框架(Model-View-Whatever,不管是MVVM还是MVC,统归MDV(model drive view)),其实是由google推出的SPA(single-page-application)应用框架。它的用于 数据在后端和前端之间的双向绑定。这就意味着你在后台...