Hive的Join连接、谓词下推

2024-02-14 17:28
文章标签 连接 join hive 谓词 下推

本文主要是介绍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博客

这篇关于Hive的Join连接、谓词下推的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/709137

相关文章

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

SQL 中多表查询的常见连接方式详解

《SQL中多表查询的常见连接方式详解》本文介绍SQL中多表查询的常见连接方式,包括内连接(INNERJOIN)、左连接(LEFTJOIN)、右连接(RIGHTJOIN)、全外连接(FULLOUTER... 目录一、连接类型图表(ASCII 形式)二、前置代码(创建示例表)三、连接方式代码示例1. 内连接(I

java如何通过Kerberos认证方式连接hive

《java如何通过Kerberos认证方式连接hive》该文主要介绍了如何在数据源管理功能中适配不同数据源(如MySQL、PostgreSQL和Hive),特别是如何在SpringBoot3框架下通过... 目录Java实现Kerberos认证主要方法依赖示例续期连接hive遇到的问题分析解决方式扩展思考总

Python中连接不同数据库的方法总结

《Python中连接不同数据库的方法总结》在数据驱动的现代应用开发中,Python凭借其丰富的库和强大的生态系统,成为连接各种数据库的理想编程语言,下面我们就来看看如何使用Python实现连接常用的几... 目录一、连接mysql数据库二、连接PostgreSQL数据库三、连接SQLite数据库四、连接Mo

oracle如何连接登陆SYS账号

《oracle如何连接登陆SYS账号》在Navicat12中连接Oracle11g的SYS用户时,如果设置了新密码但连接失败,可能是因为需要以SYSDBA或SYSOPER角色连接,解决方法是确保在连接... 目录oracle连接登陆NmOtMSYS账号工具问题解决SYS用户总结oracle连接登陆SYS账号

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

VScode连接远程Linux服务器环境配置图文教程

《VScode连接远程Linux服务器环境配置图文教程》:本文主要介绍如何安装和配置VSCode,包括安装步骤、环境配置(如汉化包、远程SSH连接)、语言包安装(如C/C++插件)等,文中给出了详... 目录一、安装vscode二、环境配置1.中文汉化包2.安装remote-ssh,用于远程连接2.1安装2

关于rpc长连接与短连接的思考记录

《关于rpc长连接与短连接的思考记录》文章总结了RPC项目中长连接和短连接的处理方式,包括RPC和HTTP的长连接与短连接的区别、TCP的保活机制、客户端与服务器的连接模式及其利弊分析,文章强调了在实... 目录rpc项目中的长连接与短连接的思考什么是rpc项目中的长连接和短连接与tcp和http的长连接短

Xshell远程连接失败以及解决方案

《Xshell远程连接失败以及解决方案》本文介绍了在Windows11家庭版和CentOS系统中解决Xshell无法连接远程服务器问题的步骤,在Windows11家庭版中,需要通过设置添加SSH功能并... 目录一.问题描述二.原因分析及解决办法2.1添加ssh功能2.2 在Windows中开启ssh服务2

Redis连接失败:客户端IP不在白名单中的问题分析与解决方案

《Redis连接失败:客户端IP不在白名单中的问题分析与解决方案》在现代分布式系统中,Redis作为一种高性能的内存数据库,被广泛应用于缓存、消息队列、会话存储等场景,然而,在实际使用过程中,我们可能... 目录一、问题背景二、错误分析1. 错误信息解读2. 根本原因三、解决方案1. 将客户端IP添加到Re