For example, the following code is stored in a file
<project_dir>/src/main/java/com/mycompany/app/App1.java:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import oracle.pgx.api.*;
import oracle.pg.rdbms.pgql.jdbc.PgqlJdbcRdbmsDriver;
public class App1 {
public static void main(String[] args) throws Exception {
String dbConnectString = args[0];
String username = args[1];
String password = args[2];
// Obtain a JDBC database connection
DriverManager.registerDriver(new PgqlJdbcRdbmsDriver());
String jdbcUrl = "jdbc:oracle:pgql:@" + dbConnectString;
System.out.println("connecting to " + jdbcUrl);
try (Connection conn = DriverManager.getConnection(jdbcUrl, username, password)) {
conn.setAutoCommit(false);
// Create a PGQL statement to execute PGQL queries
Statement stmt = conn.createStatement();
// Create a PGQL property graph using the CREATE PROPERTY GRAPH statement
String pgPgqlName = "BANK_GRAPH";
String createPgPgqlQuery =
"CREATE PROPERTY GRAPH " + pgPgqlName + " " +
"VERTEX TABLES ( BANK_ACCOUNTS AS ACCOUNTS " +
"KEY (ID) " +
"LABEL ACCOUNTS " +
"PROPERTIES (ID, NAME)" +
") " +
"EDGE TABLES ( BANK_TRANSFERS AS TRANSFERS " +
"KEY (SRC_ACCT_ID, DST_ACCT_ID, AMOUNT) " +
"SOURCE KEY (SRC_ACCT_ID) REFERENCES ACCOUNTS (ID) " +
"DESTINATION KEY (DST_ACCT_ID) REFERENCES ACCOUNTS (ID) " +
"LABEL TRANSFERS " +
"PROPERTIES (SRC_ACCT_ID, DST_ACCT_ID, AMOUNT, DESCRIPTION)" +
") OPTIONS(PG_PGQL)";
stmt.execute(createPgPgqlQuery);
// Execute a query to retrieve the first 10 elements of the graph
String pgqlQuery =
"SELECT * FROM GRAPH_TABLE ( " + pgPgqlName + " " +
"MATCH " +
"(a IS accounts) -[e IS transfers]-> (b IS accounts) " +
"COLUMNS (a.id AS acc_a, b.id AS acc_b, e.amount AS amount) " +
") FETCH FIRST 5 ROWS ONLY";
ResultSet rs = stmt.executeQuery(pgqlQuery);
while (rs.next()) {
Integer from_acct = rs.getInt(1);
Integer to_acct = rs.getInt(2);
Float amount = rs.getFloat(3);
System.out.println("from_acct:" + from_acct);
System.out.println("to_acct:" + to_acct);
System.out.println("amount:" + amount);
System.out.println("");
}
// Drop the PGQL property graph using the DROP PROPERTY GRAPH statement
String dropPgPgqlQuery = "DROP PROPERTY GRAPH " + pgPgqlName;
stmt.execute(dropPgPgqlQuery);
}
System.exit(0);
}
}
You can then compile and run the preceding code by navigating
to your project directory and running the following
command:
mvn compile exec:java -Dexec.mainClass="com.mycompany.app.App1"-Dexec.arguments='<db-connect-string>,<username>,<password>'
On successful processing, the code may produce an output
similar to the following. Note, your output may be different depending on
your
<db-connect-string>.
[INFO] --- exec-maven-plugin:3.1.0:java (default-cli) @ my-app ---
connecting to jdbc:oracle:pgql:@myhost:1521/oradb
from_acct:227
to_acct:778
amount:5279.0
from_acct:228
to_acct:458
amount:9917.0
from_acct:228
to_acct:387
amount:1993.0
from_acct:228
to_acct:126
amount:9302.0
from_acct:228
to_acct:901
amount:4339.0