hive-学习搜狗日志分析
创始人
2024-02-26 15:56:56
0

–搜狗日志分析—
–创建数据仓库
create external table if not exists sg_table(
ts string,
uid string,
keyword string,
ranks int,
orders int,
url string,
years int,
months int,
days int,
hours int)
comment ‘This is the sogou search data of extend’
row format delimited
fields terminated by ‘\t’
stored as textfile
location ‘/data/sg’;

show tables;
SELECT * from default.sg_table limit 3;

–创建动态分区表
create external table if not exists sg_partition_table(
ts string,
uid string,
keyword string,
ranks int,
orders int,
url string
)
comment ‘This is the sogou search data by partition’
partitioned by (years int,months int,days int,hours int)
row format delimited
fields terminated by ‘\t’
stored as textfile;

–空串处理
alter table sg_partition_table set serdeproperties(‘serialization.null.format’=‘’);
desc formatted sg_partition_table ;
–开启动态分区设置 开启非严格模式
set hive.exec.dynamic.partition.mode=nonstrict;

–插入数据
insert overwrite table sg_partition_table partition(years,months,days,hours)
select * from sg_table;
–数据预览
SELECT * from sg_partition_table limit 10;
–查看分区
show partitions sg_partition_table ;
–分区查询
SELECT * FROM default.sg_partition_table t1 WHERE t1.years = ‘2011’ AND t1.months = ‘12’;

–数据分析
–记录数分析
–统计记录数 5000000
SELECT
COUNT(*)
from sg_partition_table ;

–不重复记录 根据ts uid keyword url 4999636
SELECT count(t1.uid) uniq_data
from (select DISTINCT t.ts, t.uid, t.keyword, t.url
from sg_partition_table t) t1

–独立用户数 1352664
select
count(t1.uid) uniq_user
from (select DISTINCT uid from sg_partition_table) t1;

–独立url数 2466932
select
count(t1.url) uniq_url
from (select DISTINCT url from sg_partition_table) t1;

–关键词分析
–关键词的平均长度 7.5618454 注意去除空格
SELECT avg(length(replace(keyword,’ ‘,’‘))) avg_kw from sg_partition_table;
–replace(str,’ ‘,’‘) 将字符所有空格去除
–select size(split(trim(’ a sddd d ‘),’ ‘)),replace(’ a sddd d ‘,’ ‘,’‘),length(replace(’ a sddd d ‘,’ ‘,’'))

–最长关键词 1005 最短关键词1
SELECT max(length(replace(keyword,’ ‘,’‘))) max_kw
, min(length(replace(keyword,’ ‘,’'))) min_kw
from sg_partition_table;

SELECT * from sg_partition_table where length(replace(keyword,’ ‘,’‘))=1005;
SELECT * from sg_partition_table where length(replace(keyword,’ ‘,’'))=1;

– 长度为1的关键词记录统计排序 。。。
SELECT
replace(keyword,’ ‘,’')
, count() kw_ranks
from sg_partition_table
where length(replace(keyword,’ ‘,’‘))=1
GROUP by replace(keyword,’ ‘,’')
order by count(
) desc;

– 关键词频率排序 前十关键词
SELECT
t.keyword
, count() kw_rank
from sg_partition_table t
group by t.keyword
order by count(
) desc
limit 10;

– 关键词频率排序 后十关键词
SELECT
t.keyword
, count() kw_rank
from sg_partition_table t
group by t.keyword
order by count(
) asc
limit 10;

– 用户分析
–用户搜索次数统计 前十
SELECT
t.uid
,count() user_kw_rank
from sg_partition_table t
group by t.uid
order by count(
) desc
limit 10;

–搜索次数最多的用户都搜索了什么 经常搜索什么内容
SELECT
t.keyword
,count() ss_count
from sg_partition_table t
where t.uid=‘02a8557754445a9b1b22a37b40d6db38’
group by t.keyword
order by count(
) desc
limit 10

–搜索次数人数人数情况
SELECT
sum(if(t1.user_kw_rank=1,1,0)) one_nums --只搜索一次 549148
,sum(if(t1.user_kw_rank=2,1,0)) tow_nums --只搜索两次 257163
,sum(if(t1.user_kw_rank>3,1,0)) over_three --搜索三次以上 396791
from (
SELECT
t.uid
,count(t.keyword) user_kw_rank
from sg_partition_table t
group by t.uid)t1;

–搜索次数人数人数比列情况
SELECT
round(sum(if(t1.user_kw_rank=1,1,0))/count(t1.uid),2)*100 || ‘%’ one_nums_rts --只搜索一次
,round(sum(if(t1.user_kw_rank=2,1,0))/count(t1.uid),2)*100 || ‘%’ tow_nums_rts --只搜索两次
,round(sum(if(t1.user_kw_rank>3,1,0))/count(t1.uid),2)*100 || ‘%’ over_three_rts --搜索三次以上
from (
SELECT
t.uid
,count(t.keyword) user_kw_rank
from sg_partition_table t
group by t.uid)t1;

–SELECT round(0.222,2)*100 || ‘%’;

–用户平均查询次数 所有用户搜索次数 / 用户数 3.6964094557111005
SELECT
sum(t1.user_ss_counts) / count(t1.uid) avg_ss_counts
from (
SELECT
t.uid
,count(*) user_ss_counts --每个用户的搜索次数
from sg_partition_table t
group by t.uid)t1 ;

–用户行为分析
–点击次数与rank之间的关系分析
SELECT
count(*)
from sg_partition_table t
where t.ranks <= 10;
–总数为500万,比例为4999869/5000000,可看出,绝大部分会点击前10条搜索结果。

–直接输入URL作为查询词的比例
SELECT
count(*)
from sg_partition_table t
where t.keyword like ‘%www%’;

– 查询直接输入URL搜索并且搜索的URL位于点击的URL中记录统计
SELECT
sum(if(instr(t.url,t.keyword)>0,1,0)) – 27561 可看出大部分搜索URL,并不能得到自己想要的结果。
from sg_partition_table t
where t.keyword like ‘%www%’

–SELECT instr(‘asdfg’,‘df’) --查看指定字符在字符串中是否存在 如果存在返回首次出现的位置 不存在则返回0

相关内容

热门资讯

银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...