java过程语言插件-pl/java在postgresql9.6中的安装及使用

pl/java简介

pl/java是一个postgreSQL数据库插件,与pl/sql、pl/perl、pl/python类似,安装该插件之后,函数和触发器可以用java语言实现然后加载进数据库。pljava开发工作于2003年开始,2005年1月第一个正式版本发布。

pl/java下载

git clone https://github.com/tada/pljava

pl/java构建条件

1、C程序编译链接工具,gcc g++ (笔者版本为4.8.5,推荐4.3.0以上版本)
2、jdk(笔者版本为openjdk1.8,1.7以下版本可能会某些依赖下载失败)
3、安装postgre数据库,pg_config路径加入环境变量
4、pljava由maven构建,需要安装maven(不低于3.0.4版本)
note:建议不要用yum安装maven, yum安装的maven版本一般比较低,编译过程可能会由于依赖库版本问题导致编译失败,建议手动下载maven 3.6以上的版本

Building pl/java

1、下载pl/java源码到指定目录

[qin@pcmk2 pljava-master]$ pwd
/home/qin/pljava-master

2、进入源码根目录执行命令:mvn clean install

[qin@pcmk2 pljava-master]$ ls
COPYRIGHT  docs  freenode.ver  pljava  pljava-ant  pljava-api  pljava-examples  pljava-packaging  pljava-so  pom.xml  README.md  src  target
[qin@pcmk2 pljava-master]$ mvn clean install

note: 第一次构建,有很多依赖库需要下载,构建过程可能会需要几个小时,请耐心等待,二次构建就会变快

3、构建成功后显示如下:

```bash
[INFO] Executed tasks
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ pljava-packaging ---
[INFO] Installing /home/qin/pljava-master/pljava-packaging/pom.xml to /home/qin/.m2/repository/org/postgresql/pljava-packaging/1.6.0-SNAPSHOT/pljava-packaging-1.6.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for PostgreSQL PL/Java 1.6.0-SNAPSHOT:
[INFO] 
[INFO] PostgreSQL PL/Java ................................. SUCCESS [  2.224 s]
[INFO] PL/Java API ........................................ SUCCESS [ 14.573 s]
[INFO] PL/Java backend Java code .......................... SUCCESS [  5.684 s]
[INFO] PL/Java backend native code ........................ SUCCESS [ 22.446 s]
[INFO] PL/Java Ant tasks .................................. SUCCESS [  0.587 s]
[INFO] PL/Java examples ................................... SUCCESS [12:23 min]
[INFO] PL/Java packaging .................................. SUCCESS [15:00 min]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  28:11 min
[INFO] Finished at: 2020-05-02T18:48:34+08:00
[INFO] ------------------------------------------------------------------------

构建成功之后pljava-packaging/target子目录下的jar文件pljava-pg9.6-amd64-Linux-gpp.jar,包含有安装pljava所需的所有文件,java -jar命令可以把jar包所包含的文件提取出来并自动放入postgresql安装目录extension子目录下。

安装pl/java进入postgreSQL 9.6

1、build完成之后,执行以下命令,可以安装Pl/java extension进入postgresql安装目录
cd ~/pljava-master/pljava-packaging/target
java -jar pljava-pg9.6-amd64-Linux-gpp.jar

[qin@pcmk2 target]$ java -jar pljava-pg9.6-amd64-Linux-gpp.jar 
/home/qin/pg96/lib/postgresql/libpljava-so-1.6.0-SNAPSHOT.so as bytes
/home/qin/pg96/share/postgresql/pljava/pljava-1.6.0-SNAPSHOT.jar as bytes
/home/qin/pg96/share/postgresql/pljava/pljava-api-1.6.0-SNAPSHOT.jar as bytes
/home/qin/pg96/share/postgresql/pljava/pljava-examples-1.6.0-SNAPSHOT.jar as bytes
/home/qin/pg96/share/postgresql/extension/pljava.control as lines (ASCII)
/home/qin/pg96/share/postgresql/pljava/pljava--1.6.0-SNAPSHOT.sql as lines (UTF8)
/home/qin/pg96/share/postgresql/pljava/pljava--unpackaged--1.6.0-SNAPSHOT.sql as lines (UTF8)
/home/qin/pg96/share/postgresql/pljava/pljava--1.5.5--1.6.0-SNAPSHOT.sql as lines (UTF8)
/home/qin/pg96/share/postgresql/pljava/pljava--1.5.4--1.6.0-SNAPSHOT.sql as lines (UTF8)
/home/qin/pg96/share/postgresql/pljava/pljava--1.5.3--1.6.0-SNAPSHOT.sql as lines (UTF8)
/home/qin/pg96/share/postgresql/pljava/pljava--1.5.2--1.6.0-SNAPSHOT.sql as lines (UTF8)
/home/qin/pg96/share/postgresql/pljava/pljava--1.5.1--1.6.0-SNAPSHOT.sql as lines (UTF8)
/home/qin/pg96/share/postgresql/pljava/pljava--1.5.1-BETA3--1.6.0-SNAPSHOT.sql as lines (UTF8)
/home/qin/pg96/share/postgresql/pljava/pljava--1.5.1-BETA2--1.6.0-SNAPSHOT.sql as lines (UTF8)
/home/qin/pg96/share/postgresql/pljava/pljava--1.5.1-BETA1--1.6.0-SNAPSHOT.sql as lines (UTF8)
/home/qin/pg96/share/postgresql/pljava/pljava--1.5.0--1.6.0-SNAPSHOT.sql as lines (UTF8)
/home/qin/pg96/share/postgresql/pljava/pljava--1.5.0-BETA3--1.6.0-SNAPSHOT.sql as lines (UTF8)
/home/qin/pg96/share/postgresql/pljava/pljava--1.5.0-BETA2--1.6.0-SNAPSHOT.sql as lines (UTF8)
/home/qin/pg96/share/postgresql/pljava/pljava--1.5.0-BETA1--1.6.0-SNAPSHOT.sql as lines (UTF8)
[qin@pcmk2 target]$ pwd
/home/qin/pljava-master/pljava-packaging/target

2、执行完成后,进入postgresql安装目录下extension子目录,可以发现pljava库文件已经生成
在这里插入图片描述
3、在create extension之前还必须设置一个数据库环境变量pljava.libjvm_location,该变量告诉pljava使用的java库文件位置
可以在postgresql.conf中填加:
pljava.libjvm_location=’/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.252.b09-2.el7_8.x86_64/jre/lib/amd64/server/libjvm.so’
该环境变量为会话级,也可以:
SET pljava.libjvm_location TO ‘/usr/lib/jvm/java-1.8.0/lib/…’;

4、psql登录数据库执行create exteinsion:

postgres=# CREATE EXTENSION pljava;
CREATE EXTENSION

5、插件创建成功

postgres=# \dL
                                                      List of languages
  Name   |  Owner   | Trusted |                                         Description                                          
---------+----------+---------+----------------------------------------------------------------------------------------------
 java    | postgres | t       | Trusted/sandboxed language for routines and types in Java; http://tada.github.io/pljava/
 javau   | postgres | f       | Untrusted/unsandboxed language for routines and types in Java; http://tada.github.io/pljava/
 plpgsql | postgres | t       | PL/pgSQL procedural language
(3 rows)

测试plsql调用java程序

1、测试java程序Hello.java


package com.example.proj;

public class Hello {

  public static String hello(String toWhom) {
    return "Hello, " + toWhom + "!";
  }

}

2、编写manifest.txt

Manifest-Version: 1.0
Main-Class: Hello
Specification-Title: "Hello"
Specification-Version: "1.0"
Created-By: 1.8.0_252
Build-Date: 05/02/2020 21:00

3、执行如下命令,将程序编译打包

[qin@pcmk2 plj_test]$ javac -d . Hello.java
[qin@pcmk2 plj_test]$ jar cfm Hello.jar manifest.txt com 
[qin@pcmk2 plj_test]$ ls
com  Hello.jar  Hello.java  manifest.txt

4、加载包含测试程序的Jar包至数据库
pljava扩展安装之后,sqlj模式下有install_jar、replace_jar、remove_jar三个函数用于加载、替换、移除jar包到数据库中
sqlj.replace_jar(, , )函数包函三个参数
jar_url :jar文件的URL.
jar_name:数据库中映射jar包的逻辑名.
redeploy:True if the jar should be undeployed according to the deployment descriptor of the old jar and deployed according to the deployment descriptor of the new jar, false otherwise
例:

postgres=# select sqlj.install_jar('file:///home/qin/plj_test/Hello.jar','examples', true);
 install_jar 
-------------
 
(1 row)

5、映射逻辑名examples到public模式

postgres=# SELECT sqlj.set_classpath('public', 'examples');
 set_classpath 
---------------
 
(1 row)

6、查询映射是否成功

postgres=# select sqlj.get_classpath('public');
 get_classpath 
---------------
 examples
(1 row)

7、创建plsql函数

postgres=# create or replace function say_hello(varchar) returns varchar 
postgres-# as 'com.example.proj.Hello.hello' language java;
CREATE FUNCTION
postgres=# \df
                           List of functions
 Schema |    Name    | Result data type  | Argument data types |  Type  
--------+------------+-------------------+---------------------+--------
 public | getsysprop | character varying | character varying   | normal
 public | say_hello  | character varying | character varying   | normal
(2 rows)

8、测试plsql函数功能

postgres=# select say_hello('Lining');
   say_hello    
----------------
 Hello, Lining!
(1 row)

postgres=#

参考文献:

1、https://tada.github.io/pljava/build/build.html. pl/java build指南
2、https://github.com/tada/pljava/releases. pl/java源码地址

你可能感兴趣的:(postgresql,pl/java)