百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

Hive日志案例分析应用

bigegpt 2024-09-20 14:03 4 浏览

先补充两个知识点:CASE … WHEN和CAST类型转换

CASE … WHEN…的用法

创建数据库

创建员工表

create table emp(

empno int comment '员工编号',

ename string comment '员工姓名',

job string comment '员工职位',

mgr int comment '领导编号',

hiredate string comment '入职时间',

sal double comment '薪资',

comm double comment '奖金',

deptno int comment '部门编号'

)row format delimited fields terminated by '\t';

将数据上传

导入数据

load data local inpath '/data/test/emp.txt' into table emp;

查看数据

其中comm有很多null值

select comm from emp;

那怎么不让comm这列显示为Null呢?

就可以使用case when,语句

select empno,

case

when comm is NULL then sal+0

else sal+comm

end

from emp;

可以加别名,进行多层判断。语句

select empno,

case

when sal< 1000 then 'low'

when sal >=1000 and sal < 3000 then 'middle'

when sal >=3000 and sal < 5000 then 'high'

else 'very high'

end

from emp;

cast类型转换

查看下表结构

我们将double类型转换为string类型

语句

create table cast_table as select empno,ename ,job ,cast(sal as string) new_sal from emp;

查看表结构

日志案例需求分析:

指标:

(1)日期:最后统计分析的时候根据日期进行分组,可以建立分区表

(2)PV:count(url)

(3)UV: count(distinct guid)

(4)登录人数:user_id 有值,可以登录

(5)游客人数:user_id 无值,非登录人员

(6)平均访问时长:每个用户登录页面之后都会产生一个session_id,统计每个session会话平均的停留时间。求到访问时长:进入页面第一条时间戳,最后离开页面的最后一条时间戳。平均访问时长:按照session_id进行分组,求到平均访问时长。

(7)二跳率:一个用户在一个session会话中,点击的页面大于等于2的次数就是二跳率。求访问页面超过2的用户,统计PV大于等于2的用户再除以总的人数。

(8)独立ip:统计ip去重

开始将数据导入到Hive中

创建数据库

Create database track_log_ip;

使用数据库

创建表

create table log_ip_source(

id string,

url string,

referer string,

keyword string,

type string,

guid string,

pageId string,

moduleId string,

linkId string,

attachedInfo string,

sessionId string,

trackerU string,

trackerType string,

ip string,

trackerSrc string,

cookie string,

orderCode string,

trackTime string,

endUserId string,

firstLink string,

sessionViewNo string,

productId string,

curMerchantId string,

provinceId string,

cityId string,

fee string,

edmActivity string,

edmEmail string,

edmJobId string,

ieVersion string,

platform string,

internalKeyword string,

resultSum string,

currentPage string,

linkPosition string,

buttonPosition string

)partitioned by(date string)

row format delimited fields terminated by '\t';

导入数据

load data local inpath '/data/test/data1' into table log_ip_source partition(date='2015082818');

数据清洗

会话信息的关键指标:

trackerU:访问渠道:通过什么方式进入到网站:收藏夹、手输网址、论坛、博客等

landing_url着陆页:用户进入网站的第一个页面,需要获取第一条记录,分析同一个session会话中的第一个页面。

Landing_url_ref着陆页之前的页面:需要获取第一条记录

创建会话信息表

create table session_info(

session_id string ,

guid string ,

trackerU string ,

landing_url string ,

landing_url_ref string ,

user_id string ,

pv string ,

stay_time string ,

min_trackTime string ,

ip string ,

provinceId string

)partitioned by (date string)

ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' ;

加载数据

针对每个会话进行一个group by sessionId

由于sessionid分组之后会得到多条记录 ,那么就是对于每个session里面统计pv

创建临时表

create table session_tmp as

select

sessionId session_id,

max(guid) guid,

max(endUserId) user_id,

count(distinct url) pv,

(unix_timestamp(max(trackTime)) - unix_timestamp(min(trackTime)) ) stay_time,

min(trackTime) min_trackTime,

max(ip) ip,

max(provinceId) provinceId

from log_ip_source where date='2015082818'

group by sessionId;

这三个字段

trackerU,landing_url,landing_url_ref

(1)从源表中获取每一条记录的trackerU,landing_url,landing_url_ref

(2)从源表中获取每一条记录的时间

(3)然后进行最小时间与源表当中最小时间的join,获取到trackerU,landing_url,landing_url_ref

创建第二张临时表

create table track_tmp as

select

sessionId session_id,

trackTime trackTime,

url landing_url,

referer landing_url_ref,

trackerU trackerU

from log_ip_source

where date='2015082818';

Join连接

insert overwrite table session_info partition(date='2015082818')

select

a.session_id session_id,

max(a.guid) guid,

max(b.trackerU) trackerU,

max(b.landing_url) landing_url,

max(b.landing_url_ref) landing_url_ref,

max(a.user_id) user_id,

max(a.pv) pv,

max(a.stay_time) stay_time,

max(a.min_trackTime) min_trackTime,

max(a.ip) ip,

max(a.provinceId) provinceId

from session_tmp a join track_tmp b on

a.session_id = b.session_id and a.min_trackTime = b.trackTime

group by a.session_id;

数据分析

语句

create table result as

select

date date,

sum(pv) pv,

count(distinct guid) guid,

count( distinct case when user_id is not null then guid else null end ) login_user,

count( distinct case when user_id is null then guid else null end ) visitor,

avg(stay_time) avg_time,

(count(case when pv>2 then session_id else null end)/count(session_id) ) session_jump,

count(distinct ip) ip

from session_info where date='2015082818'

group by date;

查看下结果

这个地方有问题,游客人数为0

重新写语句

create table qw as

select

date date,

sum(pv) PV,

count(distinct guid) UV,

count(distinct case when length(user_id)!=0 then guid else null end) login_user,

count(distinct case when length(user_id)=0 then guid else null end) visitor,

avg(stay_time) avg_time,

(count(case when pv>=2 then session_id else null end)/count(session_id)) second_jump,

count(distinct ip) IP

from session_info

where date='2015082818'

group by date;

查看结果

相关推荐

pyproject.toml到底是什么东西?(py trim)

最近,在Twitter上有一个Python项目的维护者,他的项目因为构建失败而出现了一些bug(这个特别的项目不提供wheel,只提供sdist)。最终,发现这个bug是由于这个项目使用了一个pypr...

BDP服务平台SDK for Python3发布(bdp数据平台)

下载地址https://github.com/imysm/opends-sdk-python3.git说明最近在开发和bdp平台有关的项目,用到了bdp的python的sdk,但是官方是基于p...

Python-for-Android (p4a):(python-for-android p4a windows)

一、Python-for-Android(p4a)简介Python-for-Android(p4a),一个强大的开发工具,能够将你的Python应用程序打包成可在Android设备上运行...

Qt for Python—Qt Designer 概览

前言本系列第三篇文章(QtforPython学习笔记—应用程序初探)、第四篇文章(QtforPython学习笔记—应用程序再探)中均是使用纯代码方式来开发PySide6GUI应用程序...

Python:判断质数(jmu-python-判断质数)

#Python:判断质数defisPrime(n):foriinrange(2,n):ifn%i==0:return0re...

为什么那么多人讨厌Python(为什么python这么难)

Python那么棒,为什么那么多人讨厌它呢?我整理了一下,主要有这些原因:用缩进替代大括号许多人抱怨Python完全依赖于缩进来创建代码块,代码多一点就很难看到函数在哪里结束,那么你就需要把一个函数拆...

一文了解 Python 中带有 else 的循环语句 for-else/while-else

在本文中,我们将向您介绍如何在python中使用带有else的for/while循环语句。可能许多人对循环和else一起使用感到困惑,因为在if-else选择结构中else正常...

python的numpy向量化语句为什么会比for快?

我们先来看看,python之类语言的for循环,和其它语言相比,额外付出了什么。我们知道,python是解释执行的。举例来说,执行x=1234+5678,对编译型语言,是从内存读入两个shor...

开眼界!Python遍历文件可以这样做

来源:【公众号】Python技术Python对于文件夹或者文件的遍历一般有两种操作方法,一种是至二级利用其封装好的walk方法操作:import osfor root,d...

告别简单format()!Python Formatter类让你的代码更专业

Python中Formatter类是string模块中的一个重要类,它实现了Python字符串格式化的底层机制,允许开发者创建自定义的格式化行为。通过深入理解Formatter类的工作原理和使用方法,...

python学习——038如何将for循环改写成列表推导式

在Python里,列表推导式是一种能够简洁生成列表的表达式,可用于替换普通的for循环。下面是列表推导式的基本语法和常见应用场景。基本语法result=[]foriteminite...

详谈for循环和while循环的区别(for循环语句与while循环语句有什么区别)

初九,潜龙勿用在刚开始使用python循环语句时,经常会遇到for循环和while循环的混用,不清楚该如何选择;今天就对这2个循环语句做深入的分析,让大家更好地了解这2个循环语句以方便后续学习的加深。...

Python编程基础:循环结构for和while

Python中的循环结构包括两个,一是遍历循环(for循环),一是条件循环(while循环)。遍历循环遍历循环(for循环)会挨个访问序列或可迭代对象的元素,并执行里面的代码块。foriinra...

学习编程第154天 python编程 for循环输出菱形图

今天学习的是刘金玉老师零基础Python教程第38期,主要内容是python编程for循环输出菱形※。(一)利用for循环输出菱形形状的*号图形1.思路:将菱形分解为上下两个部分三角形图案,分别利用...

python 10个堪称完美的for循环实践

在Python中,for循环的高效使用能显著提升代码性能和可读性。以下是10个堪称完美的for循环实践,涵盖数据处理、算法优化和Pythonic编程风格:1.遍历列表同时获取索引(enumerate...