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

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. 初入Java坑,然后又入产品坑

    之前工作了一年,从事Java相关工作,不小心深得领导器重,跑去演讲.写文档.与客户沟通等,最后应公司需要,转往产品坑,坑坑相连,何时逃坑. 最近一直在学习产品经理必备工具Axure,发现这真是一个神奇 ...

  2. 算法-迪杰斯特拉算法(dijkstra)-最短路径

    迪杰斯特拉算法(dijkstra)-最短路径 简介: 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中 ...

  3. 写了这么多年 JavaScript ,竟然还不知道这些技巧?

    不少人有五年的 JavaScript 经验,但实际上可能只是一年的经验重复用了五次而已.完成同样的逻辑和功能,有人可以写出意大利面条一样的代码,也有人两三行简洁清晰的代码就搞定了.简洁的代码不但方便阅 ...

  4. 在VirtualBox上安装Ubuntu-20.04

    本文主要介绍如何在VirtualBox上安装Ubuntu-20.04 目录 下载VirtualBox 下载Ubuntu-20.04镜像 新建虚拟机 第一步:打开VirtualBox 第二步:设置虚拟机 ...

  5. easyx学习心得

    前几天算法课的实验要求实现可视化,搞了半天没动咋实现,然后有大佬说用easyx,,,我寻思着也没教这玩意咋用啊.然后很烦躁的上网找教程,发现没有教怎么使用的,都说有一本说明书(链接),自己调用函数就可 ...

  6. CF-gym/101810 J、T-Shirts Dilemma

    题目链接:点我 题意: 给你一个区间[a,b],让你从里面选一个连续子区间[x,y](子区间可以为[a,b]),把这个区间的所有数或起来x|x+1|x+2|...|y 你要使得区间[x,y]异或起来的 ...

  7. A. Little Pony and Expected Maximum

    Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she ...

  8. 使用eclipse搭建第一个java web应用

    一. 首先是eclipse得下载,你要下载Eclipse IDE for Java EE这种类型的,我之前下载的Eclipse IDE for Enterprise Java Developers是官 ...

  9. hdu5402 Travelling Salesman Problem

    Problem Description Teacher Mai is in a maze with n rows and m columns. There is a non-negative numb ...

  10. Incorrect string value: '\xF0\x9F\x93\xB7</...' for column 'content' at row 1

    出现原因:当insert数据中有表情时发生.而这些表情是按照4个字节一个单位进行编码的,而我们使用的utf-8编码在mysql数据库中默认是按照3个字节一个单位进行编码的. 解决方法:将表字段字符集设 ...