使用hive增量更新
参考文末文章,加上自己的理解。
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
使用hive增量更新的更多相关文章
- hive不分区增量更新
insert overwrite table ods.zeg_so select *,case when zsm.id is not null then cast(current_timestamp ...
- 数仓增量更新hive实现
注:参考文末文章,加上自己的理解. 1.增量更新 有一个 base_table 表存放的是 12 月 15 日之前的所有数据,当 12 月 16 日的数据产生后,生成了一个 incremental_t ...
- 谈谈混合 App Web 资源的打包与增量更新
综述 移动 App 的运行环境具有带宽不稳定,流量收费,启动速度比较重要等特点,所以混合 App 如何加载 Web 资源并不是一个新问题.本文目的是总结出一种资源打包下载的思路和方案,并且提供一种打包 ...
- SSIS Design2:增量更新
一般来说,ETL实现增量更新的方式有两种,第一种:记录字段的最大值,如果数据源中存在持续增加的数据列,记录上次处理的数据集中,该列的最大值:第二种是,保存HashValue,快速检查所有数据,发现异动 ...
- android studio增量更新
一.概述 1.1 概念 增量更新即是通过比较 本机安装版本 和 想要安装版本 间的差异,产生一个差异安装包,不需要从官网下载并安装全量安装包,更不需要将本机已安装的版本下载,而仅仅只是安装此差异安装包 ...
- Android 增量更新(BSDiff / bspatch)
Android 增量更新 BSDiff / bspatchhttp://www.daemonology.net/bsdiff/android的代码目录下 \external\bsdiff bsdiff ...
- 【转载】Unity 合理安排增量更新(热更新)
原帖地址:由于我看到的那个网站发的这篇帖子很大可能是盗贴的,我就暂时不贴地址了.避免伤害原作者 原版写的有点乱,我个人修改整理了下. --------------------------------- ...
- Unity5 如何做资源管理和增量更新
工具 Unity 中的资源来源有三个途径:一个是Unity自动打包资源,一个是Resources,一个是AssetBundle. Unity自动打包资源是指在Unity场景中直接使用到的资源会随着场景 ...
- [转载]BW增量更新的理解(时间戳)
在BW中,存在两种数据抽取方式,完全更新与增量更新,完全更新是每次把截至到某个时间的数据全部抽取,增量抽取则只抽取上次和本次抽取之间更新的数据,很显然,增量抽取能够提高系统效率,根据SAP帮 助的说法 ...
随机推荐
- /etc/fstab和/etc/mtab的区别
etc/fstab文件的作用 记录了计算机上硬盘分区的相关信息,启动 Linux 的时候,检查分区的 fsck 命令,和挂载分区的 mount 命令,都需要 fstab 中的信息,来正 ...
- python3 自动部署MariaDB主从复制
master import configparser import os def config_mariadb_yum(): exists = os.path.exists('/etc/yum.rep ...
- 小白搭建WNMP详细教程---PHP安装与设置
php的安装请参考WAMP中PHP的安装教程https://www.cnblogs.com/missbye/p/12049925.html 需要注意的是,我们下载的PHP版本要下载Non Thread ...
- Flink-v1.12官方网站翻译-P028-Custom Serialization for Managed State
管理状态的自定义序列化 本页面的目标是为需要使用自定义状态序列化的用户提供指导,涵盖了如何提供自定义状态序列化器,以及实现允许状态模式演化的序列化器的指南和最佳实践. 如果你只是简单地使用Flink自 ...
- python中numpy库的一些使用
想不用第三方库实现点深度学习的基础部分,发现numpy真的好难(笑),在此做点遇到的函数的笔记 惯例官方文档:https://docs.scipy.org/doc/numpy-1.16.1/refer ...
- 从微信小程序到鸿蒙js开发【04】——list组件
目录: 1.可滚动区域 2.list + list-item 3.list + list-item-group + list-item 1.可滚动区域 在许多场景中,页面会有一块区域是可滚动的,比如这 ...
- 一周精彩内容分享(第 1 期):"世纪逼空大战"
这里记录过去一周,我看到的值得分享的东西. 一方面是整理记录一下自己一周的学习,另一方面也是期待自己有更多的输出,有更多的价值. 周刊开源(Github:wmyskxz/weekly),欢迎提交 is ...
- CF - 392 C. Yet Another Number Sequence (矩阵快速幂)
CF - 392 C. Yet Another Number Sequence 题目传送门 这个题看了十几分钟直接看题解了,然后恍然大悟,发现纸笔难于描述于是乎用Tex把初始矩阵以及转移矩阵都敲了出来 ...
- 2019 Multi-University Training Contest 7 Kejin Player(期望)
题意:给定在当前等级升级所需要的花费 每次升级可能会失败并且掉级 然后q次询问从l到r级花费的期望 思路:对于单次升级的期望 我们可以列出方程: 所以我们可以统计一下前缀和 每次询问O1回答 #inc ...
- HDU5407 CRB and Candies 【LCM递推】
HDU5407 CRB and Candies 题意: 计算\(LCM(C(n,0),C(n,1),C(n,2),\cdots,C(n,n-1),C(n,n))\) \(n\le 10^6\) 题解: ...