1.3.5.1.2 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: 22.4.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>22.4.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: '22.4.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>22.4.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:
    //The following code sample:
    //1) Connects to Oracle Database using Oracle JDBC Driver 
    //2) Creates a property graph
    //3) Adds a vertex to the graph
    //4) Runs PGQL queries on the graph and prints the output
    //5) Deletes the property graph
    
    package com.mycompany.app;
     
    import java.sql.*;
    import oracle.pg.rdbms.*;
    import oracle.pg.rdbms.pgql.jdbc.*;
    import oracle.pgx.api.*;
     
    public class App1 {
     
      public static void main(String[] args) throws Exception {
        String dbConnectString = args[0];
        String username = args[1];
        String password = args[2];
     
        // 2-tier example: run PGQL against database
        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);
          conn.prepareStatement("CREATE PROPERTY GRAPH test").execute();
          conn.commit();
          conn.prepareStatement("INSERT INTO test VERTEX v LABELS (Foo) PROPERTIES (v.name = 'Baz')").execute();
          conn.commit();
          PreparedStatement stmt = conn.prepareStatement("SELECT v.name FROM MATCH (v) ON test");
          stmt.execute();
          ResultSet rs = stmt.getResultSet();
          while (rs.next()) {
            System.out.println("name = " + rs.getString(1));
          }
          conn.prepareStatement("DROP PROPERTY GRAPH test").execute();
        }
        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