postgres elasticsearch fdw 学习

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

猜你喜欢

转载自www.cnblogs.com/rongfengliang/p/12358206.html