From 0703d59ca4bb9f382492b165d726054671c64f75 Mon Sep 17 00:00:00 2001 From: imran zaheer Date: Wed, 14 Dec 2022 01:39:53 +0500 Subject: [PATCH 1/3] sample code for age-jdbc-drivers --- drivers/jdbc/samples/AgeBasic.java | 142 +++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 drivers/jdbc/samples/AgeBasic.java diff --git a/drivers/jdbc/samples/AgeBasic.java b/drivers/jdbc/samples/AgeBasic.java new file mode 100644 index 000000000..d9a558922 --- /dev/null +++ b/drivers/jdbc/samples/AgeBasic.java @@ -0,0 +1,142 @@ +import org.apache.age.jdbc.base.Agtype; +import org.postgresql.jdbc.PgConnection; + +import java.sql.*; + +public class AgeBasic { + + public static PgConnection connection; + static final String DB_URL = "jdbc:postgresql://localhost:5432/demo"; + static final String USER = "postgres"; + static final String PASS = "1234"; + + public static void main(String[] args) { + + // Open a connection + try { + makeConnection(); + + if (graphExist("demo_graph")) + dropGraph(); + + createGraph(); + getResult(); + getNodesFromResult(); + + // after all + dropGraph(); + closeConnection(); + } + catch (Exception e) { + System.out.println(e.getMessage()); + } + + } + + /** + * Creating a connection with the database + * Loading AGE extension & configuring database for AGE + */ + public static void makeConnection() throws SQLException { + connection = DriverManager.getConnection(DB_URL, USER, PASS).unwrap(PgConnection.class); + connection.addDataType("agtype", Agtype.class); + + // configure AGE + Statement statement = connection.createStatement(); + statement.execute("CREATE EXTENSION IF NOT EXISTS age;"); + statement.execute("LOAD 'age'"); + statement.execute("SET search_path = ag_catalog, \"$user\", public;"); + } + + + /** + * Using Agtype parser on the ResultSet to get the result in Agtype. + * + */ + public static void getResult() throws SQLException { + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT * from cypher('demo_graph', $$ MATCH (n) RETURN n $$) as (n agtype);"); + + + // Returning result as Agtype + System.out.println("\nRESULT AS AGTYPE"); + while (rs.next()) { + Agtype returnedAgtype = rs.getObject(1, Agtype.class); + System.out.println(returnedAgtype.getValue()); + } + } + + /** + * Using Agtype to extract specific objects from the Agtype result. + * + */ + public static void getNodesFromResult() throws SQLException { + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT * from cypher('demo_graph', $$ MATCH (n) RETURN n $$) as (n agtype);"); + + System.out.println("\nEXTRACTING OBJECTS FROM RESULT"); + while (rs.next()) { + Agtype returnedAgtype = rs.getObject(1, Agtype.class); + String nodeLabel = returnedAgtype.getMap().getObject("label").toString(); + String nodeProp = returnedAgtype.getMap().getObject("properties").toString(); + + System.out.println("Vertex : " + nodeLabel + ", \tProps : " + nodeProp); + } + } + + + /** + * Creating a 'demo_graph'. + * Then adding some Nodes. + * Then connecting them with some relation. + */ + public static void createGraph() throws SQLException { + Statement statement = connection.createStatement(); + + // Nodes labeled as PERSON + statement.execute("SELECT create_graph('demo_graph');"); + statement.execute("SELECT * FROM cypher('demo_graph', $$ CREATE (n:Person {name : 'imran', bornIn : 'Pakistan'}) $$) AS (a agtype);"); + statement.execute("SELECT * FROM cypher('demo_graph', $$ CREATE (n:Person {name : 'ali', bornIn : 'Pakistan'}) $$) AS (a agtype);"); + statement.execute("SELECT * FROM cypher('demo_graph', $$ CREATE (n:Person {name : 'usama', bornIn : 'Pakistan'}) $$) AS (a agtype);"); + statement.execute("SELECT * FROM cypher('demo_graph', $$ CREATE (n:Person {name : 'akbar', bornIn : 'Pakistan'}) $$) AS (a agtype);"); + statement.execute("SELECT * FROM cypher('demo_graph', $$ CREATE (n:Person {name : 'james', bornIn : 'US'}) $$) AS (a agtype);"); + statement.execute("SELECT * FROM cypher('demo_graph', $$ CREATE (n:Person {name : 'david', bornIn : 'US'}) $$) AS (a agtype);"); + statement.execute("SELECT * FROM cypher('demo_graph', $$ CREATE (n:Person {name : 'max', bornIn : 'US'}) $$) AS (a agtype);"); + + // Nodes labeled as COUNTRY + statement.execute("SELECT * FROM cypher('demo_graph', $$ CREATE (n:Country{name : 'Pakistan'}) $$) AS (a agtype);"); + statement.execute("SELECT * FROM cypher('demo_graph', $$ CREATE (n:Country{name : 'US'}) $$) AS (a agtype);"); + + // creating relationship (n:PERSON)-[r:BORNIN]-(n:COUNTRY) + statement.execute("SELECT * from cypher('demo_graph', $$ MATCH (a:Person)-[r]-(b:Country) WHERE a.bornIn = b.name RETURN a, r, b $$) as (a agtype, r agtype, b agtype);"); + + } + + /** + * After done querying drop the whole graph + * + */ + public static void dropGraph() throws SQLException { + Statement statement = connection.createStatement(); + + // DROPPING GRAPH with CASCADE:true + statement.execute("SELECT * FROM ag_catalog.drop_graph('demo_graph', true);"); + } + + public static boolean graphExist(String graph_name) throws SQLException { + + PreparedStatement ps = connection.prepareStatement("SELECT * FROM ag_catalog.ag_graph WHERE name = ?"); + ps.setString(1, graph_name); + ps.executeQuery(); + ResultSet rs = ps.getResultSet(); + return !(!rs.isBeforeFirst() && rs.getRow() == 0); + } + + /** + * At last close the connection before exit. + */ + public static void closeConnection() throws SQLException { + connection.close(); + } + +} \ No newline at end of file From 11aab817a5b312c369e5bc35d36ff46a68b6eccf Mon Sep 17 00:00:00 2001 From: imran zaheer Date: Wed, 14 Dec 2022 16:39:30 +0500 Subject: [PATCH 2/3] cleaned the code --- drivers/jdbc/samples/AgeBasic.java | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/drivers/jdbc/samples/AgeBasic.java b/drivers/jdbc/samples/AgeBasic.java index d9a558922..37284b274 100644 --- a/drivers/jdbc/samples/AgeBasic.java +++ b/drivers/jdbc/samples/AgeBasic.java @@ -8,10 +8,10 @@ public class AgeBasic { public static PgConnection connection; static final String DB_URL = "jdbc:postgresql://localhost:5432/demo"; static final String USER = "postgres"; - static final String PASS = "1234"; + static final String PASS = "pass"; public static void main(String[] args) { - + // Open a connection try { makeConnection(); @@ -21,7 +21,7 @@ public static void main(String[] args) { createGraph(); getResult(); - getNodesFromResult(); + getObjectsFromResult(); // after all dropGraph(); @@ -47,17 +47,14 @@ public static void makeConnection() throws SQLException { statement.execute("LOAD 'age'"); statement.execute("SET search_path = ag_catalog, \"$user\", public;"); } - - + /** * Using Agtype parser on the ResultSet to get the result in Agtype. - * */ public static void getResult() throws SQLException { Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * from cypher('demo_graph', $$ MATCH (n) RETURN n $$) as (n agtype);"); - - + // Returning result as Agtype System.out.println("\nRESULT AS AGTYPE"); while (rs.next()) { @@ -68,9 +65,8 @@ public static void getResult() throws SQLException { /** * Using Agtype to extract specific objects from the Agtype result. - * */ - public static void getNodesFromResult() throws SQLException { + public static void getObjectsFromResult() throws SQLException { Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * from cypher('demo_graph', $$ MATCH (n) RETURN n $$) as (n agtype);"); @@ -84,7 +80,6 @@ public static void getNodesFromResult() throws SQLException { } } - /** * Creating a 'demo_graph'. * Then adding some Nodes. @@ -113,13 +108,11 @@ public static void createGraph() throws SQLException { } /** - * After done querying drop the whole graph - * + * After done querying drop the whole graph. + * Dropping the GRAPH with CASCADE:true. Deletes the whole data in the graph. */ public static void dropGraph() throws SQLException { Statement statement = connection.createStatement(); - - // DROPPING GRAPH with CASCADE:true statement.execute("SELECT * FROM ag_catalog.drop_graph('demo_graph', true);"); } @@ -138,5 +131,5 @@ public static boolean graphExist(String graph_name) throws SQLException { public static void closeConnection() throws SQLException { connection.close(); } - + } \ No newline at end of file From 55215b118ca1a3a1e3dbf07d961ba6e67869ed62 Mon Sep 17 00:00:00 2001 From: imran zaheer Date: Thu, 15 Dec 2022 22:08:46 +0500 Subject: [PATCH 3/3] update query statement --- drivers/jdbc/samples/AgeBasic.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/jdbc/samples/AgeBasic.java b/drivers/jdbc/samples/AgeBasic.java index 37284b274..f6f296445 100644 --- a/drivers/jdbc/samples/AgeBasic.java +++ b/drivers/jdbc/samples/AgeBasic.java @@ -103,8 +103,7 @@ public static void createGraph() throws SQLException { statement.execute("SELECT * FROM cypher('demo_graph', $$ CREATE (n:Country{name : 'US'}) $$) AS (a agtype);"); // creating relationship (n:PERSON)-[r:BORNIN]-(n:COUNTRY) - statement.execute("SELECT * from cypher('demo_graph', $$ MATCH (a:Person)-[r]-(b:Country) WHERE a.bornIn = b.name RETURN a, r, b $$) as (a agtype, r agtype, b agtype);"); - + statement.execute("SELECT * FROM cypher('demo_graph', $$ MATCH (a:Person), (b:Country) WHERE a.bornIn = b.name CREATE (a)-[r:BORNIN]->(b) RETURN r $$) as (r agtype);"); } /**