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

相关内容

热门资讯

保存时出现了1个错误,导致这篇... 当保存文章时出现错误时,可以通过以下步骤解决问题:查看错误信息:查看错误提示信息可以帮助我们了解具体...
汇川伺服电机位置控制模式参数配... 1. 基本控制参数设置 1)设置位置控制模式   2)绝对值位置线性模...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
表格中数据未显示 当表格中的数据未显示时,可能是由于以下几个原因导致的:HTML代码问题:检查表格的HTML代码是否正...
本地主机上的图像未显示 问题描述:在本地主机上显示图像时,图像未能正常显示。解决方法:以下是一些可能的解决方法,具体取决于问...
表格列调整大小出现问题 问题描述:表格列调整大小出现问题,无法正常调整列宽。解决方法:检查表格的布局方式是否正确。确保表格使...
不一致的条件格式 要解决不一致的条件格式问题,可以按照以下步骤进行:确定条件格式的规则:首先,需要明确条件格式的规则是...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...