12.2 Using Autonomous Database Graph Client
Using the AdbGraphClient
API, you can access Graph Studio
features in Autonomous Database programmatically using the Oracle Graph Client or through
your Java or Python application.
This API provides the following capabilities:
- Authenticate with Autonomous Database
- Manage the Graph Studio environment
- Execute graph queries and algorithms against the graph server (PGX)
- Execute graph queries directly against Oracle Database
To use the AdbGraphClient
API, you must have access to
Oracle Graph Client installation. The API is provided by the Oracle Graph Client
library which is a part of the Oracle Graph Server and Client distribution. See
Installing Oracle Graph Client on how to
install and get started with the graph client shell CLIs for Java or Python.
AdbGraphClient
API to establish a connection to Graph Studio, start an environment with allocated
memory, load a PGQL property graph into memory,
execute PGQL queries and run algorithms against the graph.
- Prerequisites for Using Autonomous Database Graph Client
- Using the PGX JDBC Driver with the AdbGraphClient API
Starting from Graph Server and Client Release 24.1.0, you can use the PGX JDBC driver with theAdbGraphClient
API to query graphs stored in the memory of the graph server in Graph Studio on Autonomous Database.
Parent topic: Getting Started with the Client Tools
12.2.1 Prerequisites for Using Autonomous Database Graph Client
As a prerequisite requirement to get started with the
AdbGraphClient
API, you must:
- Provision an Autonomous Database instance in Oracle Autonomous Database.
- Obtain the following information if you are configuring the
AdbGraphClient
using the tenancy details. Otherwise, skip this step.Key Description More Information tenancy OCID The Oracle Cloud ID (OCID) of your tenancy To determine the OCID for your tenancy, see "Where to Find your Tenancy's OCID" in: Oracle Cloud Infrastructure Documentation. database Database name of your Autonomous Database instance - Open the OCI console and click Oracle Database in the left navigation menu.
- Click Autonomous Database and navigate to the Autonomous Databases page.
- Select the required Autonomous Database under the Display Name column and navigate to the Autonomous Database Details page.
- Note the Database Name under "General Information" in the Autonomous Database Information tab.
database OCID The Oracle Cloud ID (OCID) of your Autonomous Database - Open the OCI console and click Oracle Database in the left navigation menu.
- Click Autonomous Database and navigate to the Autonomous Databases page.
- Select the required Autonomous Database under the Display Name column and navigate to the Autonomous Database Details page.
- Note the Database OCID under "General Information" in the Autonomous Database Information tab.
username Graph enabled Autonomous Database username, used for logging into Graph Studio See Create a Graph User for more information. password Database password for the graph user If the password for a graph user is forgotten, then you can always reset password for the graph user by logging into Database Actions as the ADMIN user. See Edit User for more information. endpoint Graph Studio endpoint URL - Select your Autonomous Database instance and navigate to the Autonomous Database Details page.
- Click the Tools tab.
- Click on Graph Studio.
- Copy the URL of the new tab that opens the Graph Studio login screen.
- Edit the URL to remove the part after
oraclecloudapps.com
to obtain the endpoint URL.For example, the following shows the format of a sample endpoint URL:
https://<hostname_prefix>.adb.<region_identifier>.oraclecloudapps.com
- Access Graph Studio and create a PGQL property graph.
- Download, install and start the Oracle Graph Java or Python client.
Parent topic: Using Autonomous Database Graph Client
12.2.2 Using the PGX JDBC Driver with the AdbGraphClient API
Starting from Graph Server and Client Release 24.1.0, you can use the PGX
JDBC driver with the AdbGraphClient
API to query graphs stored in the memory of
the graph server in Graph Studio on Autonomous Database.
To use the PGX JDBC driver to connect to your Autonomous Database instance, note the following:
- Register the PGX JDBC driver with the
DriverManager:
import java.sql.DriverManager; import oracle.pgx.jdbc.PgxJdbcDriver; ... DriverManager.registerDriver(new PgxJdbcDriver());
- Use one of the following two ways to establish the connection using the
PGX JDBC Driver:
- Using
Properties
properties = new Properties(); properties.put("tenancy_ocid", "<tenancy_OCID>"); properties.put("database_ocid", "<database_OCID>"); properties.put("database", "<database_name>"); properties.put("username", "<username>"); properties.put("password", "<password>"); Connection connection = DriverManager.getConnection("jdbc:oracle:pgx:https://<hostname-prefix>.adb.<region>.oraclecloudapps.com", properties);
- Using a
Wallet
Connection connection = DriverManager.getConnection("jdbc:oracle:pgx:@<db_TNS_name>?TNS_ADMIN=<path_to_wallet>", "<ADB_username>", "<ADB_password>")
Note that the JDBC URL in the preceding code samples, use
jdbc:oracle:pgx:
as the prefix. - Using
Properties
Example 12-1 Using the PGX JDBC Driver to run graph queries in Autonomous Database
The following example establishes a connection using the PGX JDBC driver to connect to an Autonomous Database instance, starts the compute environment in Graph Studio, loads a graph into the graph server (PGX), creates a statement, and runs a PGQL query on the graph.
import java.sql.*;
import oracle.pgx.jdbc.*;
import oracle.pg.rdbms.*;
import oracle.pgx.api.*;
public class AdbPgxJdbc {
public static void main(String[] args) throws Exception {
DriverManager.registerDriver(new PgxJdbcDriver());
try (Connection conn = DriverManager.getConnection("jdbc:oracle:pgx:@<db_TNS_name>?TNS_ADMIN=<path_to_wallet>","ADB_username","<ADB_password>")) {
AdbGraphClient client = conn.unwrap(AdbGraphClient.class);
if (!client.isAttached()) {
var job = client.startEnvironment(10);
job.get();
System.out.println("job details: name=" + job.getName() + "type= " + job.getType() +"created_by= " + job.getCreatedBy());
}
PgxSession session = conn.unwrap(PgxSession.class);
PgxGraph graph = session.readGraphByName("BANK_PGQL_GRAPH", GraphSource.PG_PGQL);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * "+
"FROM GRAPH_TABLE ( BANK_PGQL_GRAPH "+
"MATCH (a IS ACCOUNTS) -[e IS TRANSFERS]-> (b IS ACCOUNTS) "+
"WHERE a.ID = 179 AND b.ID = 688 "+
"COLUMNS (e.AMOUNT AS AMOUNT ))");
while(rs.next()){
System.out.println("AMOUNT = " + rs.getLong("AMOUNT"));
}
}
}
}
The resulting output of the preceding code is as shown:
AMOUNT = 7562
Parent topic: Using Autonomous Database Graph Client