0. 说明

  在 Hive 中进行 join 操作


1. 操作步骤

  1.0 建表

  在 hiveserver2 服务启动的前提下,在 Beeline客户端中输入以下命令

# 新建顾客表
create table customers(id int, name string, age int) row format delimited fields terminated by '\t'; # 新建订单表
create table orders(oid int, oname string, oprice float, uid int) row format delimited fields terminated by '\t';

  1.1 创建并插入数据

  创建,略

  插入命令如下:

# 插入顾客数据
load data local inpath '/home/centos/files/customers.txt' into table customers; # 插入订单数据
load data local inpath '/home/centos/files/orders.txt' into table orders;

  1.2 使用 join

# 内连接
select a.id, a.name, b.oname, b.oprice from customers a inner join orders b on a.id=b.uid;
# 左外连接
select a.id, a.name, b.oname, b.oprice from customers a left outer join orders b on a.id=b.uid;
# 右外连接
select a.id, a.name, b.oname, b.oprice from customers a right outer join orders b on a.id=b.uid;
# 全外连接
select a.id, a.name, b.oname, b.oprice from customers a full outer join orders b on a.id=b.uid;

2. join 的分类与优化

  2.1 普通 join

select a.id, a.name, b.orderno, b.oprice from customers a inner join orders b on a.id=b.cid;

  a inner join b  // 返回行数 a ∩ b

  a left [outer] join b   // 返回行数 a

  a right [outer] join b  // 返回行数 b

  a full [outer] join b  // 返回行数 a+b - (a ∩ b)

  a cross join b  // 返回行数 a * b

2.2 特殊 join 优化

  map join

  小表+大表 => 将小表加入到分布式缓存,通过迭代大表所有数据进行处理

  在老版的 Hive 中(0.7)之前,所有的 join 操作都是在 reduce 端执行的(reduce 端 join)
  想要进行 map 端 join,需要进行以下操作

  SET hive.auto.convert.join=true;
  声明暗示 a join b , a小表,b大表
  /*+ mapjoin(小表) */

  SELECT /*+ MAPJOIN(a) */ a.id, a.name, b.orderno, b.oprice from customers a inner join orders b on a.id=b.cid;

  在新版 Hive 中,如果想要进行 map 端 join

  jdbc:hive2://> SET hive.auto.convert.join=true;  //设置自动转换成 map 端 join
  jdbc:hive2://> SET hive.mapjoin.smalltable.filesize=600000000;  //设置 map 端 join 中小表的最大值,默认25M

common join

  即 reduce 端 join
  1. 声明暗示,指定大表
  /*+ STREAMTABLE(大表) */

  2. 将大表放在右侧

2.3 测试

  测试:customers 和 orders

  1. 不写任何暗示,观察是 map 端 join 还是 reduce join

SELECT a.no, a.name, b.oname, b.oprice from customers a inner join orders b on a.no=b.uid;

  2. 写暗示,观察效果

SELECT /*+ MAPJOIN(a) */ a.no, a.name, b.oname, b.oprice from customers a inner join orders b on a.no=b.uid;

  3. 将自动转换 map join 设置成 false

SET hive.auto.convert.join=false;

  4. 写 reduce 端 join 的暗示,观察结果

SELECT /*+ STREAMTABLE(a) */ a.no, a.name, b.oname, b.oprice from customers a inner join orders b on a.no=b.uid;

[Hive_add_5] Hive 的 join 操作的更多相关文章

  1. hive:join操作

    hive的多表连接,都会转换成多个MR job,每一个MR job在hive中均称为Join阶段.按照join程序最后一个表应该尽量是大表,因为join前一阶段生成的数据会存在于Reducer 的bu ...

  2. Hive中JOIN操作

    1. 只支持相等JOIN. 2. 多表连接当使用不同的列进行JOIN时,会产生多个MR作业. 3. 最后的表的数据是从流中读取,而前面的会在内存中缓存,因此最好把最大的表放在最后. SELECT /* ...

  3. hive中的子查询改join操作(转)

    这些子查询在oracle和mysql等数据库中都能执行,但是在hive中却不支持,但是我们可以把这些查询语句改为join操作: -- 1.子查询 select * from A a where a.u ...

  4. 使用MapReduce实现join操作

     在关系型数据库中,要实现join操作是非常方便的,通过sql定义的join原语就可以实现.在hdfs存储的海量数据中,要实现join操作,可以通过HiveQL很方便地实现.不过HiveQL也是转化成 ...

  5. HIVE: Map Join Vs Common Join, and SMB

    HIVE  Map Join is nothing but the extended version of Hash Join of SQL Server - just extending Hash ...

  6. MapReduce 实现数据join操作

    前段时间有一个业务需求,要在外网商品(TOPB2C)信息中加入 联营自营 识别的字段.但存在的一个问题是,商品信息 和 自营联营标示数据是 两份数据:商品信息较大,是存放在hbase中.他们之前唯一的 ...

  7. Hive中Join的原理和机制

    转自:http://lxw1234.com/archives/2015/06/313.htm 笼统的说,Hive中的Join可分为Common Join(Reduce阶段完成join)和Map Joi ...

  8. hive的join

    第一:在map端产生join          mapJoin的主要意思就是,当链接的两个表是一个比较小的表和一个特别大的表的时候,我们把比较小的table直接放到内存中去,然后再对比较大的表格进行m ...

  9. hive的join查询

    hive的join查询 语法 join_table: table_reference [INNER] JOIN table_factor [join_condition] | table_refere ...

随机推荐

  1. Nacos系列:基于Nacos的配置中心

    前言 在看正文之前,我想请你回顾一下自己待过的公司都是怎么管理配置的,我想应该会有以下几种方式: 1.硬编码 没有什么配置不配置的,直接写在代码里面,比如使用常量类 优势:对开发友好,开发清楚地知道代 ...

  2. python字符串类型

    python中的字符串 python中的字符串类型为str,也是平时操作的类型.但除了str类型,还有一个专门的名为"string"的模块(导入即可知),是很早以前没有str类型的 ...

  3. python按引用赋值和深、浅拷贝

    按引用赋值而不是拷贝副本 在python中,无论是直接的变量赋值,还是参数传递,都是按照引用进行赋值的. 在计算机语言中,有两种赋值方式:按引用赋值.按值赋值.其中按引用赋值也常称为按指针传值(当然, ...

  4. navicat连接不上Linux服务器上的MySQL

    1.首先确定你的linux已经关闭防火墙 详细操作点这里: 如果是公司服务器防火墙比较重要不能关闭,那就麻烦点了,需要在防火墙的配置文件下配置属性. 如果还不能解决,请继续往下看. 2.如果是云服务器 ...

  5. Spring Boot入门(12)实现页面访问量统计功能

      在日常的网站使用中,经常会碰到页面的访问量(或者访问者人数)统计.那么,在Spring Boot中该如何实现这个功能呢?   我们的想法是比较简单的,那就是将访问量储存在某个地方,要用的时候取出来 ...

  6. [转]GitLab Continuous Integration (GitLab CI/CD)

    本文转自:https://docs.gitlab.com/ee/ci/README.html GitLab Continuous Integration (GitLab CI/CD) The bene ...

  7. Mac下写博客工具MarsEdit相关资料

    参考资料: https://www.maoshu.cc/967.html 下载地址: https://www.red-sweater.com/marsedit/ 博客园的配置: http://www. ...

  8. java动态获取WebService的两种方式(复杂参数类型)

    java动态获取WebService的两种方式(复杂参数类型) 第一种: @Override public OrderSearchListRes searchOrderList(Order_Fligh ...

  9. virtualbox中 Ubuntu挂载共享文件夹

    1. 进入 /mnt 目录 2. 新建目录 share 3. sudo mount -t vboxsf share /mnt/share(其中的第一个share是共享文件夹的名字) 4. 成功

  10. datetime模块+calendar模块

    datetime: """ 模块中的类: datatime 同时有时间和日期 timedelta 主要用于计算时间的跨度 tzinfo 时区相关 time 只关注时间 d ...