一、使用条件

hive2.2.0及之后的版本支持使用merge into 语法,使用源表数据批量目标表的数据。使用该功能还需做如下配置

1、参数配置
set hive.support.concurrency = true;
set hive.enforce.bucketing = true;
set hive.exec.dynamic.partition.mode = nonstrict;
set hive.txn.manager = org.apache.hadoop.hive.ql.lockmgr.DbTxnManager;
set hive.compactor.initiator.on = true;
set hive.compactor.worker.threads = 1;
set hive.auto.convert.join=false;
set hive.merge.cardinality.check=false; -- 目标表中出现重复匹配时要设置该参数才行
2、建表要求

Hive对使用Update功能的表有特定的语法要求, 语法要求如下: (1)要执行Update的表中, 建表时必须带有buckets(分桶)属性 (2)要执行Update的表中, 需要指定格式,其余格式目前赞不支持, 如:parquet格式, 目前只支持ORCFileformat和AcidOutputFormat (3)要执行Update的表中, 建表时必须指定参数('transactional' = true);

DROP TABLE IF EXISTS dim_date_10000;
create table dim_date_10000(
date_key       string                 comment'如:2018-08-08'
,day             int                 comment'日(1~31)'
,month           int                 comment'月,如:8'
,month_name     string       comment'月名称,如:8月'
,year            int                   comment'年,如:2018'
,year_month       int                   comment'年月,如201808'
,week_of_year   string                   comment'年内第几周 2018-1'
,week            int                 comment'周(1~7)'
,week_name       string         comment'周,如星期三'
,quarter         int                 comment'季(1~4)'
)
CLUSTERED BY (date_key) INTO 10 buckets
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
STORED AS orc
TBLPROPERTIES('transactional'='true');
3、批量更新语法
 MERGE INTO <target table> AS T USING <source expression/table> AS S
ON <``boolean` `expression1>
WHEN MATCHED [AND <``boolean` `expression2>] THEN UPDATE SET <set clause list>
WHEN MATCHED [AND <``boolean` `expression3>] THEN DELETE
WHEN NOT MATCHED [AND <``boolean` `expression4>] THEN INSERT VALUES<value list>

二、批量更新语法对比

对比在hive1.1.0 使用overwrite ,hive2.3.5使用merge into的方式 ,对不同量级的数据进行更新时的语法及效率。

1、更新语法

hive 2.3.5 merge into相较与Hive1.1.0 overwrite 更新方式语法更简洁。

Hive1.1.0
insert overwrite table dim_date_100w
-- 旧的改变了的数据
select t2.date_key,t2.day,t2.month,t2.month_name,t2.year,t2.year_month,t2.week_of_year,t2.week,t2.week_name,1001 as quarter
from dim_date_100w t1
join dim_date_1w t2 on t1.date_key=t2.date_key
-- 旧的不变的数据
union all
select t1.*
from dim_date_100w t1
left join dim_date_1w t2 on t1.date_key=t2.date_key
where t2.date_key is null
-- 新增的数据
union all
select t1.*
from dim_date_1w t1
left join dim_date_100w t2 on t1.date_key=t2.date_key
where t2.date_key is null
;
Hive2.3.5
MERGE INTO dim_date_100w AS T USING dim_date_1w AS S
ON t.date_key=s.date_key
WHEN MATCHED THEN UPDATE SET quarter=1001
WHEN NOT MATCHED THEN INSERT VALUES(S.date_key,S.day,S.month,S.month_name,S.year,S.year_month,S.week_of_year,S.week,S.week_name,S.quarter);
2、更新用时

两种更新方式在10w及10000w时更新用时相差不多,在1000w时Hive2.3.5用时只需Hive1.1.0的一半。

目标表数据量 源表数据量 Hive1.1.0 批量更新用时 Hive2.3.5 批量更新用时
10w条 1w 90s 80s
1000W 1w 330s 160s
1000w 100w 340s 180s
10000w 1w 640s 610s
10000w 100w 700s 630s

hive merge into 批量更新测试的更多相关文章

  1. sql server merge into 与update 批量更新1 百万测试数据的性能比较

    1. 1百万的测试数据的生成 declare @index int;  begin  set @index=0;  while @index<1000000  begin  insert int ...

  2. spring data jpa开启批量插入、批量更新

    spring data jpa开启批量插入.批量更新 原文链接:https://www.cnblogs.com/blog5277/p/10661096.html 原文作者:博客园--曲高终和寡 *** ...

  3. MongoDB学习笔记~大叔分享批量添加—批量更新—批量删除

    回到目录 说它是批量操作,就是说将集合对象一次提交到服务器,并对数据进行持久化,如果您的代码是一次一次的提交,那不算是批量操作!在之前的mongodb仓储中并没有对批量更新和批量删除进行实现,而今天在 ...

  4. jdbc-批量插入、批量删除、批量更新

    一.JDBC的批量插入 JDBC批量插入主要用于数据导入和日志记录因为日志一般都是先写在文件下的等.    我用Mysql5.1.5的JDBC driver 分别对三种比较常用的方法做了测试   方法 ...

  5. mybatis3批量更新 批量插入

    在公司ERP项目开发中,遇到批量数据插入或者更新,因为每次连接数据库比较耗时,所以决定改为批量操作,提升效率.库存盘点导入时,需要大量数据批量操作. 1:数据库连接代码中必须开启批量操作.加上这句,& ...

  6. mysql 批量更新与批量更新多条记录的不同值实现方法

    批量更新 mysql更新语句很简单,更新一条数据的某个字段,一般这样写: 代码如下: UPDATE mytable SET myfield = 'value' WHERE other_field = ...

  7. 使用FMDB事务批量更新数据库

    今天比较闲看到大家在群里讨论关于数据库操作的问题,其中谈到了“事务”这个词,坦白讲虽然作为计算机专业的学生,在上学的时候确实知道存储过程.触发器.事务等等这些名词的概念,但是由于毕业后从事的不是服务器 ...

  8. iOS中使用FMDB事务批量更新数据库

    今天比较闲看到大家在群里讨论关于数据库操作的问题,其中谈到了"事务"这个词,坦白讲虽然作为计算机专业的学生,在上学的时候确实知道存储过程.触发器.事务等等这些名词的概念,但是由于毕 ...

  9. mysql 批量更新

    bs_user 表,我们叫他 bu表, 字段user_id,len_id, think_wellUser 表,我们简称为tw表,中的user_id ,len_id 其中tw表的user_id 是bu表 ...

随机推荐

  1. c#代码安装字体文件

    public class FontOperate { [DllImport("kernel32.dll", SetLastError = true)] static extern ...

  2. C#基础加强篇----委托、Lamada表达式和事件(上)

    1.委托 C#的委托相当于C/C++中的函数指针.函数指针用指针获取一个函数的入口地址,实现对函数的操作. 委托与C/C++中的函数指针不同在于,委托是面向对象的,是引用类型,对委托的使用要先定义后实 ...

  3. WPF无边框移动窗体

    WPF无边框移动窗体,先在<Window>里添加 MouseLeftButtonDown=”Window_MouseLeftButtonDown” 然后导航到事件,在事件里添加 if (e ...

  4. uwp开发:数据绑定——值转换器 的简单使用

    原文:uwp开发:数据绑定--值转换器 的简单使用 今天,我在做最近正在开发的“简影”uwp应用时遇到一个问题,其中有个栏目,叫做“画报”,是分组显示一组一组的 图片,每组图片在界面上只显示9个,点击 ...

  5. “真正的工作不是说的天花乱坠”,Torvalds 说, “而是在于细节”(Torvalds 认为成功的项目都是99%的汗水和1%的创新)

    在刚刚结束的加利福尼亚州的开源领袖峰会(2月14日-16日)上,Linus Torvalds 接受了外媒的采访,分享了他如何管理 Linux kernel 的开发以及他对工作的态度. “真正的工作不是 ...

  6. 逆向工程mybatis-geneator.xml

    <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfiguration ...

  7. VS2013编译Qt5.6.0静态库,并提供了百度云下载(乌合之众)good

    获取qt5.6.0源码包 直接去www.qt.io下载就好了,这里就不详细说了. 这里是我已经编译好的** 链接:http://pan.baidu.com/s/1pLb6wVT 密码: ak7y ** ...

  8. 在本地安装RabbitMQ Server教程以及可能遇到的问题及解决办法

    1. Download latest erlang OTP platform from : erlang:http://www.erlang.org/download.html (The latest ...

  9. redis连接错误3种解决方案System Error MISCONF Redis is configured to save RDB snapshots

    redis连接错误System Error MISCONF Redis is configured to save RDB snapshots, but XX   情况1解决办法: 由于强制停止red ...

  10. aspose授权亲测可用配套代码

    支持excel,word,ppt,pdf using Aspose.Cells; using Aspose.Words.Saving; using ESBasic; using OMCS.Engine ...