1. 概述
PostgreSQL 中的 FDW(Foreign Data Wrapper) 是一种非常重要的扩展机制,用来访问外部数据源,就像访问本地表一样。它是 SQL/MED 标准(SQL Management of External Data)在 PostgreSQL 中的实现。
- FDW 的核心概念
FDW 允许 PostgreSQL:
- 访问非 PostgreSQL 数据库
- 访问其它 PostgreSQL 节点
- 访问文件系统、REST API、Kafka、Redis 等数据源
- 无需 ETL 或同步,也不需要复制数据
访问方式表现为 SQL 读写外部表,就像本地表一样。
- FDW 的架构原理(简化)
- 定义 FDW(扩展)
- 创建服务器对象(server)
- 定义用户认证(user mapping)
- 导入或创建外部表(foreign table)
当你查询外部表时:
- PostgreSQL 调用 FDW 驱动
- 通过该驱动访问远程数据源
- 读取结果返回本地执行计划
2. 插件下载地址
oracle_fdw 插件的下载地址为:
github-oracle_fdw
Oracle Instant Client 的下载地址:
instant-client
3. 实验环境
前提已安装好瀚高数据库,并服务能正常运行。
| 主机名 | 操作系统 | 瀚高数据库版本 | oracle_fdw 版本 | Oracle Instant Client 版本 |
| Kylin | KylinV10 | hgdb-enterprise-9.1.1 | 2.8.0 | 19.29 |
4. 相关安装包
- instantclient-basic-linux.x64-19.29.0.0.0dbru.zip
- instantclient-sdk-linux.x64-19.29.0.0.0dbru.zip
- instantclient-sqlplus-linux.x64-19.29.0.0.0dbru.zip
- oracle_fdw-ORACLE_FDW_2_8_0.tar.gz
5. 解压安装包
新建目录
[highgo@kylin highgo]$ mkdir –p /highgo/oracle
将文件上传到/highgo/oracle 目录下,并进入此目录进行解压 instantclient 相关文件。
[highgo@kylin oracle]$ ll
-rw-r--r-- 1 root root 77431995 12月 8 12:15 instantclient-basic-linux.x64-19.29.0.0.0dbru.zip
-rw-r--r-- 1 root root 951583 12月 8 12:14 instantclient-sdk-linux.x64-19.29.0.0.0dbru.zip
-rw-r--r-- 1 root root 919877 12月 8 12:14 instantclient-sqlplus-linux.x64-19.29.0.0.0dbru.zip
[highgo@kylin oracle]$ unzip instantclient-basic-linux.x64-19.29.0.0.0dbru.zip
[highgo@kylin oracle]$ unzip instantclient-sdk-linux.x64-19.29.0.0.0dbru.zip
[highgo@kylin oracle]$ unzip instantclient-sqlplus-linux.x64-19.29.0.0.0dbru.zip
注意:按照顺序进行解压,解压过程会出现提示replace META-INF/MANIFEST.MF? [y]es, [n]o, [A]ll, [N]one, [r]ename: A,选择A即可。
6. 配置 instantclient 环境变量
[highgo@kylin oracle]$ vim ~/.bashrc
# 增加以下内容
export ORACLE_HOME=/highgo/oracle/instantclient_19_29
export OCI_INC_DIR=$ORACLE_HOME/sdk/include
export LD_LIBRARY_PATH=/usr/lib64:/usr/local/lib:/highgo/hgdb-v9.0/lib:$ORACLE_HOME:$LD_LIBRARY_PATH
[highgo@kylin oracle]$ source ~/.bashrc
环境变量配置完成后,需要使用 source ~/.bashrc 来刷新环境变量才会生效。
注意:系统lib路径要在最前面,否则make oracle_fdw时会失败,提示如下信息:
/usr/bin/mkdir: /highgo/hgdb-v9.0/lib/libselinux.so.1: no version information available
/usr/bin/install: /highgo/hgdb-v9.0/lib/libselinux.so.1: no version information available
7. 安装 oracle_fdw
oracle_fdw 使用以下步骤安装:
- 解压 oracle_fdw 包
- 进入到 oracle_fdw 解压后的目录,执行 make
- 编译正常后,再执行 make install
[highgo@kylin ora_fdw]$ tar zxvf oracle_fdw-ORACLE_FDW_2_8_0.tar.gz
[highgo@kylin ora_fdw]$ cd oracle_fdw-ORACLE_FDW_2_8_0/
[highgo@kylin oracle_fdw-ORACLE_FDW_2_8_0]$ make
mkdir: /highgo/hgdb-v9.0/lib/libselinux.so.1: no version information available (required by mkdir)
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-
truncation -O2 -fPIC -I"/highgo/oracle/instantclient_19_29/sdk/include" -I"/highgo/oracle/instantclient_19_29/oci/include" -I"/highgo/oracle/instantclient_19_29/rdbms/public" -I"/highgo/oracle/instantclient_19_29/" -I. -I./ -I/highgo/hgdb-v9.0/include/postgresql/serv
er -I/highgo/hgdb-v9.0/include/postgresql/internal -I/usr/local/include -D_GNU_SOURCE -I/usr/include/libxml2 -I/home/jenkins/workspace/iflow_build/iflow-bundle/3361/ivorysql-pro/dep/license/x86_64/include -c -o oracle_fdw.o oracle_fdw.c -MMD -MP -MF .deps/oracle_f
dw.Po
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-
truncation -O2 -fPIC -I"/highgo/oracle/instantclient_19_29/sdk/include" -I"/highgo/oracle/instantclient_19_29/oci/include" -I"/highgo/oracle/instantclient_19_29/rdbms/public" -I"/highgo/oracle/instantclient_19_29/" -I. -I./ -I/highgo/hgdb-v9.0/include/postgresql/serv
er -I/highgo/hgdb-v9.0/include/postgresql/internal -I/usr/local/include -D_GNU_SOURCE -I/usr/include/libxml2 -I/home/jenkins/workspace/iflow_build/iflow-bundle/3361/ivorysql-pro/dep/license/x86_64/include -c -o oracle_utils.o oracle_utils.c -MMD -MP -MF .deps/orac
le_utils.Po
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-
truncation -O2 -fPIC -I"/highgo/oracle/instantclient_19_29/sdk/include" -I"/highgo/oracle/instantclient_19_29/oci/include" -I"/highgo/oracle/instantclient_19_29/rdbms/public" -I"/highgo/oracle/instantclient_19_29/" -I. -I./ -I/highgo/hgdb-v9.0/include/postgresql/serv
er -I/highgo/hgdb-v9.0/include/postgresql/internal -I/usr/local/include -D_GNU_SOURCE -I/usr/include/libxml2 -I/home/jenkins/workspace/iflow_build/iflow-bundle/3361/ivorysql-pro/dep/license/x86_64/include -c -o oracle_gis.o oracle_gis.c -MMD -MP -MF .deps/oracle_g
is.Po
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-
truncation -O2 -fPIC -shared -o oracle_fdw.so oracle_fdw.o oracle_utils.o oracle_gis.o -L/highgo/hgdb-v9.0/lib -Wl,-rpath,'$ORIGIN/../lib',--disable-new-dtags -L/home/jenkins/workspace/iflow_build/iflow-bundle/3361/ivorysql-pro/dep/license/x86_64/lib -Wl,--as-needed
-L"/highgo/oracle/instantclient_19_29/" -L"/highgo/oracle/instantclient_19_29/bin" -L"/highgo/oracle/instantclient_19_29/lib" -L"/highgo/oracle/instantclient_19_29/lib/amd64" -lclntsh
[highgo@kylin oracle_fdw-ORACLE_FDW_2_8_0]$ make install
/usr/bin/mkdir -p '/highgo/hgdb-v9.0/lib/postgresql'
/usr/bin/mkdir -p '/highgo/hgdb-v9.0/share/postgresql/extension'
/usr/bin/mkdir -p '/highgo/hgdb-v9.0/share/postgresql/extension'
/usr/bin/mkdir -p '/highgo/hgdb-v9.0/share/doc/postgresql/extension'
/usr/bin/install -c -m 755 oracle_fdw.so '/highgo/hgdb-v9.0/lib/postgresql/oracle_fdw.so'
/usr/bin/install -c -m 644 .//oracle_fdw.control '/highgo/hgdb-v9.0/share/postgresql/extension/'
/usr/bin/install -c -m 644 .//oracle_fdw--1.2.sql .//oracle_fdw--1.0--1.1.sql .//oracle_fdw--1.1--1.2.sql '/highgo/hgdb-v9.0/share/postgresql/extension/'
/usr/bin/install -c -m 644 .//README.oracle_fdw '/highgo/hgdb-v9.0/share/doc/postgresql/extension/'
8. 外部表创建
安装完 oracle_fdw 插件后,现在可以登录highgo用户,进入 psql 命令行窗口创建外部服务器的数据封装器,创建映射,创建外部表。
--创建 foreign server
create server oradb foreign data wrapper oracle_fdw options(dbserver '//192.192.103.138:1521/orcl');
--创建用户映射
create user mapping for postgres server oradb options(user 'hr',password 'hr');
--创建外部表
create foreign table test_jobs(job_id varchar(10) not null,job_title varchar(35) not null,min_salary numeric(6),max_salary numeric(6)) server oradb options(table 'JOBS');
查询示例:
testdb=# \conninfo
以 用 户 "test" 的 身 份 , 通 过 套 接 字 "/tmp"在 端 口 "5866"连 接 到 数 据 库 "testdb"
testdb=# select * from pg_foreign_data_wrapper ;
oid | fdwname | fdwowner | fdwhandler | fdwvalidator | fdwacl | fdwoptions
-------+------------+----------+------------+--------------+--------+------------
16459 | oracle_fdw | 10 | 16454 | 16455 | |
(1 行 记 录 )
testdb=# select * from pg_foreign_server;
oid | srvname | srvowner | srvfdw | srvtype | srvversion | srvacl | srvoptions
-------+---------+----------+--------+---------+------------+--------+----------------------------------------
16463 | oradb | 10 | 16459 | | | | {dbserver=//192.192.103.138:1521/orcl}
(1 行 记 录 )
testdb=# select * from pg_foreign_table;
ftrelid | ftserver | ftoptions
---------+----------+-------------
16465 | 16463 | {table=JOBS}
(1 行 记 录 )
testdb=# select * from test_jobs;
job_id | job_title | min_salary | max_salary
------------+---------------------------------+------------+------------
AD_PRES | President | 20080 | 40000
AD_VP | Administration Vice President | 15000 | 30000
AD_ASST | Administration Assistant | 3000 | 6000
FI_MGR | Finance Manager | 8200 | 16000
FI_ACCOUNT | Accountant | 4200 | 9000
AC_MGR | Accounting Manager | 8200 | 16000
AC_ACCOUNT | Public Accountant | 4200 | 9000
SA_MAN | Sales Manager | 10000 | 20080
SA_REP | Sales Representative | 6000 | 12008
PU_MAN | Purchasing Manager | 8000 | 15000
PU_CLERK | Purchasing Clerk | 2500 | 5500
ST_MAN | Stock Manager | 5500 | 8500
ST_CLERK | Stock Clerk | 2008 | 5000
SH_CLERK | Shipping Clerk | 2500 | 5500
IT_PROG | Programmer | 4000 | 10000
MK_MAN | Marketing Manager | 9000 | 15000
MK_REP | Marketing Representative | 4000 | 9000
HR_REP | Human Resources Representative | 4000 | 9000
PR_REP | Public Relations Representative | 4500 | 10500
(19 行 记 录 )
testdb=#
957

被折叠的 条评论
为什么被折叠?



