前边已经构建好了,关于sqlite,mysql,以及默认的pg 的扩展,以下是一个简单的学习试用
环境准备
为了简单目前只测试关于pg的,后边会学习关于mysql的
- docker-compose 文件
version: "3"
services:
pg-core:
image: dalongrong/pgspider:core
ports:
- "5432:5432"
environment:
- "POSTGRES_PASSWORD:dalong"
pg:
image: postgres:12.1-alpine
ports:
- "5433:5432"
environment:
- "POSTGRES_PASSWORD:dalong"
pg-mysql:
image: dalongrong/pgspider:mysql
ports:
- "5434:5432"
environment:
- "POSTGRES_PASSWORD:dalong"
mysql:
image: mysql:5.7.16
ports:
- 3306:3306
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
environment:
MYSQL_ROOT_PASSWORD: dalongrong
MYSQL_DATABASE: demo
MYSQL_USER: demo
MYSQL_PASSWORD: dalongrong
TZ: Asia/Shanghai
pg-sqlite:
image: dalongrong/pgspider:sqlite
ports:
- "5435:5432"
environment:
- "POSTGRES_PASSWORD:dalong"
使用
- 启动docker-compose 服务
docker-compose up -d
- 创建扩展
CREATE EXTENSION pgspider_core_fdw;
CREATE EXTENSION postgres_fdw;
CREATE EXTENSION pgspider_fdw;
- 创建parent 以及child server
// parent 为当前操作的pg server
CREATE SERVER parent FOREIGN DATA WRAPPER pgspider_core_fdw OPTIONS (host '127.0.0.1', port '5432');
// child 为另外一个pg server
CREATE SERVER postgres_svr FOREIGN DATA WRAPPER postgres_fdw OPTIONS(host 'pg', port '5432', dbname 'postgres');
- 创建user 映射
CREATE USER MAPPING FOR CURRENT_USER SERVER parent OPTIONS(user 'postgres', password 'dalong');
CREATE USER MAPPING FOR CURRENT_USER SERVER postgres_svr OPTIONS(user 'postgres', password 'dalong');
- 创建parent 表映射
// __spd_url 是必须在parent 中定义的表字段
CREATE FOREIGN TABLE t1(i int, t text, __spd_url text) SERVER parent;
- 创建child 表映射
// 当暴露为多租户级的表,需要使用格式为`[Multi-Tenant table name]__[data source name]__0`
CREATE FOREIGN TABLE t1__postgres_svr__0(i int, t text) SERVER postgres_svr OPTIONS (table_name 't1');
- 创建remote pg server 表t1&&
CREATE TABLE t1 (
i SERIAL PRIMARY KEY,
t text
);
INSERT INTO "public"."t1"("i","t")
VALUES
(1,E'demo');
- 查询数据
select * from t1;
效果
- 基于node 节点过滤
SELECT * FROM t1 IN ('/postgres_svr/');
效果
说明
以上是一个简单的使用,目前pgspider 还提供了influxdb 以及GridDB 的fdw 扩展,其他的扩展应该也是可以集成的
参考资料
https://github.com/pgspider/pgspider
https://hub.docker.com/repository/docker/dalongrong/pgspider
https://github.com/rongfengliang/pgspider-docker