Hive做一个PV,UV统计的案例记录

hive学习记录

使用hive统计一个网站的pv,和uv,只有源文件,从导入文件到最后导出统计结果,统计一个网站某天某时的pv uv访问值。

步骤

  • 在hive中新建数据源表。
  • 导入源文件到hive表中。
  • 对hive原表做一个数据清洗,筛选有用的字段,新建清洗表。
  • 新建分区表,从数据清洗表中把输入导入到分区表。
  • 对分区表的数据进行分组统计。
  • 使用sqoop导出数据到mysql中。
源文件

链接:https://pan.baidu.com/s/1FwYSrehk0bg5VK8p61ttOQ 密码:ubvi

新建数据源表
//在hive上新建track_log库
create database track_log;
//新建数据源表
create table yhd_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
)
row format delimited fields terminated by '\t'
stored as textfile;

加载数据到源表

加载2015年8月28好18点的数据
load data local inpath '/opt/cdhmoduels/data/2015082818' into table yhd_source;
加载2015年8月28好19点的数据
load data local inpath '/opt/cdhmoduels/data/2015082818' into table yhd_source;

此时yhd_source表里面的数据还比凌乱,并且字段也比较多,我们只需要看一下必须要的字段,为了方便处理,我们继续新建一个数据清洗表,并把数据清洗一下放到数据清洗表。

//创建数据清洗表
create table yhd_qingxi(
id string,
url string,
guid string,
date string,
hour string
)
row format delimited fields terminated by '\t';
//从源表导入数据到清洗表,对数据进行筛选。
insert into table yhd_qingxi select id,url,guid,substring(trackTime,9,2) date,substring(trackTime,12,2) hour from yhd_source ; 

此时,数据清洗表的数据就比较干净了,可以放到我们新建的分区表中去了

insert into table yhd_part1 partition (date='20150828',hour='18') select id,url,guid from yhd_qingxi where date='28' and hour='18';
insert into table yhd_part1 partition (date='20150828',hour='19') select id,url,guid from yhd_qingxi where date='28' and hour='19';

通过sql对分区表进行pv uv统计

select date,hour,count(distinct url),count(url) from yhd_part1 group by date,hour;
上面创建动态分区表

上面创建的分区表在导出数据的时候是写死分区字段的值的partition (date='20150828',hour='19'),这样非常的不方便,我们可以动态的创建分区表。
创建动态分区表需要设置两个属性

开启动态分区,设置成true(这里就是修改hive的配置文件属性,我们可以直接在hive的set中设置临时变量值。)

  hive.exec.dynamic.partition
  <value>truevalue>
  Whether or not to allow dynamic partitions in DML/DDL.

//设置动态分区的模式为非严格模式

  hive.exec.dynamic.partition.mode
  <value>strictvalue>
  In strict mode, the user must specify at least one static partition in case the user accidentally overwrites all partitions.

//设置允许动态分区
set hive.exec.dynamic.partition=true; 
//使用非严格模式
set hive.exec.dynamic.partition.mode=nonstrict;  

在新建一个分区表使用动态分区加载数据

create table yhd_part2(
id string,
url string,
guid string
)
partitioned by (date string,hour string)
row format delimited fields terminated by '\t';
//加载数据
insert into table yhd_part2 partition (date,hour) select * from yhd_qingxi;

将PV和UV的值导出到mysql

//新建临时表result用于保存pv和uv值,等下导出需要用
create table if not exists result as select date,hour,count(url) PV ,count(distinct guid) UV from yhd_part1 group by date,hour; 
//在mysql的数据库新建表save
create table if not exists save(
date varchar(30) not null,
hour varchar(30) not null,
pv varchar(30) not null,
uv varchar(30) not null,
primary key(date,hour)
);
//使用sqoop进行导出到mysql中
bin/sqoop export \
--connect \
jdbc:mysql://hadoop.madman.com:3306/sqoop \
--username root \
--password aaa111 \
--table save \
--export-dir /user/hive/warehouse/track_log.db/result \
--num-mappers 1 \
--input-fields-terminated-by '\001'

主意事项:hive默认的分隔符:\001

你可能感兴趣的:(大数据学习)