Hive多表关联

1)示例

表信息:部门表、员工表、位置表。字段信息如下:

-- 部门表信息
create external table if not exists department(
    deptno bigint comment '部门编号',
    dname  string comment '部门名',
    loc    bigint comment '部门位置'
)comment '部门表'
row format delimited fields terminated by '\t'
location '/test/department';   -- 外部表,通常数据不在表路径下,因此需要指定数据位置。

-- 员工表信息
create external table if not exists employee(
    empno    bigint comment '员工编号',
    ename    string comment '员工姓名',
    job      string comment '工种',
    mgr      bigint comment '部门领导',
    hiredate string comment '入职时间',
    sal      decimal(16,2) comment '薪资',   -- 注意资金问题字段选型:double|decimal
    comm     decimal(16,2) comment '奖金',
    deptno   bigint comment '部门编号'
)comment '员工表'
row format delimited fields terminated by '\t'
location '/test/employee';

create external table if not exists location(
	loc int,
	loc_name string
)
row format delimited fields terminated by '\t'
location '/test/location';

多表连接查询

SELECT e.ename, d.dname, l.loc_name
FROM   employee e 
JOIN   department d
	ON     d.deptno = e.deptno 
JOIN   location l
	ON     d.loc = l.loc;

底层执行过程解析:

  1. 先启动一个MapReduce job对表e和表d进行连接操作。
  2. 然后再启动一个MapReduce job将第一个MapReduce job的输出和表l进行连接操作。
  3. 虽然有两个MapReduce job在执行,但是此时是串行执行的,不是并行执行的。因为,第3张表要与表1-2的连接结果进行连接

2)总结

  1. 连接 n个表,至少需要n-1个连接条件。例如:连接三个表,至少需要两个连接条件。
  2. 多表连接时,省略连接条件、连接条件无效、所有表中的所有行互相连接都会产生笛卡尔积。
  3. 大多数情况下,Hive会对每对join连接对象启动一个MapReduce任务。连接条件相同时,只启动一个MapReduce任务
  4. 先执行表d和表e连接,然后表d和表l连接。Hive总是按照从左到右的顺序执行的。
  5. 一条SQL语句,底层由MapReduce执行时,是串行执行的,不是并行执行的。

3)优化

  1. 当对3个或者更多表进行join连接时,如果每个on子句都使用相同的连接键,那么只会产生一个MapReduce job
  2. 串行转并行思想。根据需求环境尽可能减少一次多个表连接。如2n个表连接,可以采用2组n个表连接,然后合并连接。
  3. 优化计算引擎。Hive on Spark替换Hive on MapReduce,Spark基于内存计算引擎替换MapReduce数据落盘计算引擎。
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值