|
| 1 | +package pg.oracle; |
| 2 | + |
| 3 | +/* property graph demo |
| 4 | + this code demonstrates: |
| 5 | + 1. integration of Property Graph with SQL in Oracle Database 23c |
| 6 | + 2. Java API for Property Graph Server |
| 7 | + It uses demonstration tables described in the following LiveLabs Workshop: |
| 8 | + "Analyze, Query, and Visualize Property Graphs with Oracle Database" |
| 9 | + https://apexapps.oracle.com/pls/apex/r/dbpm/livelabs/run-workshop?p210_wid=686&p210_wec=&session=108772142407826 |
| 10 | + Requirements: |
| 11 | + 1. Oracle Database 23c |
| 12 | + 2. Database user account ( <username> ) with the privileges described in the mentioned LiveLabs Workshop |
| 13 | + 3. Tables used in this workshop created in the mentioned schema |
| 14 | + 4. Oracle Property Graph Server installation using user account |
| 15 | + */ |
| 16 | + |
| 17 | + |
| 18 | +import java.sql.*; |
| 19 | +import oracle.jdbc.pool.OracleDataSource; |
| 20 | +import oracle.pg.rdbms.GraphServer; |
| 21 | +import oracle.pgx.api.*; |
| 22 | + |
| 23 | +public class Main { |
| 24 | + private static String dbHost = "<database_hostname_or_op>"; |
| 25 | + private static String pgxHost = "<graph_server_hostname_or_ip>"; |
| 26 | + private static int dbPort = <listener_port_number_ususally_1521>; |
| 27 | + private static int pgxPort = <graph_server_port_number_usually_7007>; |
| 28 | + private static String dbService = "<database_service_name>"; |
| 29 | + private static String username = "<username>"; |
| 30 | + private static String password = "<password>"; |
| 31 | + private static String pgName = "CUSTOMER_GRAPH"; |
| 32 | + public static void SQLPGDemo() { |
| 33 | + try { |
| 34 | + OracleDataSource ds = new OracleDataSource(); |
| 35 | + ds.setDriverType("thin"); |
| 36 | + ds.setServerName(dbHost); |
| 37 | + ds.setServiceName(dbService); |
| 38 | + ds.setPortNumber(dbPort); |
| 39 | + ds.setUser(username); |
| 40 | + ds.setPassword(password); |
| 41 | + Connection con = ds.getConnection(); |
| 42 | + System.out.println("Connected to the database"); |
| 43 | + System.out.println("(Re)creating property graph "+pgName); |
| 44 | + Statement stmt = con.createStatement(); |
| 45 | + stmt.execute("CREATE OR REPLACE PROPERTY GRAPH "+pgName+" VERTEX TABLES (\n" + |
| 46 | + " customer\n" + |
| 47 | + " , account\n" + |
| 48 | + " , merchant\n" + |
| 49 | + " )\n" + |
| 50 | + " EDGE TABLES (\n" + |
| 51 | + " account as account_edge\n" + |
| 52 | + " SOURCE KEY(id) REFERENCES account (id)\n" + |
| 53 | + " DESTINATION KEY(customer_id) REFERENCES customer (id)\n" + |
| 54 | + " LABEL owned_by PROPERTIES (id)\n" + |
| 55 | + " , parent_of as parent_of_edge \n" + |
| 56 | + " SOURCE KEY(customer_id_parent) REFERENCES customer (id)\n" + |
| 57 | + " DESTINATION KEY(customer_id_child) REFERENCES customer (id)\n" + |
| 58 | + " , purchased as puchased_edge \n" + |
| 59 | + " SOURCE KEY(account_id) REFERENCES account (id)\n" + |
| 60 | + " DESTINATION KEY(merchant_id) REFERENCES merchant (id)\n" + |
| 61 | + " , transfer as transfer_edge \n" + |
| 62 | + " SOURCE KEY(account_id_from) REFERENCES account (id)\n" + |
| 63 | + " DESTINATION KEY(account_id_to) REFERENCES account (id)\n" + |
| 64 | + " ) "); |
| 65 | + System.out.println("Graph (re)created succesfully."); |
| 66 | + ResultSet rset = stmt.executeQuery("SELECT account_no\n" + |
| 67 | + "FROM GRAPH_TABLE ( CUSTOMER_GRAPH MATCH (v1)-[transfer_edge]->{1,2}(v1)\n" + |
| 68 | + "columns (v1.account_no as account_no))"); |
| 69 | + while (rset.next()) { |
| 70 | + System.out.println(rset.getString(1)); |
| 71 | + } |
| 72 | + con.close(); |
| 73 | + } |
| 74 | + catch (Exception e) {e.printStackTrace();} |
| 75 | + } |
| 76 | + |
| 77 | + public static void PGXDemo() { |
| 78 | + try { |
| 79 | + ServerInstance si = GraphServer.getInstance("https://"+pgxHost+":"+pgxPort,username,password.toCharArray()); |
| 80 | + PgxSession ses = si.createSession("my-session"); |
| 81 | + System.out.println("Connected to graph server"); |
| 82 | + PgxGraph graph = ses.readGraphByName(username.toUpperCase(), pgName, GraphSource.PG_SQL); |
| 83 | + System.out.println("Graph loaded into Property Graph Server"); |
| 84 | + PgqlResultSet rset = graph.queryPgql("SELECT a1.account_no AS a1_account\n" + |
| 85 | + " , t1.transfer_date AS t1_date\n" + |
| 86 | + " , t1.amount AS t1_amount\n" + |
| 87 | + " , a2.account_no AS a2_account\n" + |
| 88 | + " , t2.transfer_date AS t2_date\n" + |
| 89 | + " , t2.amount AS t2_amount\n" + |
| 90 | + "FROM MATCH (a1)-[t1:transfer_edge]->(a2)-[t2:transfer_edge]->(a1)\n" + |
| 91 | + "WHERE t1.transfer_date < t2.transfer_date"); |
| 92 | + while (rset.next()) { |
| 93 | + System.out.println(rset.getString(1)); |
| 94 | + } |
| 95 | + } |
| 96 | + catch (Exception e) {e.printStackTrace();} |
| 97 | + } |
| 98 | + public static void main(String[] args) { |
| 99 | + SQLPGDemo(); |
| 100 | + PGXDemo(); |
| 101 | + } |
| 102 | +} |
0 commit comments