关注公众号:分享电脑学习
回复"百度云盘" 可以免费获取所有学习文档的代码(不定期更新)
云盘目录说明:
tools目录是安装包
res 目录是每一个课件对应的代码和资源等
doc 目录是一些第三方的文档工具

承接上一篇文档《新增访客数量MR统计之MR数据输出到MySQL

hive-1.2.1的版本可以直接映射HBase已经存在的表

如果说想在hive创建表,同时HBase不存在对应的表,也想做映射,那么采用编译后的hive版本hive-1.2.1-hbase

1. Hive中创建外部表,关联hbase

CREATE EXTERNAL TABLE event_log_20180728(
key string,
pl string,
ver string,
s_time string,
u_ud string,
u_sd string,
en string)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,info:pl,info:ver,info:s_time,info:u_ud,info:u_sd,info:en")
TBLPROPERTIES("hbase.table.name" = "event_log_20180728");

统计多少个新用户:

select count(*) from event_log_20180728 where en="e_l";

2. 提取数据,进行初步的数据过滤操作,最终将数据保存到临时表

创建临时表

CREATE TABLE stats_hourly_tmp01(
pl string,
ver string,
s_time string,
u_ud string,
u_sd string,
en string,
`date` string,
hour int
);

将原始数据提取到临时表中

INSERT OVERWRITE TABLE stats_hourly_tmp01
SELECT pl,ver,s_time,u_ud,u_sd,en,
from_unixtime(cast(s_time/1000 as int),'yyyy-MM-dd'), hour(from_unixtime(cast(s_time/1000 as int),'yyyy-MM-dd HH:mm:ss'))
FROM event_log_20200510
WHERE en="e_l" or en="e_pv";

SELECT from_unixtime(cast(s_time/1000 as int),'yyyy-MM-dd'),from_unixtime(cast(s_time/1000 as int),'yyyy-MM-dd HH:mm:ss') FROM event_log_20180728;

查看结果

3. 具体kpi的分析

创建临时表保存数据结果

CREATE TABLE stats_hourly_tmp02(
pl string,
ver string,
`date` string,
kpi string,
hour int,
value int
);

统计活跃用户 u_ud 有多少就有多少用户

统计platform维度是:(name,version)

INSERT OVERWRITE TABLE stats_hourly_tmp02
SELECT pl,ver,`date`,'hourly_new_install_users' as kpi,hour,COUNT(distinct u_ud) as v
FROM stats_hourly_tmp01
WHERE en="e_l"
GROUP BY pl,ver,`date`,hour;

查看结果:

统计会话长度指标

会话长度 = 一个会话中最后一条记录的时间 - 第一条的记录时间 = maxtime - mintime

步骤:

1. 计算出每个会话的会话长度 group by u_sd

2. 统计每个区间段的总会话长度

统计platform维度是:(name,version)

INSERT INTO TABLE

SELECT pl,ver,`date`,'hourly_session_length' as kpi,hour, sum(s_length)/1000 as v
FROM (
SELECT pl,ver,`date`,hour,u_sd,(max(s_time) - min(s_time)) as s_length
FROM stats_hourly_tmp01
GROUP BY pl,ver,`date`,hour,u_sd
) tmp
GROUP BY pl,ver,`date`,hour;

查看结果

将tmp02的数据转换为和mysql表结构一致的数据

窄表转宽表 => 转换的结果保存到临时表中

CREATE TABLE stats_hourly_tmp03(
pl string, ver string, `date` string, kpi string,
hour00 int, hour01 int, hour02 int, hour03 int,
hour04 int, hour05 int, hour06 int, hour07 int,
hour08 int, hour09 int, hour10 int, hour11 int,
hour12 int, hour13 int, hour14 int, hour15 int,
hour16 int, hour17 int, hour18 int, hour19 int,
hour20 int, hour21 int, hour22 int, hour23 int
);

INSERT OVERWRITE TABLE stats_hourly_tmp03
SELECT pl,ver,`date`,kpi,
max(case when hour=0 then value else 0 end) as h0,
max(case when hour=1 then value else 0 end) as h1,
max(case when hour=2 then value else 0 end) as h2,
max(case when hour=3 then value else 0 end) as h3,
max(case when hour=4 then value else 0 end) as h4,
max(case when hour=5 then value else 0 end) as h5,
max(case when hour=6 then value else 0 end) as h6,
max(case when hour=7 then value else 0 end) as h7,
max(case when hour=8 then value else 0 end) as h8,
max(case when hour=9 then value else 0 end) as h9,
max(case when hour=10 then value else 0 end) as h10,
max(case when hour=11 then value else 0 end) as h11,
max(case when hour=12 then value else 0 end) as h12,
max(case when hour=13 then value else 0 end) as h13,
max(case when hour=14 then value else 0 end) as h14,
max(case when hour=15 then value else 0 end) as h15,
max(case when hour=16 then value else 0 end) as h16,
max(case when hour=17 then value else 0 end) as h17,
max(case when hour=18 then value else 0 end) as h18,
max(case when hour=19 then value else 0 end) as h19,
max(case when hour=20 then value else 0 end) as h20,
max(case when hour=21 then value else 0 end) as h21,
max(case when hour=22 then value else 0 end) as h22,
max(case when hour=23 then value else 0 end) as h23
FROM stats_hourly_tmp02
GROUP BY pl,ver,`date`,kpi;

select hour14,hour15,hour16 from stats_hourly_tmp03;

结果:

将维度的属性值转换为id,使用UDF进行转换

1. 将udf文件夹中的所有自定义HIVE的UDF放到项目中

2. 使用run maven install环境进行打包

3. 将打包形成的jar文件上传到HDFS上的/jar文件夹中

4. hive中创建自定义函数,命令如下:

create function dateconverter as 'com.xlgl.wzy.hive.udf.DateDimensionConverterUDF' using jar 'hdfs://master:9000/jar/transformer-0.0.1.jar';

create function kpiconverter as 'com.xlgl.wzy.hive.udf.KpiDimensionConverterUDF' using jar 'hdfs://master:9000/jar/transformer-0.0.1.jar';

create function platformconverter as 'com.xlgl.wzy.hive.udf.PlatformDimensionConverterUDF' using jar 'hdfs://master:9000/jar/transformer-0.0.1.jar';

创建hive中对应mysql的最终表结构

CREATE TABLE stats_hourly(
platform_dimension_id int,
date_dimension_id int,
kpi_dimension_id int,
hour00 int, hour01 int, hour02 int, hour03 int,
hour04 int, hour05 int, hour06 int, hour07 int,
hour08 int, hour09 int, hour10 int, hour11 int,
hour12 int, hour13 int, hour14 int, hour15 int,
hour16 int, hour17 int, hour18 int, hour19 int,
hour20 int, hour21 int, hour22 int, hour23 int
);

INSERT OVERWRITE TABLE stats_hourly
SELECT
platformconverter(pl,ver), dateconverter(`date`,'day'),kpiconverter(kpi),
hour00 , hour01 , hour02 , hour03 ,
hour04 , hour05 , hour06 , hour07 ,
hour08 , hour09 , hour10 , hour11 ,
hour12 , hour13 , hour14 , hour15 ,
hour16 , hour17 , hour18 , hour19 ,
hour20 , hour21 , hour22 , hour23
FROM stats_hourly_tmp03;

导出sqoop-》mysql

bin/sqoop export \
--connect jdbc:mysql://master:3306/test \
--username root \
--password 123456 \
--table stats_hourly \
--export-dir /user/hive/warehouse/log_lx.db/stats_hourly \
-m 1 \
--input-fields-terminated-by '\001'

查询mysql

Hive分析统计离线日志信息的更多相关文章

  1. 使用hive分析nginx访问日志方法

    以下案例是使用hive分析nginx的访问日志案例,其中字段分隔通过正则表达式匹配,具体步骤如下: 日志格式: 192.168.5.139 - - [08/Jun/2017:17:09:12 +080 ...

  2. Hive分析hadoop进程日志

    想把hadoop的进程日志导入hive表进行分析,遂做了以下的尝试. 关于hadoop进程日志的解析 使用正则表达式获取四个字段,一个是日期时间,一个是日志级别,一个是类,最后一个是详细信息, 然后在 ...

  3. 在linux中使用shell来分析统计日志中的信息

    在运维工作中,要经常分析后台系统的日志,通过抓取日志中的关键字信息,对抓取结果进行统计,从而为监控结果提供基础数据.下面的shell演示了如何从大量的日志中取得想要的统计结果.其中展示了各种有趣的命令 ...

  4. 使用awk进行日志信息的分组统计

    起因 这是今天我线上出了一个bug,需要查看日志并统计一个我需要的信息出现的频率,可以叫做分组统计. 日志文件部分内容 00:09:07.655 [showcase_backend][topsdk] ...

  5. hive分析nginx日志之UDF清洗数据

    hive分析nginx日志一:http://www.cnblogs.com/wcwen1990/p/7066230.html hive分析nginx日志二:http://www.cnblogs.com ...

  6. Flume 概述+环境配置+监听Hive日志信息并写入到hdfs

    Flume介绍Flume是Apache基金会组织的一个提供的高可用的,高可靠的,分布式的海量日志采集.聚合和传输的系统,Flume支持在日志系统中定制各类数据发送方,用于收集数据:同时,Flume提供 ...

  7. Monkey测试:日志信息分析

    在跑monkey时,我们需要将日志输出到文件,然后对日志信息进行分析. 一.输出日志到文件 在monkey命令后加>文件地址 如:adb shell monkey 1000>E:/text ...

  8. shell常用命令及正则辅助日志分析统计

    https://www.cnblogs.com/wj033/p/3451618.html 正则日志分析统计 3 grep 'onerror'  v3-0621.log | egrep  -v '(\d ...

  9. 如何分析和研究Log文件 ,如何看日志信息

    如何分析和研究Log文件 ,如何看日志信息 . Log 在android中的地位非常重要,要是作为一个android程序员不能过分析log这关,算是android没有入门吧 . 下面我们就来说说如何处 ...

随机推荐

  1. markDodn使用技巧

    markdown 标题 一级标题书写语法: 井符(#)加上空格加上标题名称 二级标题书写语法: 两个井符(#)加上空格加上标题名称 三级标题书写语法: 三个井符(#)加上空格加上标题名称 字体 字体加 ...

  2. 【划重点】Python pandas简介

    一.pandas获取Excel表单的两种方式 import pandas as pd df1 = pd.DataFrame(pd.read_excel(r'C:\Users\ASUS\Desktop\ ...

  3. Tableau如何绘制凹凸图

    一.把订单日期拖拽至列,把销售额拖拽至行,类别拖拽至标记,并把订单日期拖拽至筛选器选择2017年 二.创建计算字段销售排名 三.将刚刚创建的销售排名拖拽至行,计算依据-类别 四.销量排名拖拽成两个,图 ...

  4. mysql--求中位数

    第一种求中位数方法: /* 第一步:添加一个正序和反序 第二步:当列表数目为奇数的时候,列表选出的情况,当列表为偶数的时候列表的情况 第三步:统筹奇数和偶数时中位数 */ select sum(Mat ...

  5. [BUUCTF]PWN——wustctf2020_closed

    wustctf2020_closed 附件 步骤: 例行检查,64位程序,开启了nx保护 本地试运行一下看看大概的情况 64位ida载入,首先是检索程序里的字符串,找到了后门 main函数里的关键函数 ...

  6. 开源企业平台Odoo 15社区版之项目管理应用模块功能简介

    项目管理无论是各类证书的认证,如PMP.软考高级的信息系统项目管理师.中级的系统集成项目管理工程师等,还是企业实践都有着广泛的实际应用中,至今还是处于热门的行业,合格的或优化的项目经理还是偏少,对于I ...

  7. Springboot MVC 自动配置

    Springboot MVC 自动配置 官方文档阅读 https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#w ...

  8. vue 判断页面是否滚动到底部

    需求 要求用户阅读完本页所有内容后,下一步按钮才可以点击. 实现思路 通过判断当前页面是否到达底部来设置按钮的点击事件. 要判断当前页面是否到达底部需要用到三个距离--距离顶部的距离scrollTop ...

  9. JAVA 通过url下载图片保存到本地

    //java 通过url下载图片保存到本地 public static void download(String urlString, int i) throws Exception { // 构造U ...

  10. 【LeetCode】1436. 旅行终点站 Destination City (Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 日期 题目地址:https://leetcod ...