Using Oracle Graph Java Client on Maven Central

You can obtain the property graph Java client from Maven Central.

The Maven artifact for the graph Java client is described as follows:
  • Group Name: com.oracle.database.graph
  • Artifact Name: opg-client
  • Version: 23.1.0

You can perform the following steps to use the graph Java client from Maven Central:

  1. Download and Install Apache Maven on your system.
    See Apache Maven Project for more information.
  2. Add the bin folder with the mvn command to the PATH variable.
  3. Build your Maven project and navigate to the project directory.
  4. Edit the pom.xml file on the following:
    1. Add the graph Java client dependency as shown:
      <dependencies>
          <dependency>
            <groupId>com.oracle.database.graph</groupId>
            <artifactId>opg-client</artifactId>
            <version>23.1.0</version>
          </dependency>
      </dependencies>

      Note:

      If you use Gradle as a build tool, then the equivalent dependency declaration for the Java client is:

      implementation group: 'com.oracle.database.graph', name: 'opg-client', version: '23.1.0'

    2. Add the following repository as the Java client depends on the Spoofax Language Workbench Library to compile PGQL queries:
      <repositories>
          <repository>
            <id>spoofax</id>
            <url>https://artifacts.metaborg.org/content/repositories/releases</url>
          </repository>
      </repositories>
  5. Optionally, you can skip step 4 and copy the following minimal POM configuration in <project_dir>/pom.xml file:
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.mycompany.app</groupId>
      <artifactId>my-app</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>my-app</name>
      <repositories>
        <repository>
          <id>spoofax</id>
          <url>https://artifacts.metaborg.org/content/repositories/releases</url>
        </repository>       
      </repositories>
      <dependencies>
        <dependency>
          <groupId>com.oracle.database.graph</groupId>
          <artifactId>opg-client</artifactId>
          <version>23.1.0</version>
        </dependency>
      </dependencies>
    </project>
  6. Build your Java code in <project_dir>/src/main/java/com/mycompany/app and compile with Maven.
    For example, the following code is stored in a file <project_dir>/src/main/java/com/mycompany/app/App1.java:
    package com.mycompany.app;
     
    import java.sql.DriverManager;
    import java.sql.Connection;
    import java.sql.Statement;
    import oracle.pg.rdbms.pgql.PgqlConnection;
    import oracle.pg.rdbms.pgql.PgqlStatement;
    import oracle.pg.rdbms.pgql.PgqlResultSet;
    import oracle.pgx.api.*;
    import oracle.pg.rdbms.GraphServer;
    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 PGQL connection
          PgqlConnection pgqlConn = PgqlConnection.getConnection(conn);
    
          // Create a PGQL statement to execute PGQL queries
          PgqlStatement pgqlStmt = pgqlConn.createStatement();
    
          // Create a property graph view using the CREATE PROPERTY GRAPH statement
          String pgViewName = "BANK_GRAPH_VIEW";
          String createPgViewQuery = 
              "CREATE PROPERTY GRAPH " + pgViewName + " " +
              "VERTEX TABLES ( BANK_ACCOUNTS AS ACCOUNTS " +
              "KEY (ID) " +
              "LABEL ACCOUNTS " +
              "PROPERTIES (ID, NAME)" +
              ") " +
              "EDGE TABLES ( BANK_TXNS AS TRANSFERS " +
              "KEY (FROM_ACCT_ID, TO_ACCT_ID, AMOUNT) " +
              "SOURCE KEY (FROM_ACCT_ID) REFERENCES ACCOUNTS (ID) " +
              "DESTINATION KEY (TO_ACCT_ID) REFERENCES ACCOUNTS (ID) " +
              "LABEL TRANSFERS " +
              "PROPERTIES (FROM_ACCT_ID, TO_ACCT_ID, AMOUNT, DESCRIPTION)" +
              ") OPTIONS(PG_VIEW)";
    
          pgqlStmt.execute(createPgViewQuery);
    
          // Execute a query to retrieve the first 10 elements of the graph
          String pgqlQuery = 
              "SELECT e.from_acct_id, e.to_acct_id, e.amount FROM " +
              "MATCH (n:ACCOUNTS) -[e:TRANSFERS]-> (m:ACCOUNTS) ON " +
              pgViewName + " LIMIT 10";
    
          PgqlResultSet rs = pgqlStmt.executeQuery(pgqlQuery);
          rs.print();
    
          // Drop the property graph view using the DROP PROPERTY GRAPH statement
          String dropPgViewQuery = "DROP PROPERTY GRAPH " + pgViewName;
          pgqlStmt.execute(dropPgViewQuery);
        }
        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
    name = Baz