前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hive一次更新多个分区数据方案

Hive一次更新多个分区数据方案

作者头像
awwewwbbb
发布2022-09-16 12:29:34
7820
发布2022-09-16 12:29:34
举报

场景

订单数据之类的业务表,因为有状态要更新,比如订单状态,物流状态之类的,需要同步很久之前的数据到Hive. 如何同步时在Hive中进行操作一次更新多个分区内的数据?

Hive 操作

  1. 设置Hive动态分区
SET hive.exec.dynamic.partition=true;
SET hive.exec.dynamic.partition.mode=nonstrict;
  1. 创建分区表:

源表:

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");
  1. 插入分区数据
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;
  1. 查看分区
show partitions temp_partition_table;


OK
dt=20220717
dt=20220720
Time taken: 0.175 seconds, Fetched: 2 row(s)
  1. 查看表信息
show create table temp_partition_table;

或者

desc temp_partition_table
  1. 加载到目标表后, 可以删除源表中的分区数据,避免数据冗余
alter table ods_binlog_person  drop partition(dt=2022072400)

结论

通过Hive动态分区, 我们就实现基于源表的业务时间生成目标表的分区, 并且将数据加载到对应分区中. 然后删除源表对应分区的数据,避免数据冗余节省空间.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-07-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 场景
  • Hive 操作
  • 结论
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档