es 的fdw 是基於multicorn(python擴展)開發的,官方的multicorn 版本已經很老了,對於pg 的高版本是不支持的
但是已經有人提供了支持新版本的包了,還是很不錯的,基於python 開發fdw 簡單方便
multicorn 支持的版本
apt-get install postgresql-9.4-python-multicorn
apt-get install postgresql-9.5-python-multicorn
apt-get install postgresql-9.6-python-multicorn
apt-get install postgresql-10-python-multicorn
apt-get install postgresql-11-python-multicor
es fdw docker 鏡像
- Dockerfile
9.5 版本的,es 為7 (默認使用es python 的最新版本)
FROM postgres:9.5
RUN apt-get update && apt-get -y install \
postgresql-9.5-python-multicorn \
python-pip \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
RUN pip install pg_es_fdw
環境准備
- docker-compose 文件
version: "3"
services:
elasticsearch:
image: elasticsearch:7.6.0
environment:
- "discovery.type=single-node"
- "http.host=0.0.0.0"
- "cluster.name=odfe-cluster"
- "transport.host=0.0.0.0"
- "network.host=0.0.0.0"
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ports:
- 9200:9200
psotgres-fdw:
image: dalongrong/pg-es-fdw:9.5
ports:
- "5432:5432"
environment:
- "POSTGRES_PASSWORD=dalong"
啟動&&使用
- 啟動
docker-compose up -d
- 使用
// 創建擴展
CREATE EXTENSION multicorn;
// 創建
CREATE SERVER multicorn_es FOREIGN DATA WRAPPER multicorn
OPTIONS (
wrapper 'pg_es_fdw.ElasticsearchFDW'
);
// 創建外部表映射
CREATE FOREIGN TABLE articles_es
(
id BIGINT,
title TEXT,
body TEXT,
query TEXT,
score NUMERIC
)
SERVER multicorn_es
OPTIONS
(
host 'elasticsearch',
port '9200',
index 'article-index',
type 'article',
rowid_column 'id',
query_column 'query',
score_column 'score',
timeout '20'
)
;
// insert 數據
INSERT INTO articles_es
(
id,
title,
body
)
VALUES
(
1,
'foo',
'spike'
);
- 效果
說明
dockerhub 中我已經push 了9.5 以及11的版本,可以體驗試用,為dalongrong/pg-es-fdw:9.5
, dalongrong/pg-es-fdw:11
參考資料
https://github.com/matthewfranglen/postgres-elasticsearch-fdw
https://hub.docker.com/repository/docker/dalongrong/pg-es-fdw
https://github.com/rongfengliang/es-fdw-learning