批量日志数据库外表写入

本文介绍了一种利用SQL脚本在HDFS中创建和管理外部表的方法,并通过示例展示了如何进行数据的批量迁移及表的清理工作。重点讲解了创建外部表、数据插入过程以及对历史表的维护策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#创建外链表映射日志文件

audience_attributes_path = "gphdfs://xxx/audience_attributes/#{batch_id}"

create_sql = <<-eos
create writable external table #{audience_attributes_table} (
opxpid varchar(60),
hash_key varchar(50),
hash_value text
)

location ('#{audience_attributes_path}')
format 'text';
eos

AudienceStructure.connection.execute(create_sql)

#表数据插入

insert_sql = <<-sql
insert into #{audience_attributes_table}
select opxpid, hash_key, array_to_string(jx_array_sort(madlib.array_agg(audience_id)), ',')
from (
select opxpid, hash_key, hash_value audience_id
from #{master_table}
where hash_key = 'aids'
and hash_value in (#{aids.uniq.join(",")})
) m
group by 1, 2;
sql

AudienceStructure.connection.execute(insert_sql)


#保留12个表超过12的删除。

drop_tables("select 'drop external table workspace.' || relname || ';' cmd from pg_class where relname like 'audience_segments_hdfs_%' order by 1")

def drop_tables(sql, col_name="cmd")
drop_sqls = AudienceStructure.connection.select_all(sql)
# only keep latest 12 tables
if drop_sqls.size > 12
drop_sqls[0..drop_sqls.size-13].each do |drop_sql|
AudienceStructure.connection.execute(drop_sql[col_name])
end
end
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值