(08)Hive——Join连接、谓词下推

前言

  Hive-3.1.2版本支持6种join语法。分别是:inner join(内连接)、left join(左连接)、right join(右连接)、full outer join(全外连接)、left semi join(左半开连接)、cross join(交叉连接,也叫做笛卡尔乘积)。

一、Hive的Join连接

数据准备: 有两张表studentInfo、studentScore

create table if not exists studentInfo
(user_id   int comment '学生id',name      string comment '学生姓名',gender    string comment '学生性别'
)comment '学生信息表';
INSERT overwrite table studentInfo
VALUES (1, '吱吱', '男'),(2, '格格', '男'),(3, '纷纷', '女'),(4, '嘻嘻', '女'),(5, '安娜', '女');create table if not exists studentScore
(user_id   int comment '学生id',subject   string comment '学科',score     int comment '分数'
)comment '学生分数表';INSERT overwrite table studentScore
VALUES (1, '生物', 78),(2, '生物', 88),(3, '生物', 34),(4, '数学', 98),(null, '数学', 64);

1.1 inner join 内连接

       内连接是最常见的一种连接,其中inner可以省略:inner join == join ; 只有进行连接的两个表中都存在与连接条件相匹配的数据才会被留下来。

selectt1.user_id,t1.name,t1.gender,t2.subject,t2.score
from studentInfo t1inner join studentScore t2 on t1.user_id = t2.user_id

1.2 left join 左外连接

    join时以左表的全部数据为准,右边与之关联;左表数据全部返回,右表关联上的显示返回,关联不上的显示null返回。

selectt1.user_id,t1.name,t1.gender,t2.user_id,t2.subject,t2.score
from studentInfo t1left  join studentScore t2 on t1.user_id = t2.user_id;

1.3 right join 右外连接

       join时以右表的全部数据为准,左边与之关联;右表数据全部返回,左表关联上的显示返回,关联不上的显示null返回。

selectt2.user_id,t2.subject,t2.score,t1.user_id,t1.name,t1.gender
from studentInfo t1right  join studentScore t2on t1.user_id = t2.user_id;

1.4 full join 满外连接

  包含左、右两个表的全部行,不管另外一边的表中是否存在与它们匹配的行;在功能上等价于对这两个数据集合分别进行左外连接和右外连接,然后再使用消去重复行的操作将上述两个结果集合并为一个结果集。full join 本质等价于 left join  union   right join; 

selectt1.user_id,t1.name,t1.gender,t2.user_id,t2.subject,t2.score
from studentInfo t1full  join studentScore t2on t1.user_id = t2.user_id;

ps:full join 本质等价于 left join union  right join; 

selectt1.user_id,t1.name,t1.gender,t2.user_id,t2.subject,t2.score
from studentInfo t1full  join studentScore t2on t1.user_id = t2.user_id;----- 等价于下述代码selectt1.user_id as t1_user_id ,t1.name,t1.gender,t2.user_id as  t2_user_id,t2.subject,t2.score
from studentInfo t1left  join studentScore t2on t1.user_id = t2.user_id
union
selectt1.user_id as t1_user_id ,t1.name,t1.gender,t2.user_id as t2_user_id,t2.subject,t2.score
from studentInfo t1right  join studentScore t2on t1.user_id = t2.user_id

1.5 多表连接

      注意:连接 n 个表,至少需要 n-1 个连接条件。例如:连接三个表,至少需要两个连接
条件。 join on使用的key有几组就会被转化为几个MR任务,使用相 同的key来连接,则只会被转化为1个MR任务。

1.6 cross join 交叉连接

    交叉连接cross join,将会返回被连接的两个表的笛卡尔积,返回结果的行数等于两个表行数的乘积 N*M。对于大表来说,cross join慎用(笛卡尔积可能会造成数据膨胀

    在SQL标准中定义的cross join就是无条件的inner join。返回两个表的笛卡尔积,无需指定关联 键。
  在HiveSQL语法中,cross join 后面可以跟where子句进行过滤,或者on条件过滤。

---举例:
selectt1.user_id as t1_user_id ,t1.name,t1.gender,t2.user_id as t2_user_id,t2.subject,t2.score
from studentInfo t1, studentScore t2--- 等价于:
selectt1.user_id as t1_user_id ,t1.name,t1.gender,t2.user_id as t2_user_id,t2.subject,t2.score
from studentInfo t1join studentScore t2---等价于:
selectt1.user_id as t1_user_id ,t1.name,t1.gender,t2.user_id as t2_user_id,t2.subject,t2.score
from studentInfo t1cross  join studentScore t2

1.7 join on和where条件区别

       两者之间的区别见文章:
Hive中left join 中的where 和 on的区别-CSDN博客文章浏览阅读1.2k次,点赞21次,收藏23次。Hive中left join 中的where 和 on的区别https://blog.csdn.net/SHWAITME/article/details/135892183?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170780016016800197016026%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=170780016016800197016026&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-1-135892183-null-null.nonecase&utm_term=where&spm=1018.2226.3001.4450

1.8 join中不能有null

  • group by字段为null,会导致结果不正确(null值也会参与group by 分组)

group by column1
  • join字段为null会导致结果不正确(例如:下述 t2.b字段是null值)
t1 left join t2 on t1.a=t2.a and t1.b=t2.b 

1.9 join操作导致数据膨胀

select *
from a 
left join b 
on a.id = b.id 

     如果主表a的id是唯一的,副表b的id有重复值,非唯一,那当on a.id = b.id 时,就会导致数据膨胀(一条变多条)。因此两表或多表join的时候,需保证join的字段唯一性,否则会出现一对多的数据膨胀现象。

二、Hive的谓词下推

2.1 谓词下推概念

      在不影响结果的情况下,尽量将过滤条件提前执行。谓词下推后,过滤条件在map端执行,减少了map端的输出,降低了数据在集群上传输的量,提升任务性能。

     在hive生成的物理执行计划中,有一个配置项用于管理谓词下推是否开启。

set hive.optimize.ppd=true; 默认是true

   疑问:如果hive谓词下推的功能与join同时存在,那下推功能可以在哪些场景下生效

2.2 谓词下推场景分析

     数据准备:以上述两张表studentInfo、studentScore为例

    查看谓词下推是否开启:set hive.optimize.ppd;

(1) inner join 内连接

  • 对左表where过滤
 explain
selectt1.user_id as t1_user_id,t1.name,t1.gender,t2.user_id as t2_user_id,t2.subject,t2.score
from studentInfo t1inner join studentScore t2 on t1.user_id = t2.user_id
where t1.user_id >2

     explain查看执行计划,在对t2表进行scan后,优先对t1表进行filter,过滤t1.user_id >2,即谓词下推生效。

  • 对右表where过滤
 explain
selectt1.user_id as t1_user_id,t1.name,t1.gender,t2.user_id as t2_user_id,t2.subject,t2.score
from studentInfo t1inner join studentScore t2 on t1.user_id = t2.user_id
where t2.user_id is not null

    explain查看执行计划,在对t2表进行scan后,优先进行filter,过滤t2.user_id is not null,即谓词下推生效。

 

  • 对左表on过滤
explain
selectt1.user_id as t1_user_id,t1.name,t1.gender,t2.user_id as t2_user_id,t2.subject,t2.score
from studentInfo t1inner join studentScore t2 on t1.user_id = t2.user_id and t1.user_id >2

    explain查看执行计划,在对t2表进行scan后,优先对t1表进行filter,过滤t1.user_id >2,即谓词下推生效。

  • 对右表on过滤
 explain
selectt1.user_id as t1_user_id,t1.name,t1.gender,t2.user_id as t2_user_id,t2.subject,t2.score
from studentInfo t1inner join studentScore t2 on t1.user_id = t2.user_id and t2.user_id is not null

    explain查看执行计划,在对t2表进行scan后,优先进行filter,过滤t2.user_id is not null,即谓词下推生效。 

 (2) left join(right join 同理)

  • 对左表where过滤
explain
selectt1.user_id,t1.name,t1.gender,t2.user_id,t2.subject,t2.score
from studentInfo t1left  join studentScore t2on t1.user_id = t2.user_id
where t1.user_id >2;

    explain查看执行计划,在对t2表进行scan后,优先对t1表进行filter,过滤t1.user_id >2,即谓词下推生效。

  • 对右表where过滤
explain
selectt1.user_id,t1.name,t1.gender,t2.user_id,t2.subject,t2.score
from studentInfo t1left  join studentScore t2on t1.user_id = t2.user_id
where t2.user_id is not null;

     explain查看执行计划,在对t2表进行scan后,优先进行filter,过滤t2.user_id is not null,即谓词下推生效。 

 

  • 对左表on过滤
explain 
selectt1.user_id as t1_user_id,t1.name,t1.gender,t2.user_id as t2_user_id,t2.subject,t2.score
from studentInfo t1left join studentScore t2on t1.user_id = t2.user_id and t1.user_id >2

      explain查看执行计划,在对t2表进行scan后,在对t1表未进行filter,即谓词下推不生效

 

  • 对右表on过滤
explain
selectt1.user_id as t1_user_id,t1.name,t1.gender,t2.user_id as t2_user_id,t2.subject,t2.score
from studentInfo t1left join studentScore t2on t1.user_id = t2.user_id and t2.user_id is not null;

      explain查看执行计划,在对t2表进行scan后,优先进行filter,过滤t2.user_id is not null,即谓词下推生效。 

 (3) full join

  • 对左表where过滤
explain 
selectt1.user_id as t1_user_id,t1.name,t1.gender,t2.user_id as t2_user_id,t2.subject,t2.score
from studentInfo t1full  join studentScore t2on t1.user_id = t2.user_id
where  t1.user_id >2 ;

     explain查看执行计划,在对t2表进行scan后,优先对t1表进行filter,过滤t1.user_id >2,即谓词下推生效。

 

  • 对右表where过滤
explain
selectt1.user_id as t1_user_id,t1.name,t1.gender,t2.user_id as t2_user_id,t2.subject,t2.score
from studentInfo t1full  join studentScore t2on t1.user_id = t2.user_id
where  t2.user_id is not null

     explain查看执行计划,在对t1 表进行scan后,优先进行filter,过滤t2.user_id is not null,即谓词下推生效。 

  • 对左表on过滤
explain
selectt1.user_id as t1_user_id,t1.name,t1.gender,t2.user_id as t2_user_id,t2.subject,t2.score
from studentInfo t1full  join studentScore t2on t1.user_id = t2.user_id and t1.user_id >2;

       explain查看执行计划,在对t1表进行scan后,未对t1表进行filter,即谓词下推不生效

  • 对右表on过滤
explain
selectt1.user_id as t1_user_id,t1.name,t1.gender,t2.user_id as t2_user_id,t2.subject,t2.score
from studentInfo t1full  join studentScore t2on t1.user_id = t2.user_id and t2.user_id is not null;

     explain查看执行计划,在对t1表进行scan后,未对t2表未进行filter,即谓词下推不生效

总结:

hive中谓词下推的各种场景下的生效情况如下表:

inner joinleft joinright joinfull join
左表右表左表右表左表右表左表右表
where条件
on条件××××

三、Hive Join的数据倾斜

          待补充

参考文章:

Hive的Join操作_hive join-CSDN博客

《Hive用户指南》- Hive的连接join与排序_hive 对主表排序后连接查询能保持顺序吗-CSDN博客

Hive 中的join和谓词下推_hive谓词下推-CSDN博客

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/472751.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

鸿蒙系统优缺点,能否作为开发者选择

凡是都有对立面,就直接说说鸿蒙的优缺点吧。 鸿蒙的缺点: 鸿蒙是从2019年开始做出来的,那时候是套壳Android大家都知晓。从而导致大家不看鸿蒙系统,套壳Android就是多次一举。现在鸿蒙星河版已经是纯血鸿蒙,但是它的…

高性能MySQL 阅读笔记

mysql由服务器端与存储引擎两部分组成,存储引擎部分的锁机制对服务器端是透明的。服务器端内置缓存机制,有解析器和优化器机制。不同的存储引擎对事务、并发等都用不同的处理。 ACID代表的特性:原子性、一致性、隔离性、持久性 共享锁与排他…

【Web】CTFSHOW java刷题记录(全)

目录 web279 web280 web281 web282 web283 web284 web285 web286 web287 web288 ​web289 web290 web291 web292 web293 web294 web295 web296 web297 web298 web299 web300 web279 题目提示 url里告诉我们是S2-001 直接进行一个exp的搜 S2-001漏洞分析…

网络安全最典型基础靶场-DVWA-本地搭建与初始化

写在前面: 之前也打过这个 DVWA 靶场,但是是在虚拟机环境下的一个小块分区靶场; 本篇博客主要介绍在本地搭建 DVWA 靶场以及靶场的初始化,后续会陆续更新通关教程。 由于我们是在本地搭建,则需要基于你已经装好 phpstu…

C++数据结构与算法——双指针法

C第二阶段——数据结构和算法,之前学过一点点数据结构,当时是基于Python来学习的,现在基于C查漏补缺,尤其是树的部分。这一部分计划一个月,主要利用代码随想录来学习,刷题使用力扣网站,不定时更…

Windows 连接共享文件夹 切换/退出账号操作

工作中遇到个问题,登录公司内部共享文件夹,使用自己的账号,但需要切换别人账号找东西时,没有发现登出的地方。在网上找了两种方法:通过命令行登出账号的方法 (1)打开cmd命令提示符,…

MongoDB从入门到实战之.NET Core使用MongoDB开发ToDoList系统(3)-系统数据集合设计

前言 前几章教程我们把ToDoList系统的基本框架搭建好了,现在我们需要根据我们的需求把ToDoList系统所需要的系统集合(相当于关系型数据库中的数据库表)。接下来我们先简单概述一下这个系统主要需要实现的功能以及实现这些功能我们需要设计那些…

快速部署MES源码/万界星空科技开源MES

什么是开源MES软件? 开源MES软件是指源代码可以免费获取、修改和分发的MES软件。与传统的商业MES软件相比,开源MES软件具有更高的灵活性和可定制性。企业可以根据自身的需求对软件进行定制化开发,满足不同生产环境下的特定需求。 开源MES软件…

【Java程序员面试专栏 分布式中间件】Redis 核心面试指引

关于Redis部分的核心知识进行一网打尽,包括Redis的基本概念,基本架构,工作流程,存储机制等,通过一篇文章串联面试重点,并且帮助加强日常基础知识的理解,全局思维导图如下所示 基础概念 明确redis的特性、应用场景和数据结构 什么是Redis,Redis有哪些应用场景 Redi…

关于Swin Transformer的架构记录

Swin Transformer 可以说是批着Transformer外表的卷积神经网络。 具体的架构如下图所示: 首先我们得到一张224*224*3的图片。 通过分成4*4的patch,变成了56*56*48。 线性变换后又变成了56*56*96。 然后利用了Swin Transformer中一个比较特别的结构 P…

Day-01-01

项目准备 项目介绍 1. 业务功能 本项目包括了用户端、机构端、运营端。 2. 技术选型 各层简要说明: 环境配置 1. 版本信息 在开发过程中,个人使用gitee作为版本控制工具。 2. 虚拟机安装说明 根据所提供资料,虚拟机IP地址已设置为192.1…

人工智能学习与实训笔记(十四):Langchain Agent

0、概要 Agent是干什么的? Agent的核心思想是使用语言模型(LLM)作为推理的大脑,以制定解决问题的计划、借助工具实施动作。在agents中几个关键组件如下: Agent:制定计划和思考下一步需要采取的行动。Tools…