大家在平時使用 Greenplum 的過程中,肯定免不了存儲過程的使用。今天來分享一個存儲過程使用過程中的小Tips:
如何打印/獲取 Raise Notice 信息。存儲過程 demo
因為主要展示如何獲取調試信息,所以這裡的存儲過程 demo 比較簡單,結構如下,執行過程中會返回幾條調試信息和結果1。
<code>create or replace function test_proc()
returns text
as
$$
BEGIN
raise notice 'This is a JDBC raise notice test case!';
raise notice 'Raise notice 1';
raise notice 'Raise notice 2';
raise notice 'Raise notice 3';
raise notice 'Raise notice 4';
return '1';
END;
$$ language plpgsql;/<code>
psql 命令行獲取 Raise Notice
一般情況下我們都是在終端中通過 psql 命令行進行調試,這樣可以輸出最全的信息,如下:
<code>postgres=# select test_proc();
NOTICE: This is a JDBC raise notice test case!
NOTICE: Raise notice 1
NOTICE: Raise notice 2
NOTICE: Raise notice 3
NOTICE: Raise notice 4
test_proc
-----------
1
(1 row)
postgres=#/<code>
JDBC 獲取 Raise Notice
Java 開發過程中,可能需要獲取上面這些 NOTICE 調試信息,方便對存儲過程執行的過程進行判斷控制。這個可以通過 statement 的 getWarnings() 方法獲取,如果有多條調試信息,可以循環獲取。下面直接展示代碼:
<code>import java.sql.*;
public class PostgreSQLJDBC {
public static void main(String args[]) {
Connection c = null;
Statement stmt = null;
try {
Class.forName("com.pivotal.jdbc.GreenplumDriver");
c = DriverManager.getConnection("jdbc:pivotal:greenplum://10.211.55.6:5432;;DatabaseName=postgres", "gpadmin1", "");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
//打印結果及打印存儲過程告警信息
ResultSet rs = stmt.executeQuery("select test_proc();");
while (rs.next()) {
System.out.println(rs.getString(1));
}
SQLWarning warn = stmt.getWarnings();
if(warn != null)
{
for (Throwable e : warn)
{
System.out.println(e.getMessage());
}
}
stmt.close();
c.commit();
c.close();
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
}
}/<code>
阿福:
社區活躍志願者,山東輝鴻泛在電子科技技術總監。豐富的數據庫開發運維經驗。主導並完成了多個基於 Greenplum 數據平臺的落地。
閱讀更多 Greenplum中文社區 的文章