Hive企业使用优化二

大表【拆分】

子表
根据实际业务可以把大表拆分为几个小表。
例如可以把merit_log20180304表拆分出ip,user,url,date 等字表。

外部表、分区表

  • 结合使用 :把分区和外部表结合使用
create external table if not exists  emp_partition(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int
)
partitioned by (month string)
row format delimited fields terminated by '\t'
  • 多级分区
drop table default.kwu_tracklog;  
create EXTERNAL table default.kwu_tracklog (   
    datetime string comment "时间 : 如2015-01-01 11:30:01:123",  
    ip string comment "IP:用户本机IP或用户所在网段对外路由IP",  
    cookieid string comment "用户cookie:和讯统一在用户端生成的唯一标志",  
    userid string comment "用户和讯注册ID :用户在和讯网的注册ID",   
    logserverip string comment "记录日志服务器IP : 和讯的日志收集服务器IP",  
    referer string comment "来源 :用户浏览网页的REFER",  
    requesturl string comment "访问网址 : 当前访问网址" ,  
    remark1 string comment "【暂时没用】 :该数据无意义,由于早期加入目前不能去除",  
    remark2 string comment "【暂时没用】 : 该数据无意义,由于早期加入目前不能去除",  
    alexaflag string comment "ALEXA标志  :这个字段也是早期加入,当用户安装alexa工具时值为1,否则为0.早期加入,目前来看应该没有任何意义了。",  
    ua string comment "UA :用户浏览器UA",  
    wirelessflag string comment "无线频道标志:给无线频道专用的,一个单词,表示该文章对应和讯哪一个频道"  
)   
comment "浏览轨迹日志"  
partitioned by(day string comment "按天的分区表字段",hour string comment "按小时的分区表字段")
ROW FORMAT DELIMITED FIELDS TERMINATED BY ' '  
location '/hdfs/hive/default/kwu_tracklog';  
  • 导入数据
load data local inpath '/diskg/hexunlogs/tracklog_by5min/tracklog_10.0.121.74/20151014/2015101414*.dat' overwrite into table default.kwu_tracklog partition (day='20151014',hour='14');  
load data local inpath '/diskg/hexunlogs/tracklog_by5min/tracklog_10.0.121.74/20151014/2015101415*.dat' overwrite into table default.kwu_tracklog partition (day='20151014',hour='15'); 
  • 测试导入数据
select count(*) from default.kwu_tracklog where day='20151014' and hour='14' limit 1;  
select count(*) from default.kwu_tracklog where day='20151014' and hour='15' limit 1; 

数据

  • 存储格式(textfile、orcfile、parquet)
drop table default.kwu_tracklog;  
create EXTERNAL table default.kwu_tracklog (   
    datetime string comment "时间 : 如2015-01-01 11:30:01:123",  
    ip string comment "IP:用户本机IP或用户所在网段对外路由IP",  
    cookieid string comment "用户cookie:和讯统一在用户端生成的唯一标志",  
    userid string comment "用户和讯注册ID :用户在和讯网的注册ID",   
    logserverip string comment "记录日志服务器IP : 和讯的日志收集服务器IP",  
    referer string comment "来源 :用户浏览网页的REFER",  
    requesturl string comment "访问网址 : 当前访问网址" ,  
    remark1 string comment "【暂时没用】 :该数据无意义,由于早期加入目前不能去除",  
    remark2 string comment "【暂时没用】 : 该数据无意义,由于早期加入目前不能去除",  
    alexaflag string comment "ALEXA标志  :这个字段也是早期加入,当用户安装alexa工具时值为1,否则为0.早期加入,目前来看应该没有任何意义了。",  
    ua string comment "UA :用户浏览器UA",  
    wirelessflag string comment "无线频道标志:给无线频道专用的,一个单词,表示该文章对应和讯哪一个频道"  
)   
comment "浏览轨迹日志"  
partitioned by(day string comment "按天的分区表字段",hour string comment "按小时的分区表字段")  
ROW FORMAT DELIMITED FIELDS TERMINATED BY ' '  
STORED AS ORCFILE
location '/hdfs/hive/default/kwu_tracklog';
  • 数据压缩(snappy)

    《Hive企业使用优化二》

SQL

优化SQL语句

  • join
语法
join_table:
    table_reference [INNER] JOIN table_factor [join_condition]
  | table_reference {LEFT|RIGHT|FULL} [OUTER] JOIN table_reference join_condition
  | table_reference LEFT SEMI JOIN table_reference join_condition
  | table_reference CROSS JOIN table_reference [join_condition] (as of Hive 0.10)
 
table_reference:
    table_factor
  | join_table
 
table_factor:
    tbl_name [alias]
  | table_subquery alias
  | ( table_references )
 
join_condition:
    ON expression

join 分类

1、Common/Shuffle/Reduce Join

连接发生的阶段,发生在Reduce task。
大表对大表
每个表中的数据都是在文件中读取的。

《Hive企业使用优化二》

2、Map Join

连接发生的阶段,发生在Map task。
小表对大表
a、大表的数据从文件中读取 cid
b、小表中的数据从内存中读取。根据cid把小表数据从内存中取出。实现方式是通过DistinctedCache

《Hive企业使用优化二》

3、 SMB Join(SMB(Sort-Merge-Bucket) Join)

《Hive企业使用优化二》

《Hive企业使用优化二》

过程描述:排序–>合并–>入桶

《Hive企业使用优化二》

关于桶的文章见Hive 基础之:分区、桶、Sort Merge Bucket Join

MapReduce

Reduce Number
JVM重用
推测执行

优化方式

  • 让hive自动判断是执行哪种join。大表对小表还是大表对小表。

    《Hive企业使用优化二》

    原文作者:志辉聊码
    原文地址: https://www.jianshu.com/p/77e736b7ebe2
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞