zoukankan      html  css  js  c++  java
  • Hive实现网站PV分析

    原文链接:

    https://www.toutiao.com/i6773241257528394248/

    之前我们做过《java mapreduce实现网站PV分析》,这次我们可以用hive分析一些需求指标

    提出需求:统计分析24小时各个时段的pv和uv

    分析:

    (1) pv统计总的浏览量 count(url)

    (2) uv统计去重 count(distinct guid)

    (3) 获取时间字段,日期和小时(分区表)

    最终结果预期

    Hive实现网站PV分析

     

    接下来注意每个阶段:采集阶段,清洗阶段,分析阶段。

    准备数据,查看数据字典了解数据的结构和意义(此处省略了数据和数据字典的展示),可以认为此时数据已经采集完成了(采集阶段),一般由采集人员将数据交由到我们手上。

    Hive实现网站PV分析

     

    登录beeline客户端

    启动服务端:bin/hiveserver2 &

    Hive实现网站PV分析

     

    启动客户端

    bin/beeline -u jdbc:hive2://mastercdh:10000 -n root -p password

    Hive实现网站PV分析

     

    根据数据字典,创建数据表

    创建数据库

    Hive实现网站PV分析

     

    创建数据表

    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 ' ';

    Hive实现网站PV分析

     

    准备数据

    Hive实现网站PV分析

     

    将准备好的数据导入

    load data local inpath '/data/test/data1' into table track_log_source;

    load data local inpath '/data/test/data2' into table track_log_source;

    Hive实现网站PV分析

     

    再查看下

    Hive实现网站PV分析

     

    采集完成后,需要对数据进行清洗,比如之前做过的《mapreduce实现数据去重》

    根据之前的分析,我们创建表,将我们需要的字段提取出来

    create table track_log_qingxi(

    id string,

    url string,

    guid string,

    date string,

    hour string

    )row format delimited fields terminated by ' ';

    Hive实现网站PV分析

     

    插入数据

    insert into table track_log_qingxi select id,url,guid,substring(trackTime,9,2) date,substring(trackTime,12,2) hour from track_log_source;

    Hive实现网站PV分析

     

    分区表:根据时间字段进行分区

    create table track_log_part1(

    id string,

    url string,

    guid string

    )partitioned by(date string,hour string)

    row format delimited fields terminated by ' ';

    Hive实现网站PV分析

     

    插入数据

    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';

    Hive实现网站PV分析

     

    这样写的话,每次都需要填写条件,非常的不方便

    我们来看一个概念:动态分区

    首先在hive的配置文件hive-site.xml中,有两个属性

    表示是否启用动态分区(这个是默认开启的)

    <property>

    <name>hive.exec.dynamic.partition</name>

    <value>true</value>

    </property>

    使用动态分区,需要设置成非严格模式

    <property>

    <name>hive.exec.dynamic.partition.mode</name>

    <value>strict</value>

    </property>

    我们用命令更改,不直接配置了

    set hive.exec.dynamic.partition.mode=nonstrict;

    Hive实现网站PV分析

     

    那我们重新创建分区表

    create table track_log_part2(

    id string,

    url string,

    guid string

    )partitioned by(date string,hour string)

    row format delimited fields terminated by ' ';

    Hive实现网站PV分析

     

    重新插入(这个地方利用动态分区的特性)

    insert into table track_log_part2 partition(date,hour) select * from track_log_qingxi;

    Hive实现网站PV分析

     

    查看数据发现自动帮我们分开了,这样如果是多个时间的话也会自动完成

    Hive实现网站PV分析

     

    数据分析

    PV查看

    select date,hour,count(url) pv from track_log_part2 group by date,hour;

    Hive实现网站PV分析

     

    UV分析

    select date,hour,count(distinct guid) uv from track_log_part2 group by date,hour;

    Hive实现网站PV分析

     

    最终结果导入最终结果表中

    create table result as select date,hour,count(url) pv,count(distinct guid) uv from track_log_part2 group by date,hour;

    Hive实现网站PV分析

     

    数据导出

    将最终的结果保存在mysql中

    在mysql中创建表

    create table track_pv_uv_save(

    date varchar(30),

    hour varchar(30),

    pv varchar(30),

    uv varchar(30),

    primary key (date,hour)

    );

    Hive实现网站PV分析

     

    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 '01'

    Hive实现网站PV分析

     

    在mysql中查看

    Hive实现网站PV分析

     

    我们可以将数据下载到本地

    bin/hdfs dfs -get /user/hive/warehouse/exp_track_log.db/result/000000_0 /data/test

    Hive实现网站PV分析

     

    查看下数据

    Hive实现网站PV分析

     

    查看下数据是没有问题的

    Hive实现网站PV分析
  • 相关阅读:
    GoWeb-Gin 文件上载
    Node.JS + Mysql数据库
    Node.JS 项目打包 JXCore
    Express web框架 upload file
    hdoj 4430 Yukari's Birthday
    hdoj 4282 A very hard mathematic problem
    hdoj 3750 Guess Game
    hdoj 2199 Can you solve this equation?
    uva 11384 Help is needed for Dexter
    ios中fixed元素在滚动布局中的延时渲染问题
  • 原文地址:https://www.cnblogs.com/bqwzy/p/12535810.html
Copyright © 2011-2022 走看看