使用postgres_fdw 串接elasticsearch fdw


目的很簡單,主要還是elasticsearch fdw 的鏡像太大,為了減少all-in-one 的大小,實現一個層級的數據模型

環境准備

  • 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:11
    ports:
      - "5432:5432"
    environment:
      - "POSTGRES_PASSWORD=dalong"
  pg:
    image: dalongrong/pgspider:pg_cron
    ports: 
    - "5433:5432"
    environment:
      - "POSTGRES_PASSWORD=dalong"
  • 啟動服務
docker-compose up -d

集成elasticsearch fdw 以及postgres_fdw

  • 說明
    postgres-fdw 服務為pg 11 版本的es 擴展,主要為了集成es,pg 服務為包含了postgres_fdw pg_cron,擴展以及pgspider
    pg 服務通過postgres_fdw 集成包含了elasticsearch fdw 擴展的服務
  • 配置elasticsearch fdw 擴展
 
// 創建擴展
CREATE EXTENSION multicorn;
// 創建server
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'
    );

效果

 

 

  • pg 服務 集成elasticsearch fdw
    通過postgres_fdw 集成elasticsearch fdw
 
// 創建擴展
create extension postgres_fdw;
// 創建server
CREATE SERVER pg_server
  FOREIGN DATA WRAPPER postgres_fdw
  OPTIONS (host 'psotgres-fdw', dbname 'postgres');
// 創建用戶映射
CREATE USER MAPPING FOR postgres
  SERVER pg_server
  OPTIONS (user 'postgres', password 'dalong');
// 創建schema
CREATE SCHEMA app;
// 導入schema
IMPORT FOREIGN SCHEMA public
  FROM SERVER pg_server
  INTO app;
// 數據查詢
select * from app.articles_es;

效果

 

 

說明

通過各種fdw 的集成我們可以靈活的進行數據的聚合處理,同時結合 pg_cron可以做一些簡單的數據任務處理

參考資料

https://github.com/matthewfranglen/postgres-elasticsearch-fdw
https://github.com/rongfengliang/es-fdw-learning


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM