订单数据之类的业务表,因为有状态要更新,比如订单状态,物流状态之类的,需要同步很久之前的数据到Hive. 如何同步时在Hive中进行操作一次更新多个分区内的数据?
SET hive.exec.dynamic.partition=true;
SET hive.exec.dynamic.partition.mode=nonstrict;
源表:
CREATE TABLE `ods_binlog_person`(
`binlog_id` bigint,
`binglog_es` bigint,
`binlog_ts` bigint,
`binlog_type` string,
`id` bigint,
`name` string,
`score` int,
`created_at` string,
`updated` string)
PARTITIONED BY (`dt` string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
加载数据:
load data inpath '/camus/exec/binlog/person/pt_hour=2022072400' into table ods_binlog_person partition (dt='2022072400')
目标表
CREATE TABLE IF NOT EXISTS temp_partition_table(
id string comment "字段id",
name string comment "字段注释",
`score` int,
`created_at` string,
`updated` string
) COMMENT "分区表"
PARTITIONED BY(`dt` string)
STORED AS ORC
TBLPROPERTIES("orc.compress"="SNAPPY");
insert overwrite table temp_partition_table partition(dt)
select
id,
name,
score,
created_at,
updated,
from_unixtime(unix_timestamp(created_at,'yyyy-MM-dd HH:mm:ss'), 'yyyyMMdd')
from ods_binlog_person where dt = 2022072400;
show partitions temp_partition_table;
OK
dt=20220717
dt=20220720
Time taken: 0.175 seconds, Fetched: 2 row(s)
show create table temp_partition_table;
或者
desc temp_partition_table
alter table ods_binlog_person drop partition(dt=2022072400)
通过Hive动态分区, 我们就实现基于源表的业务时间生成目标表的分区, 并且将数据加载到对应分区中. 然后删除源表对应分区的数据,避免数据冗余节省空间.
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有