🍳
Between code and words
  • About
    • About Me
    • About Book
  • 技术专辑
    • 大数据BigData
      • Ambari
        • 搭建虚拟机集群以及安装Ambari
          • self
            • 构建b
            • 构建m
            • 构建s
            • 构建集群
          • Windows
            • +VMware从头完全部署
            • +Docker从已有镜像简易部署
          • Linux
            • +Docker从头完全部署
            • +Docker从已有镜像简易部署
      • CDH
      • Maven
        • 一个简单的项目
        • GitHub远程maven私人仓库
      • Hadoop
        • HDFS介绍
        • MapReduce介绍
        • YARN介绍
        • HDFS常用命令
      • Hive
        • Hive简介及核心概念
        • Hive CLI和Beeline命令行的基本使用
        • ambari安装Hive
        • Hive常用DDL操作
        • Hive常用DML操作
        • Hive常用DCL操作
        • Hive分区表和分桶表
        • Hive 视图和索引
        • Hive数据查询详解
        • 进阶-优化
        • 进阶-函数
        • 进阶-Brickhouse UDF
        • 进阶-接入Python
      • 帮助
        • 常用端口
        • 常用命令
        • QA
    • 项目Program
      • 大数据项目实践
        • 1 亿条淘宝用户行为数据分析
          • 1. 部署环境
          • 2. 数据集下载
          • 3. 数据处理和表优化
          • 4.数据分析
          • 5.可视化
      • Web实践-Qhubl
        • 第一章-概
        • 第二章-面向公众的前后端
        • 部署指导
      • GNN
        • 1. 环境配置
        • 2. 节点分类
          • 数据集
            • Cora.py
          • 模型
            • GCN.py
            • GAT.py
          • Utils
            • draw.py
            • TTV.py
      • 美亚柏科
    • Linux
      • Linux
        • 常见
        • 代理
        • 科学计算
          • MPAS7
        • WSL
          • WSL数据迁移
          • 安装Docker Engine
        • 脚本
        • QA
      • Git
        • QA
      • VMware
        • 虚拟机代理
        • 双向复制粘贴
        • 磁盘扩容
        • QA
      • Docker
        • WSL安装Docker Engine
        • 优雅的上代理
        • 优雅地给容器新添端口
        • QA
      • MySQL
        • 重置初始密码
        • 免输密码登录
        • 低密码策略脚本
        • DeBug
    • Java
      • 语言特性
        • 多线程
        • AQS
        • JVMG1
      • 框架
        • SpringBoot
          • 注解
          • 配置
          • YAML
  • Self
    • 电脑应用
    • 奖项存档
    • 日语笔记
      • 入门五十音
      • 入门音调声调
    • 读书笔记
      • 《贫穷的本质》
        • 前言
        • 第一章 再好好想想
        • 第二章 饥饿人口已达到10亿?
Powered by GitBook
On this page
  • 3. 数据处理和表优化
  • 3.1 数据导入
  • 3.2 数据清洗
  1. 技术专辑
  2. 项目Program
  3. 大数据项目实践
  4. 1 亿条淘宝用户行为数据分析

3. 数据处理和表优化

3. 数据处理和表优化

3.1 数据导入

beeline -n hive -p进入hql命令行

创建一个临时表,并加载csv数据文件加载到其中。

CREATE TEMPORARY TABLE temp_user_behavior (
`user_id` string comment 'user ID',
`item_id` string comment 'item ID',
`category_id` string comment 'category ID',
`behavior_type` string  comment 'behavior type among pv, buy, cart, fav',
`timestamp` int comment 'timestamp')
row format delimited
fields terminated by ','
lines terminated by '\n'
STORED AS TEXTFILE;

LOAD DATA LOCAL INPATH '/root/Hive/UserBehavior.csv' OVERWRITE INTO TABLE temp_user_behavior ;

创建用户行为表1。

drop table if exists user_behavior1;
create table user_behavior1 (
`user_id` string comment 'user ID',
`item_id` string comment 'item ID',
`category_id` string comment 'category ID',
`timestamp` timestamp comment 'timestamp'
)
PARTITIONED BY (`date` date, `behavior_type` string comment 'behavior type among pv, buy, cart, fav')
row format delimited
fields terminated by ','
lines terminated by '\n'
STORED AS ORC
TBLPROPERTIES ("orc.compress"="SNAPPY");

这里使用以列优先的存储格式,定义压缩算法为snappy,对于像电商分析这样主要查询列的项目,会提高很多效率。同时对日期date进行分区,以及用户行为behavior_type进行分区是一种合理的分区方法,在后续分析过程中将大大提高查询速度。

将数据导入到ORC表中,hive会自动执行 行列转化

INSERT OVERWRITE TABLE user_behavior1 PARTITION (`date`, behavior_type)
SELECT
  user_id,
  item_id,
  category_id,
  from_unixtime(`timestamp`) AS `timestamp`,
  date(from_unixtime(`timestamp`)) AS `date`,
  behavior_type
FROM
  temp_user_behavior;

查看一共多少条数据。

select count(*) from user_behavior1;
+------------+
|    _c0     |
+------------+
| 100150807  |
+------------+

3.2 数据清洗

-- 查看时间-数据分布情况,是否有异常值
select `date`, COUNT(*) from user_behavior1 group by `date` order by `date`;

-- 删除不在2017-11-25 到 2017-12-03日期的数据
alter table user_behavior1 
drop IF EXISTS partition (`date`<'2017-11-25'), partition (`date`>'2017-12-03');

-- 再次查看时间是否有异常值
select `date`, COUNT(*) from user_behavior1 group by `date` order by `date`;

+-------------+-----------+
|    date     |    _c1    |
+-------------+-----------+
| 2017-11-25  | 10511605  |
| 2017-11-26  | 10571046  |
| 2017-11-27  | 10013457  |
| 2017-11-28  | 9884189   |
| 2017-11-29  | 10319066  |
| 2017-11-30  | 10541698  |
| 2017-12-01  | 11171515  |
| 2017-12-02  | 13940949  |
| 2017-12-03  | 11961008  |
+-------------+-----------+
--查看 behavior_type 是否有异常值
select behavior_type, COUNT(*) from user_behavior1 group by behavior_type;

+----------------+-----------+
| behavior_type  |    _c1    |
+----------------+-----------+
| cart           | 5466118   |
| pv             | 88596903  |
| buy            | 1998976   |
| fav            | 2852536   |
+----------------+-----------+
-- 去掉完全重复的数据
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;

INSERT OVERWRITE TABLE user_behavior1
PARTITION (`date`, `behavior_type`)
SELECT DISTINCT `user_id`, `item_id`, `category_id`, `timestamp`, `date`, `behavior_type`
FROM user_behavior1
DISTRIBUTE BY `date`, `behavior_type`;

-- 查看目前多少条
select count(*) from user_behavior1;
+-----------+
|    _c0    |
+-----------+
| 98914484  |
+-----------+

DISTRIBUTE BY date, behavior_type这个是用来指定数据分发的策略,它会根据分区键的值将数据分发到不同的reduce任务中,每个reduce任务只处理一个分区的数据。这样就可以在每个分区内部去重,而不需要到全局数据去比较,所以效率高很多。在hdfs上,表按照 date, behavior_type分区后,分区的文件夹数量= date分区数* behavior_type分区数。当DISTRIBUTE BY date, behavior_type;时,可以理解为是在date分区数* behavior_type分区数 这么多个局部中比较去重。

同时如果DISTRIBUTE BY date, behavior_type粒度划分的太细,导致启动的容器太多,计算时间占比较低,可以选择只DISTRIBUTE BY一个。

Previous2. 数据集下载Next4.数据分析

Last updated 1 year ago