前言
jdbc_fdw插件也许性能不一定很高,但是通用性非常好,只要提供了对应的jdbc库,基本上能访问所有的数据库。我这里使用jdk1.8,并在PostgreSQL 14.7下边进行试验。连接目标数据库:Sybase ASE 16.0.3 以及 PostgreSQL 14.4/14.7
实际验证
1、环境准备
准备jdk1.8
[09:21:57-postgres@sean-rh3:/pgccc/source]$ java -version
openjdk version "1.8.0_332"
OpenJDK Runtime Environment (build 1.8.0_332-b09)
OpenJDK 64-Bit Server VM (build 25.332-b09, mixed mode)
光这个还不够。需要javac
yum install java-1.8.0-openjdk-devel java-1.8.0-openjdk.x86_64
提前配置好动态库路径,以供jdbc_fdw编译安装时调用:
[09:29:55-root@sean-rh3:/usr]$ find / -name libjvm.so
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.362.b09-2.el8_7.x86_64/jre/lib/amd64/server/libjvm.so
....
ln -s /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.362.b09-2.el8_7.x86_64/jre/lib/amd64/server/libjvm.so /usr/lib64/libjvm.so
2、编译jdbc_fdw
git clone https://github.com/pgspider/jdbc_fdw
cd jdbc_fdw
[09:42:44-postgres@sean-rh3:/pgccc/source/jdbc_fdw]$ make
Makefile:49: /contrib/contrib-global.mk: No such file or directory
make: *** No rule to make target '/contrib/contrib-global.mk'. Stop.
同现这个错,是因为: Makefile中有判断:
ifdef USE_PGXS
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
使用命令make USE_PGXS=1 即可。接着编译:
--因为文件权限,都用su权限编译并安装
[09:51:20-postgres@sean-rh3:/pgccc/source/jdbc_fdw]$ su -c "make USE_PGXS=1 install"
Password:
javac -d /usr/pgsql-14.7build/lib JDBCUtils.java JDBCDriverLoader.java
/usr/bin/mkdir -p '/usr/pgsql-14.7build/lib'
/usr/bin/mkdir -p '/usr/pgsql-14.7build/share/extension'
/usr/bin/mkdir -p '/usr/pgsql-14.7build/share/extension'
/usr/bin/install -c -m 755 jdbc_fdw.so '/usr/pgsql-14.7build/lib/jdbc_fdw.so'
/usr/bin/install -c -m 644 .//jdbc_fdw.control '/usr/pgsql-14.7build/share/extension/'
/usr/bin/install -c -m 644 .//jdbc_fdw--1.0.sql .//jdbc_fdw--1.0--1.1.sql .//jdbc_fdw--1.2.sql '/usr/pgsql-14.7build/share/extension/'
3、验证使用
3.1、超级用户安装插件:
[09:57:27-postgres@sean-rh3:/opt/pg]$ psql mydb
psql (14.7 [Sean])
Type "help" for help.
mydb=# create extension jdbc_fdw;
CREATE EXTENSION
mydb=# \dx
List of installed extensions
Name | Version | Schema | Description
----------+---------+------------+-------------------------------------------------------------
jdbc_fdw | 1.2 | public | foreign-data wrapper for remote servers available over JDBC
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
(2 rows)
mydb=# \dx+ jdbc_fdw
Objects in extension "jdbc_fdw"
Object description
-----------------------------------------
foreign-data wrapper jdbc_fdw
function jdbc_exec(text,text)
function jdbc_fdw_handler()
function jdbc_fdw_validator(text[],oid)
function jdbc_fdw_version()
(5 rows)
mydb=# select jdbc_fdw_version();
jdbc_fdw_version
------------------
301
(1 row)
3.2、给普通用户授权
mydb=# grant USAGE on FOREIGN data wrapper jdbc_fdw to mydb;
GRANT
3.3、用超级用户创建server, 配置jdbc连接参数
创建jdbc server:
CREATE SERVER jdbc_ase FOREIGN DATA WRAPPER jdbc_fdw OPTIONS(
drivername 'com.sybase.jdbc4.jdbc.SybDriver',
url 'jdbc:sybase:Tds:10.47.43.86:5000',
querytimeout '5s',
jarfile '/pgccc/source/jconn4.jar',
maxheapsize '256MB'
);
CREATE SERVER jdbc_pg FOREIGN DATA WRAPPER jdbc_fdw OPTIONS(
drivername 'org.postgresql.Driver',
url 'jdbc:postgresql://10.47.43.86:5433/mydb',
querytimeout '5s',
jarfile '/pgccc/source/postgresql-42.5.1.jar',
maxheapsize '256MB'
);
mydb=# \des+
List of foreign servers
Name | Owner | Foreign-data wrapper | Access privileges | Type | Version |
FDW options
| Description
----------+----------+----------------------+-------------------+------+---------+------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------
-----------------+-------------
jdbc_ase | postgres | jdbc_fdw | | | | (drivername 'com.sybase.jdbc4.jdbc.SybDriver', url 'j
dbc:sybase:Tds:10.47.43.86:5000?ServiceName=sean', querytimeout '5s', jarfile '/home/sean/sybase/jConnect-16_0/classes/jconn4.jar', maxh
eapsize '256MB') |
(1 row)
3.4、超级用户创建与jdbc server之间的用户映射
CREATE USER MAPPING FOR postgres SERVER jdbc_ase OPTIONS (username 'sean',password 'abcdef');
CREATE USER MAPPING FOR postgres SERVER jdbc_pg OPTIONS (username 'postgres',password 'abcdef');
mydb=# CREATE USER MAPPING FOR postgres SERVER jdbc_ase OPTIONS (username 'sean',password 'secret');
CREATE USER MAPPING
3.5、创建外部表
-- wrong
CREATE FOREIGN TABLE f_t123(id int options(key 'true'), col2 varchar(32)) SERVER jdbc_ase OPTIONS (
schema_name 'public',
table_name 't123',
updatable 'true'
);
--这个可以工作
CREATE FOREIGN TABLE t123 (id int , col2 varchar(32)) SERVER jdbc_ase;
-- wrong
CREATE FOREIGN TABLE f_t(id int options(key 'true'), col2 varchar(32)) SERVER jdbc_pg OPTIONS (
schema_name 'public',
table_name 't123',
updatable 'true'
);
--这个可以工作?
CREATE FOREIGN TABLE t1 (id int , col2 varchar(32)) SERVER jdbc_pg;
3.6、外部表测试
insert into f_t123 values(1, 'Zhao');
mydb=# select * from f_t123;
ERROR: remote server returned an error
CREATE FOREIGN TABLE
mydb=# select * from f_t;
ERROR: remote server returned an error
mydb=# select * from t123; -- for ASE
id | col2
----+------
1 | zhao
(1 row)
参考
[1] https://www.modb.pro/db/616311: https://www.modb.pro/db/616311