共计 3582 个字符,预计需要花费 9 分钟才能阅读完成。
PostgreSQL 中怎么实现海量数据无限空间存储,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
使用 s3_fdw 插件打通云数据库 PostgreSQL 与 OSS 之间的数据流转,无需复杂的程序操作,用户仅需要几行代码,即可轻松实现。下面我们就来展示在 PostgreSQL 中使用 s3_fdw 读写 OSS 外部表的操作过程:
# 准备数据
postgres=# CREATE TABLE local_table(id integer, name character varying, password character varying);
CREATE TABLE
postgres=# insert into local_table select generate_series(1,40000000), md5(random()::text), md5(random()::text);
INSERT 0 40000000
# 创建插件
postgres=# create extension s3_fdw;
CREATE EXTENSION
postgres=# CREATE SERVER s3_fdw_server FOREIGN DATA WRAPPER s3_fdw options(host s3-internal.cn-north-1.jdcloud-oss.com , bucket postgresql
CREATE SERVER
postgres=# CREATE USER MAPPING FOR CURRENT_USER SERVER s3_fdw_server OPTIONS (access_key_id xxxxxx , secret_access_key xxxxxx
CREATE USER MAPPING
# 创建外部表
postgres=# CREATE FOREIGN TABLE local_table_oss_1_10000000(id integer, name character varying, password character varying) SERVER s3_fdw_server OPTIONS(dir s3_fdw_local_table/1_10000000/ , format csv
CREATE FOREIGN TABLE
postgres=# CREATE FOREIGN TABLE local_table_oss_10000001_20000000(id integer, name character varying, password character varying) SERVER s3_fdw_server OPTIONS(dir s3_fdw_local_table/10000001_20000000/ , format csv
CREATE FOREIGN TABLE
postgres=# CREATE FOREIGN TABLE local_table_oss_20000001_30000000(id integer, name character varying, password character varying) SERVER s3_fdw_server OPTIONS(dir s3_fdw_local_table/20000001_30000000/ , format csv
CREATE FOREIGN TABLE
postgres=# CREATE FOREIGN TABLE local_table_oss_30000001_40000000(id integer, name character varying, password character varying) SERVER s3_fdw_server OPTIONS(dir s3_fdw_local_table/30000001_40000000/ , format csv
CREATE FOREIGN TABLE
# 开启 4 个客户端,并行地将云 PostgreSQL 实例的数据导入 OSS
postgres=# insert into local_table_oss_1_10000000 select * from local_table where id = 10000000;
postgres=# insert into local_table_oss_10000001_20000000 select * from local_table where id = 10000001 and id = 20000000;
postgres=# insert into local_table_oss_20000001_30000000 select * from local_table where id = 20000001 and id = 30000000;
postgres=# insert into local_table_oss_30000001_40000000 select * from local_table where id = 30000001 and id = 40000000;
# 直接读取 OSS 数据源
postgres=# select * from local_table_oss_1_10000000 limit 5;
id | name | password
----+----------------------------------+----------------------------------
1 | b6c4d3e0efff1ff051ed7989ade43287 | 2859055a8d51b2f8888993887340fe7d
2 | f718eb7452c59bae2ee06dd88eae1488 | bd21f8468c8d2f5d7bb756a55203d204
3 | 95735e3472903502f0a08dc895220ff9 | d06704faad7a247d29d6257af85d2906
4 | 3b690bb8912fe96567d04287fc9fa701 | d04ad214f9dd1d22cb680b72a0a9d0bb
5 | 849842e47625c271de0221adc55608a6 | 8e961dcffcef418200f623c1b5f34d7f
(5 rows)
# 本地创建新表
postgres=# create table local_table_2 (like local_table);
CREATE TABLE
postgres=# select * from local_table_2;
id | name | password
----+------+----------
(0 rows)
# 开启 4 个客户端,并行地将数据从 OSS 上 load 到云 PostgreSQL 实例
postgres=# insert into local_table_2 select * from local_table_oss_1_10000000;
postgres=# insert into local_table_2 select * from local_table_oss_10000001_20000000;
postgres=# insert into local_table_2 select * from local_table_oss_20000001_30000000;
postgres=# insert into local_table_2 select * from local_table_oss_30000001_40000000;
# 数据一致性校验
postgres=# select sum(hashtext(t.*::text)) from local_table t;
sum
----------------
10851381716282
(1 row)
postgres=# select sum(hashtext(t.*::text)) from local_table_2 t;
sum
----------------
10851381716282
(1 row)
关于 PostgreSQL 中怎么实现海量数据无限空间存储问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注丸趣 TV 行业资讯频道了解更多相关知识。
正文完