插入数据到hive_Hive实现网站PV分析
之前我們做過《java mapreduce實(shí)現(xiàn)網(wǎng)站PV分析》,這次我們可以用hive分析一些需求指標(biāo)
提出需求:統(tǒng)計(jì)分析24小時(shí)各個(gè)時(shí)段的pv和uv
分析:
(1) pv統(tǒng)計(jì)總的瀏覽量 count(url)
(2) uv統(tǒng)計(jì)去重 count(distinct guid)
(3) 獲取時(shí)間字段,日期和小時(shí)(分區(qū)表)
最終結(jié)果預(yù)期
接下來注意每個(gè)階段:采集階段,清洗階段,分析階段。
準(zhǔn)備數(shù)據(jù),查看數(shù)據(jù)字典了解數(shù)據(jù)的結(jié)構(gòu)和意義(此處省略了數(shù)據(jù)和數(shù)據(jù)字典的展示),可以認(rèn)為此時(shí)數(shù)據(jù)已經(jīng)采集完成了(采集階段),一般由采集人員將數(shù)據(jù)交由到我們手上。
登錄beeline客戶端
啟動(dòng)服務(wù)端:bin/hiveserver2 &
啟動(dòng)客戶端
bin/beeline -u jdbc:hive2://mastercdh:10000 -n root -p password
根據(jù)數(shù)據(jù)字典,創(chuàng)建數(shù)據(jù)表
創(chuàng)建數(shù)據(jù)庫
創(chuàng)建數(shù)據(jù)表
create table track_log_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 '';
準(zhǔn)備數(shù)據(jù)
將準(zhǔn)備好的數(shù)據(jù)導(dǎo)入
load data local inpath '/data/test/data1' into table track_log_source;
load data local inpath '/data/test/data2' into table track_log_source;
再查看下
采集完成后,需要對(duì)數(shù)據(jù)進(jìn)行清洗,比如之前做過的《mapreduce實(shí)現(xiàn)數(shù)據(jù)去重》
根據(jù)之前的分析,我們創(chuàng)建表,將我們需要的字段提取出來
create table track_log_qingxi(
id string,
url string,
guid string,
date string,
hour string
)row format delimited fields terminated by '';
插入數(shù)據(jù)
insert into table track_log_qingxi select id,url,guid,substring(trackTime,9,2) date,substring(trackTime,12,2) hour from track_log_source;
分區(qū)表:根據(jù)時(shí)間字段進(jìn)行分區(qū)
create table track_log_part1(
id string,
url string,
guid string
)partitioned by(date string,hour string)
row format delimited fields terminated by '';
插入數(shù)據(jù)
insert into table track_log_part1 partition(date='20150828',hour='18') select id,url,guid from track_log_qingxi where date='28' and hour='18';
insert into table track_log_part1 partition(date='20150828',hour='19') select id,url,guid from track_log_qingxi where date='28' and hour='19';
這樣寫的話,每次都需要填寫條件,非常的不方便
我們來看一個(gè)概念:動(dòng)態(tài)分區(qū)
首先在hive的配置文件hive-site.xml中,有兩個(gè)屬性
表示是否啟用動(dòng)態(tài)分區(qū)(這個(gè)是默認(rèn)開啟的)
hive.exec.dynamic.partition
true
使用動(dòng)態(tài)分區(qū),需要設(shè)置成非嚴(yán)格模式
hive.exec.dynamic.partition.mode
strict
我們用命令更改,不直接配置了
set hive.exec.dynamic.partition.mode=nonstrict;
那我們重新創(chuàng)建分區(qū)表
create table track_log_part2(
id string,
url string,
guid string
)partitioned by(date string,hour string)
row format delimited fields terminated by '';
重新插入(這個(gè)地方利用動(dòng)態(tài)分區(qū)的特性)
insert into table track_log_part2 partition(date,hour) select * from track_log_qingxi;
查看數(shù)據(jù)發(fā)現(xiàn)自動(dòng)幫我們分開了,這樣如果是多個(gè)時(shí)間的話也會(huì)自動(dòng)完成
數(shù)據(jù)分析
PV查看
select date,hour,count(url) pv from track_log_part2 group by date,hour;
UV分析
select date,hour,count(distinct guid) uv from track_log_part2 group by date,hour;
最終結(jié)果導(dǎo)入最終結(jié)果表中
create table result as select date,hour,count(url) pv,count(distinct guid) uv from track_log_part2 group by date,hour;
數(shù)據(jù)導(dǎo)出
將最終的結(jié)果保存在mysql中
在mysql中創(chuàng)建表
create table track_pv_uv_save(
date varchar(30),
hour varchar(30),
pv varchar(30),
uv varchar(30),
primary key (date,hour)
);
sqoop方式(hive-mysql)
bin/sqoop export
--connect jdbc:mysql://mastercdh:3306/track_log_mysql
--username root
--password password
--table track_pv_uv_save
--export-dir /user/hive/warehouse/exp_track_log.db/result
-m 1
--input-fields-terminated-by '001'
在mysql中查看
我們可以將數(shù)據(jù)下載到本地
bin/hdfs dfs -get /user/hive/warehouse/exp_track_log.db/result/000000_0 /data/test
查看下數(shù)據(jù)
查看下數(shù)據(jù)是沒有問題的
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的插入数据到hive_Hive实现网站PV分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 寻路机器人单片机程序示例_单片机精华程序
- 下一篇: python无向加权图_图:无向图(Gr