参考文末文章,加上自己的理解。

1、增量更新

有一个 base_table 表存放的是 12 月 15 日及其之前的所有数据,当 12 月 16 日的数据产生后,存入 incremental_table 表的当日分区中。

现在需要,将 incremental_table 表的新增数据合并到 base_table 表中。

那么,就有两种情况:

(1)保留历史数据

通过将主表建成拉链表实现:

将 历史数据中修改了的数据 union 当日新增的数据,

再 insert overwrite 到 base_table 表。

这样的话,就会存在重复的数据,保留了历史数据。

(2)不保留了历史数据

方法1:

先将 base_table 表和 incremental_table 表 left join,将 base_table 表中没有修改的数据插入到 base_table 表,

再将 incremental_table 表中的增量数据(最新数据)插入到 base_table 表。

方法2:

将 base_table 表和 incremental_table 表 union all ,再取更新时间最新的记录。

这样,就不会存在重复的数据,但是没有了历史数据。

2、对第一种情况

通过将主表建成拉链表实现

2.1、准备工作

(1)建表

-- 存放产生的每日增量数据,按天分区
create table incremental_table (
id string,
name string,
addr string
) comment '增量表'
partitioned by (dt string)
row format delimited fields terminated by ','
stored as textfile; -- 存放更新后的数据
create table base_table (
id string,
name string,
addr string,
start_date string,
end_date string
) comment '主表'
row format delimited fields terminated by ','
stored as textfile;

(2)数据

incre0.txt:导入主表的历史数据

(模拟主表已有数据)

1,lijie,chongqing,20191020,99991231
2,zhangshan,sz,20191020,99991231
3,lisi,shanghai,20191020,99991231
4,wangwu,usa,20191020,99991231

incre1.txt:导入增量表的 20191020 新增数据

1,lijie,chongqing
2,zhangshan,sz
3,lisi,shanghai
4,wangwu,usa

incre2.txt:导入增量表的 20191021 新增数据

1,lijie,chengdu      # 地址变了
2,zhangshan,huoxing # 地址变了
4,wangwu,lalalala # 地址变了
5,xinzeng,hehe # 新增数据

(3)导入数据

-- 将 incre0.txt 导入主表中,表示主表已经有数据了,
-- 现在需要更新主表里的数据
load data local inpath '/root/data/incre0.txt' overwrite into table base_table; hive> select * from base_table;
OK
1 lijie chongqing 20191020 99991231
2 zhangshan sz 20191020 99991231
3 lisi shanghai 20191020 99991231
4 wangwu usa 20191020 99991231 -- 将 incre1.txt 和 incre2.txt 分别导入增量表中的相应分区中
load data local inpath '/root/data/incre1.txt' overwrite into table incremental_table partition (dt='20191020'); load data local inpath '/root/data/incre2.txt' overwrite into table incremental_table partition (dt='20191021'); hive> select * from incremental_table;
OK
1 lijie chongqing 20191020
2 zhangshan sz 20191020
3 lisi shanghai 20191020
4 wangwu usa 20191020
1 lijie chengdu 20191021
2 zhangshan huoxing 20191021
4 wangwu lalalala 20191021
5 xinzeng hehe 20191021

2.2、更新数据

-- 将 历史数据中修改了的数据 union 当日新增的数据,
-- 再 insert overwrite 到 base_table 表。
-- 也可以使用 hive 的 merge into 语法,但从 Hive 2.2 版本才开始可用,且只能在支持 ACID 的表上执行。
insert overwrite table base_table
select * from
(
select a.id, -- 更新历史数据中修改了的数据
a.name,
a.addr,
a.start_date,
case
when a.end_date='99991231' and b.id is not null then '20191020' -- 更新了end_date
else a.end_date
end as end_date
from base_table as a
left join (select * from incremental_table where dt='20191021') as b
on a.id=b.id
union
select c.id, -- 添加当日新增的数据
c.name,
c.addr,
'20191021' as start_date,
'99991231' as end_date
from incremental_table c
where c.dt='20191021'
) as t; hive> select * from base_table;
OK
1 lijie chengdu 20191021 99991231
1 lijie chongqing 20191020 20191020
2 zhangshan huoxing 20191021 99991231
2 zhangshan sz 20191020 20191020
3 lisi shanghai 20191020 99991231
4 wangwu lalalala 20191021 99991231
4 wangwu usa 20191020 20191020
5 xinzeng hehe 20191021 99991231

3、对第二种情况

3.1、准备工作

(1)建表

create table incremental_table (
id string,
name string,
addr string
) comment '增量表'
partitioned by (dt string)
row format delimited fields terminated by ','
stored as textfile; create table base_table (
id string,
name string,
addr string
) comment '主表'
partitioned by (dt string)
row format delimited fields terminated by ','
stored as textfile;

(2)数据

源数据incre0.txt

1,lijie,chongqing
2,zhangshan,sz
3,lisi,shanghai
4,wangwu,usa

增量数据incre1.txt

1,lijie,chengdu      # 地址变了
2,zhangshan,huoxing # 地址变了
4,wangwu,lalalala # 地址变了
5,xinzeng,hehe # 新增数据

(3)导入数据

-- 将 incre0.txt 导入主表中
load data local inpath '/root/data/incre0.txt' overwrite into table base_table partition (dt='20191020'); hive> select * from base_table;
OK
1 lijie chongqing 20191020
2 zhangshan sz 20191020
3 lisi shanghai 20191020
4 wangwu usa 20191020 -- 将 incre0.txt 和 incre1.txt 导入增量表中
load data local inpath '/root/data/incre0.txt' overwrite into table incremental_table partition (dt='20191020'); load data local inpath '/root/data/incre1.txt' overwrite into table incremental_table partition (dt='20191021'); hive> select * from incremental_table;
OK
1 lijie chongqing 20191020
2 zhangshan sz 20191020
3 lisi shanghai 20191020
4 wangwu usa 20191020
1 lijie chengdu 20191021
2 zhangshan huoxing 20191021
4 wangwu lalalala 20191021
5 xinzeng hehe 20191021

3.2、方法1

先将 base_table 表和 incremental_table 表 left join,将 base_table 表中没有修改的数据插入到 base_table 表,

再将 incremental_table 表中的增量数据插入到 base_table 表。

hive> select * from base_table;
OK
1 lijie chongqing 20191020
2 zhangshan sz 20191020
3 lisi shanghai 20191020
4 wangwu usa 20191020 hive> select * from incremental_table;
OK
1 lijie chongqing 20191020
2 zhangshan sz 20191020
3 lisi shanghai 20191020
4 wangwu usa 20191020
1 lijie chengdu 20191021
2 zhangshan huoxing 20191021
4 wangwu lalalala 20191021
5 xinzeng hehe 20191021 insert overwrite table base_table
select a.id, -- 插入 base_table 表中没有修改的数据
a.name,
a.addr,
a.dt
from base_table a
left join (select * from incremental_table where dt='20191021') b
on a.id=b.id
where b.id is null
union
select c.id, -- 插入 incremental_table 表中的增量数据,即最新数据
c.name,
c.addr,
c.dt
from (select * from incremental_table where dt='20191021') c; hive> select * from base_table;
OK
3 lisi shanghai 20191020
1 lijie chengdu 20191021
2 zhangshan huoxing 20191021
4 wangwu lalalala 20191021
5 xinzeng hehe 20191021

3.3、方法2

将 base_table 表和 incremental_table 表 union all ,再取更新时间最新的记录。

【可以通过窗口函数编一个序号,也可以使用 hive 的预定义属性最近更新时间字段】

hive> select * from base_table;
OK
1 lijie chongqing 20191020
2 zhangshan sz 20191020
3 lisi shanghai 20191020
4 wangwu usa 20191020 hive> select * from incremental_table;
OK
1 lijie chongqing 20191020
2 zhangshan sz 20191020
3 lisi shanghai 20191020
4 wangwu usa 20191020
1 lijie chengdu 20191021
2 zhangshan huoxing 20191021
4 wangwu lalalala 20191021
5 xinzeng hehe 20191021 insert overwrite table base_table
select b.id,b.name,b.addr,b.dt
from
(
select a.*,
row_number() over(distribute by a.id sort by a.dt desc) as rn
from
(
select id,name,addr,dt from base_table
union all -- 这里是 union all
select id,name,addr,dt from incremental_table where dt='20191021'
) a
) b
where b.rn=1; hive> select * from base_table;
OK
3 lisi shanghai 20191020
1 lijie chengdu 20191021
2 zhangshan huoxing 20191021
4 wangwu lalalala 20191021
5 xinzeng hehe 20191021

参考地址:

https://www.cnblogs.com/lxbmaomao/p/9821128.html

https://blog.csdn.net/qq_20641565/article/details/52763663

https://blog.csdn.net/qq_20641565/article/details/53164155?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control

https://blog.csdn.net/ZhouyuanLinli/article/details/86638454?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.control

使用hive增量更新的更多相关文章

  1. hive不分区增量更新

    insert overwrite table ods.zeg_so select *,case when zsm.id is not null then cast(current_timestamp ...

  2. 数仓增量更新hive实现

    注:参考文末文章,加上自己的理解. 1.增量更新 有一个 base_table 表存放的是 12 月 15 日之前的所有数据,当 12 月 16 日的数据产生后,生成了一个 incremental_t ...

  3. 谈谈混合 App Web 资源的打包与增量更新

    综述 移动 App 的运行环境具有带宽不稳定,流量收费,启动速度比较重要等特点,所以混合 App 如何加载 Web 资源并不是一个新问题.本文目的是总结出一种资源打包下载的思路和方案,并且提供一种打包 ...

  4. SSIS Design2:增量更新

    一般来说,ETL实现增量更新的方式有两种,第一种:记录字段的最大值,如果数据源中存在持续增加的数据列,记录上次处理的数据集中,该列的最大值:第二种是,保存HashValue,快速检查所有数据,发现异动 ...

  5. android studio增量更新

    一.概述 1.1 概念 增量更新即是通过比较 本机安装版本 和 想要安装版本 间的差异,产生一个差异安装包,不需要从官网下载并安装全量安装包,更不需要将本机已安装的版本下载,而仅仅只是安装此差异安装包 ...

  6. Android 增量更新(BSDiff / bspatch)

    Android 增量更新 BSDiff / bspatchhttp://www.daemonology.net/bsdiff/android的代码目录下 \external\bsdiff bsdiff ...

  7. 【转载】Unity 合理安排增量更新(热更新)

    原帖地址:由于我看到的那个网站发的这篇帖子很大可能是盗贴的,我就暂时不贴地址了.避免伤害原作者 原版写的有点乱,我个人修改整理了下. --------------------------------- ...

  8. Unity5 如何做资源管理和增量更新

    工具 Unity 中的资源来源有三个途径:一个是Unity自动打包资源,一个是Resources,一个是AssetBundle. Unity自动打包资源是指在Unity场景中直接使用到的资源会随着场景 ...

  9. [转载]BW增量更新的理解(时间戳)

    在BW中,存在两种数据抽取方式,完全更新与增量更新,完全更新是每次把截至到某个时间的数据全部抽取,增量抽取则只抽取上次和本次抽取之间更新的数据,很显然,增量抽取能够提高系统效率,根据SAP帮 助的说法 ...

随机推荐

  1. 在 .NET Core Logging中使用 Trace和TraceSource

    本文介绍了在.NET Core中如何在组件设计中使用Trace和TraceSource. 在以下方面会提供一些帮助: 1.你已经为.NET Framework和.NET Core / .NET Sta ...

  2. DEDECMS:更改dede提示框标题

    在include文件夹下,修改common.func.php文件里的 <title>DedeCMS提示信息</title> 修改成自己想要的标题. 将 <b>Ded ...

  3. 三维CAD——基于B_rep的建模操作

    内容来自高老师的<三维CAD建模>课,本文就主要介绍半边结构和欧拉操作以及代码实现. 1. 边界表示法及其数据结构 · 拓扑结构 a.拓扑元素:面.边.点.体 b.拓扑关系:9种.V{V} ...

  4. hdu2196 Compute

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

  5. MySql 执行 DELETE/UPDATE时,报 Error Code: 1175错误

    MySql 执行 DELETE FROM Table 时,报 Error Code: 1175. You are using safe update mode and you tried to upd ...

  6. LEETCODE - 160【相交链表】

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  7. 一篇文章图文并茂地带你轻松学会 HTML5 storage

    html5 storage api localStorage 和 sessionStorage 是 html5 新增的用来存储数据的对象,他们让我们可以以键值对的形式存储信息. 为什么要有 stora ...

  8. PAT L2-016. 愿天下有情人都是失散多年的兄妹 (BFS)

    L2-016. 愿天下有情人都是失散多年的兄妹 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 呵呵.大家都知道五服以内不得通婚 ...

  9. Adaptive Threshold

    Adaptive Threshold 1. Otsu's Binarization: Using a discriminant analysis to partition the image into ...

  10. CSS 检测 IE 浏览器

    CSS 检测 IE 浏览器 <!--[if IE]> <link href="ie.css" rel="stylesheet"> < ...